Else if statement imbedded in a for loop

1 次查看(过去 30 天)
I am trying to use the matrix Tp inside a for loop to prodice a matrix. The first problem i have is that it always chooses the else statement and the second problem is that it produces a 5x17 matrix for each value in the Tp matrix. What I am trying to do is get the for loop to read each value of Tp and then the else if statement will decide if it is less than or greater than 1600 to use the right equation then that answer will fill a column in the 5x17 matrix so that the 17 values in Tp will fill the 17 comlumns in the new matrix. I am using MATLAB R2019a
clc; clear;
format short g
MC=10;MH=22;MO=0;
ycc=MC+MH/4-MO/2;
ymin=ycc-MC/2;
Tr=500;
A=[299180,309070;56835,93048;88923,154670;43388,127010;31317,44639];
B=[37.85,39.29;66.27,68.58;49.36,60.43;42.27,46.25;37.46,39.32];
C=[-4571.9,-6201.9;-11634,-16979;-7940.8,-19212;-6635.4,-18798;-4559.3,-6753.4];
Tp=[600,700,800,900,1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000,2100,2200];
Hfuel=-6312300;
Mfuel=142;
Mair=28.97;
if Tr<=1600
hbarR=[A(1,1)+B(1,1)*Tr+C(1,1)*log(Tr);A(2,1)+B(2,1)*Tr+C(2,1)*log(Tr);A(3,1)+B(3,1)*Tr+C(3,1)*log(Tr);A(4,1)+B(4,1)*Tr+C(4,1)*log(Tr);A(5,1)+B(5,1)*Tr+C(5,1)*log(Tr)];
else
hbarR=[A(1,2)+B(1,2)*Tr+C(1,2)*log(Tr);A(2,2)+B(2,2)*Tr+C(2,2)*log(Tr);A(3,2)+B(3,2)*Tr+C(3,2)*log(Tr);A(4,2)+B(4,2)*Tr+C(4,2)*log(Tr);A(5,2)+B(5,2)*Tr+C(5,2)*log(Tr)];
end
for i=1:length(Tp)
if Tp<=1600
HbarP=[A(1,1)+B(1,1)*Tp+C(1,1)*log(Tp);A(2,1)+B(2,1)*Tp+C(2,1)*log(Tp);A(3,1)+B(3,1)*Tp+C(3,1)*log(Tp);A(4,1)+B(4,1)*Tp+C(4,1)*log(Tp);A(5,1)+B(5,1)*Tp+C(5,1)*log(Tp)];
DeltaHbar=[HbarP-hbarR]
else
hbarP=[A(1,2)+B(1,2)*Tp+C(1,2)*log(Tp);A(2,2)+B(2,2)*Tp+C(2,2)*log(Tp);A(3,2)+B(3,2)*Tp+C(3,2)*log(Tp);A(4,2)+B(4,2)*Tp+C(4,2)*log(Tp);A(5,2)+B(5,2)*Tp+C(5,2)*log(Tp)];
DeltaHbar=[hbarP-hbarR]
end
end

回答(1 个)

S. Walter
S. Walter 2020-11-2
编辑:S. Walter 2020-11-2
If you want your for loop to pick only one item from the Tp vector, you have to iterate Tp inside your for loop.
Thus:
if Tp<=1600
HbarP=[A(1,1)+B(1,1)*Tp+C(1,1)*log(Tp);
A(2,1)+B(2,1)*Tp+C(2,1)*log(Tp);
A(3,1)+B(3,1)*Tp+C(3,1)*log(Tp);
A(4,1)+B(4,1)*Tp+C(4,1)*log(Tp);
A(5,1)+B(5,1)*Tp+C(5,1)*log(Tp)];
DeltaHbar=[HbarP-hbarR]
This doesn't tell Matlab to only look at the i-th iteration of Tp, but at the full vector of Tp and will give you a logical matrix. You can see that if you enter the following
>> Tp<1600
ans =
1×17 logical array
Columns 1 through 15
1 1 1 1 1 1 1 1 1 1 0 0 0 0 0
Columns 16 through 17
0 0
You would have to change your if statement to index through Tp inside your for loop:
if Tp(i)<=1600
HbarP=[A(1,1)+B(1,1)*Tp(i)+C(1,1)*log(Tp(i));
A(2,1)+B(2,1)*Tp(i)+C(2,1)*log(Tp(i));
A(3,1)+B(3,1)*Tp(i)+C(3,1)*log(Tp(i));
A(4,1)+B(4,1)*Tp(i)+C(4,1)*log(Tp(i));
A(5,1)+B(5,1)*Tp(i)+C(5,1)*log(Tp(i))];
DeltaHbar=[HbarP-hbarR];
  4 个评论
CW
CW 2020-11-2
I did
DeltaHbar(:,i)=HbarP-hbarR;
and I got the error "unable to perform assingment because the indicies on the left side are not compatible with the size of the right side". so i tried adding in
DeltaHbar=zeros(5,17);
to the script at the top with all the data to see if writing the matrix first would help and got the same error saying "Unable to perform assignment because the size of the left side is 5-by-1 and the size of the right side is 5-by-17." i understand they are not the same size but how would I go about it trying to fill one column at a time for each iteration of the loop?
S. Walter
S. Walter 2020-11-3
What is the size of your HbarP and hbarR in the loop? You can set a breakpoint by clicking on the little dash to the side of the line where it breaks:
then try the command
size(HbarP)
When I do that, I get:
K>> size(HbarP)
ans =
5 1
(Note that the "K" in front of the double arrow means you are in debug mode)
If your size is not 5 by 1, which it sounds from the error code is the problem, then you probably forgot to index Tp somewhere in your code.

请先登录,再进行评论。

类别

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