Wrong loop, please help!
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
Image Analyst
2021-12-19
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 个评论
Image Analyst
2021-12-19
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?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!