Wrong loop, please help!

clear all
clc
%%input parameter%%
R=8.314;
T=[873:100:1073];%%%K
Cs=[15 30 45];%%Carbon at surface
Cx=0.12;
C0= 0.08;%Carbon in specimen
t=3600;% sec
% find diffusion coefficients
D=-0.27*(1.04/-0.22)*exp((-246)./(R*T));%(unit: cm^2*sec^-1)
% use D to find how far that carbon can diffusion
Erf = (Cs-Cx)./(Cs-C0);
z=NaN(size(Erf));
for n=1:numel(Erf)
if Erf(n)<2
z(n)=(-0.3725*Erf(n)^2)+(1.2144*(Erf(n)))+(0.0006);%error(0.01-0.08%) 0.4286
else
z(n)=(-0.0109*Erf(n)^2)+(0.0577*(Erf(n)))+(0.9235);%error(<0.01%)
end
%find X6
x=z*(2*sqrt(D*t));%%% ***t=3600 ประกาศตัวแปลเพิ่ม หน่วย***
end
%Graph 1
T=[873:100:1073];
Cs=[0.15 0.30 0.45];
[T,Cs]=meshgrid(T,Cs);
x=z(n)*(2*sqrt(D*t));
mesh(T,Cs,x)
Ylabel('length Carbon can diffuse(cm)','fontsize',18);
Xlabel('temperature (°C)','fontsize',18);
Zslabel('CarbonStart ,fontsize',18);
If you run program, the problem on Z. After looping I got 3 Z and all correct but on x formula, I cant use Z. I try use z(n) but it like average from looping. X should have 9 answer isnt it? also cant continue plot 3d graph because Z must be matrix.
So I have 2 problem
  1. I can't use z from loop
  2. I can't plot 3d graph
Please help & thank you for any help.

Answers (1)

You assign x inside the loop and outside. You can just do it in one place. If you want to assign it inside, you have
x=z*(2*sqrt(D*t));
when you should probably have
x(n) = z(n) * (2*sqrt(D*t));
If you want to do it outside the loop instead, you should probably do
x = z * (2*sqrt(D*t));
but again, you don't need to assign x in both places.

8 Comments

thank you for your help, but i try to use your code and it says
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Error in MatlabNEWWWWW (line 22)
x(n) = z(n) * (2*sqrt(D*t));
OK, this fixes the x and z stuff but I don't know what you're wanting to plot or do with mesh():
%%input parameter%%
R=8.314;
T=[873:100:1073];%%%K
Cs=[15 30 45];%%Carbon at surface
Cx=0.12;
C0= 0.08;%Carbon in specimen
t=3600;% sec
% find diffusion coefficients
D=-0.27*(1.04/-0.22)*exp((-246)./(R*T)) %(unit: cm^2*sec^-1)
D = 1×3
1.2338 1.2381 1.2416
% use D to find how far that carbon can diffusion
Erf = (Cs-Cx)./(Cs-C0);
z=NaN(size(Erf));
scalingFactor = 2*sqrt(D*t)
scalingFactor = 1×3
133.2934 133.5258 133.7151
for n = 1 : numel(Erf)
if Erf(n)<2
z(n)=(-0.3725*Erf(n)^2)+(1.2144*(Erf(n)))+(0.0006);%error(0.01-0.08%) 0.4286
else
z(n)=(-0.0109*Erf(n)^2)+(0.0577*(Erf(n)))+(0.9235);%error(<0.01%)
end
%find X6
x = z .* scalingFactor %%% ***t=3600 ประกาศตัวแปลเพิ่ม หน่วย***
end
x = 1×3
112.1316 NaN NaN
x = 1×3
112.1316 112.4116 NaN
x = 1×3
112.1316 112.4116 112.5990
%Graph 1
T=[873:100:1073];
Cs=[0.15 0.30 0.45];
[T,Cs]=meshgrid(T,Cs);
x = z .* scalingFactor; % Not needed here since it was computed inside the loop.
mesh(T,Cs,x)
Error using mesh (line 71)
Z must be a matrix, not a scalar or vector.
Ylabel('length Carbon can diffuse(cm)','fontsize',18);
Xlabel('temperature (°C)','fontsize',18);
Zslabel('CarbonStart ,fontsize',18);
thank you very much
if not bothering you,
as you can see at
Erf = (Cs-Cx)./(Cs-C0);
z=NaN(size(Erf));
scalingFactor = 2*sqrt(D*t)
z=Erf so it have 3 answer
and also scalingFactor use D from above, have 3 answer same
so it should have 9 answer or am i wrong
but anyway big thank to you
Image Analyst
Image Analyst on 19 Dec 2021
Edited: Image Analyst on 19 Dec 2021
So do you want a 3x3 matrix where we have a value for each of the 3 z values combined with each of the 3 D values? And what is x for?
surawut.A
surawut.A on 19 Dec 2021
Edited: surawut.A on 19 Dec 2021
I want to plot 3d graph by using
x=length Carbon can diffuse
T=Temperature
Cs=CarbonStart
the value of z and D use to find x
Im really new to this :(
So do you want a bar chart where x and y are two of those things, and the bar height is another of those things?
Tell me which chart here:
is closest to what you want/expect to see.
OK, use surf(). So x is x, ,y is Cs, and z (surface height) is T?
So do you have a T for every single combination of x and y? Or are some missing you you need to interpolate with scatteredInterpolant, like the attached demo?

Sign in to comment.

Tags

Asked:

on 19 Dec 2021

Commented:

on 19 Dec 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!