Wrong loop, please help!
Show older comments
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
- I can't use z from loop
- I can't plot 3d graph
Please help & thank you for any help.
Answers (1)
Image Analyst
on 19 Dec 2021
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
surawut.A
on 19 Dec 2021
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)
% use D to find how far that carbon can diffusion
Erf = (Cs-Cx)./(Cs-C0);
z=NaN(size(Erf));
scalingFactor = 2*sqrt(D*t)
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
%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)
Ylabel('length Carbon can diffuse(cm)','fontsize',18);
Xlabel('temperature (°C)','fontsize',18);
Zslabel('CarbonStart ,fontsize',18);
surawut.A
on 19 Dec 2021
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?
Image Analyst
on 19 Dec 2021
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.
surawut.A
on 19 Dec 2021
Image Analyst
on 19 Dec 2021
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?
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!