Wrong loop, please help!

1 view (last 30 days)
surawut.A
surawut.A on 19 Dec 2021
Commented: Image Analyst on 19 Dec 2021
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)

Image Analyst
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
Image Analyst
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?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!