필터 지우기
필터 지우기

Wrong loop, please help!

조회 수: 4 (최근 30일)
surawut.A
surawut.A 2021년 12월 19일
댓글: Image Analyst 2021년 12월 19일
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.

답변 (1개)

Image Analyst
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
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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by