필터 지우기
필터 지우기

For looping/if statement issue (simple hopefully)

조회 수: 1 (최근 30일)
Jenna Broders
Jenna Broders 2022년 6월 23일
댓글: Voss 2022년 6월 23일
I'm trying to obtain a temperature based on the given polynomial profiles over the length z. There are two conditions, if z is less than or equal to 0.32 use Ts1 and Tg1, and if z is greater than 0.32 use Ts2 and Tg2. I know what the final values should be because I generated these equations from a plot. So, I've tried the loop using only a range that suits Ts1 and Tg1 conditon and it worked. Then a tried a range that suits Ts2 and Tg2 condition and it worked. But when I try the whole range, it only uses Ts2 ad Tg2 equations, it is skiping the first if statement condition.
z=linspace(0,0.407);
i = 100;
for c = i
%explicit equations
Ts1 = ((-3508440.83).*(z.^5))+((3221859.34).*(z.^4))-((1089760.82).*(z.^3))+((165361.13).*(z.^2))-((12015.96).*z)+(1179.21);
Ts2 = ((-1906981.2).*(z.^3))+((2073664.15).*(z.^2))-((754331.65).*(z))+(92096.47);
Tg1 = ((2796881.8).*(z.^5))-((2553240.18).*(z.^4))+((852324.51).*(z.^3))-((124780.34).*(z.^2))+((6092.92).*(z))+(889.33);
Tg2 = ((-1567143.11).*(z.^3))+((1751141.7).*(z.^2))-((653704.46).*(z))+(81812.04);
%Temperature Loop
if z(:,c)>=0 && z(:,c)<=0.32
Ts = Ts1;
Tg = Tg1;
elseif z(:,c)>0.32 && z(:,c)<=0.414
Ts = Ts2;
Tg = Tg2;
end
Tg
Ts
end
APPROXIMATE FINAL VALUES FOR REFERENCE
Length [m] Temperature Solid [C] Temperature Gas [C]
0 1200 890
0.007 1085.714 920
0.017 1000 965
0.02857 950 985.71
0.05 890.5714 970
0.1 828.857 870
0.15 772 800
0.2 714 742.8
0.25 642 685.7
0.3 600 620
0.32 572 600
0.335 400 400
0.35 357.14 360
0.3893 200 257.14
0.4 85.714 200
0.407 28 185

답변 (1개)

Voss
Voss 2022년 6월 23일
편집: Voss 2022년 6월 23일
There are a few problems.
Problem 1:
for c = i
tells the loop to iterate once, with the value of c being i. I imagine you meant:
for c = 1:i
which tells the loop to iterate i times, with the c taking values starting with 1 and going to i.
Problem 2:
if ...
Ts = Ts1;
Tg = Tg1;
elseif ...
Ts = Ts2;
Tg = Tg2;
end
This overwrites Ts and Tg on each iteration, so after the loop, you only ever have the value from the last iteration (which are the entire Ts1/Tg1 or Ts2/Tg2 vectors).
Instead, take only the appropriate single element each iteration:
if ...
Ts(c) = Ts1(c);
Tg(c) = Tg1(c);
elseif ...
Ts(c) = Ts2(c);
Tg(c) = Tg2(c);
end
Problem 3:
if z(:,c)>=0 && z(:,c)<=0.32
% ...
elseif z(:,c)>0.32 && z(:,c)<=0.414
% ...
end
What happens if z(:,c)<0 or z(:,c)>0.414? Then neither of those conditions is true, so Ts and Tg do not get assigned to, so they have their value left over from the previous iteration, or they are undefined if this happens on the first iteration. As you said, there are two conditions ("z is less than or equal to 0.32 ... z is greater than 0.32"), so there is only one check necessary:
if z(:,c)<=0.32
% ...
else
% ...
end
Inefficiency (but not a problem in that it doesn't impact the result): You can calculate Ts1, Ts2, Tg1, Tg2 one time before the loop, rather than on each iteration of the loop, because these vectors contain the values for all elements of z, so they don't depend on the loop iterator variable c.
However, all that being said, you can replace your loop approach with a simple and efficient logical indexing approach:
z=linspace(0,0.407);
%explicit equations
Ts1 = ((-3508440.83).*(z.^5))+((3221859.34).*(z.^4))-((1089760.82).*(z.^3))+((165361.13).*(z.^2))-((12015.96).*z)+(1179.21);
Ts2 = ((-1906981.2).*(z.^3))+((2073664.15).*(z.^2))-((754331.65).*(z))+(92096.47);
Tg1 = ((2796881.8).*(z.^5))-((2553240.18).*(z.^4))+((852324.51).*(z.^3))-((124780.34).*(z.^2))+((6092.92).*(z))+(889.33);
Tg2 = ((-1567143.11).*(z.^3))+((1751141.7).*(z.^2))-((653704.46).*(z))+(81812.04);
Tg = NaN(size(Tg1)); % initialize Tg and Ts
Ts = NaN(size(Ts1));
idx = z <= 0.32; % idx is a vector of logicals (true/false) saying whether each element of z is <= 0.32
Tg(idx) = Tg1(idx); % take Tg1 when z <= 0.32
Ts(idx) = Ts1(idx); % take Ts1 when z <= 0.32
Tg(~idx) = Tg2(~idx); % take Tg2 when z > 0.32
Ts(~idx) = Ts2(~idx); % take Ts2 when z > 0.32
plot(z,Tg)
hold on
plot(z,Ts)
legend({'Gas' 'Solid'})
ylabel('T')
xlabel('z')
  댓글 수: 4
Jenna Broders
Jenna Broders 2022년 6월 23일
Right makes sense! Thanks again!! This was very helpful
Voss
Voss 2022년 6월 23일
You're welcome! Any other questions, let me know. Otherwise, please "Accept This Answer". Thanks!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by