if elseif giving too many outputs
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi everyone, I'm trying to finish writing a matlab code that will read an input for a certain altitude, and then calculate a couple of properties based on that altitude. The thing is, for each range of altitude, the values for the variables used in calculating these properties changes, so I have to reassign each variable new values based on the range this input falls under.
Here is my code:
if (H <= 11000)
Hbi = 0;
deltai = 1;
KTi = -6.5;
Tbi = 288.15;
LinP = (deltai.*(1+((KTi/Tbi).*(H-Hbi))).^(expterm/KTi));
ConstP = (deltai.*exp(-((expterm)/Tbi).*(H-Hbi)));
fprintf('Linear pressure ratio = %f\n', LinP);
elseif (H < 20000)
Hbi= 11000;
deltai = 0.22336;
KTi = 0;
Tbi = 216.65;
LinP = (deltai.*(1+((KTi/Tbi).*(H-Hbi))).^(expterm/KTi));
ConstP = (deltai.*exp(-((expterm)/Tbi).*(H-Hbi)));
fprintf('Linear pressure ratio = %f\n', LinP);
end
Please note that "expterm", etc. has already been established, so that's not my issue. There is no error when I run it. When I run it, it asks for the input, and when I put in the input, it gives me like ten thousand different values.
댓글 수: 0
답변 (2개)
Image Analyst
2017년 9월 25일
That's because LinP is an array of like ten thousand elements, and this:
fprintf('Linear pressure ratio = %f\n', LinP);
will repeat for every one of those ten thousand elements. LinP is 10,000 values because either H or expterm is 10,000 values.
댓글 수: 2
Walter Roberson
2017년 9월 25일
Note that if H is a vector, then
if (H <= 11000)
is the same as
if all(H(:) <= 11000)
which would be false if even one value of H > 11000.
Walter Roberson
2017년 9월 25일
LinP = nan(size(H));
ConstP = nan(size(H));
mask = H <= 11000;
Hbi = 0;
deltai = 1;
KTi = -6.5;
Tbi = 288.15;
LinP(mask) = (deltai.*(1+((KTi/Tbi).*(H(mask)-Hbi))).^(expterm/KTi));
ConstP(mask) = (deltai.*exp(-((expterm)/Tbi).*(H(mask)-Hbi)));
mask = H > 11000 & H < 20000;
Hbi= 11000;
deltai = 0.22336;
KTi = 0;
Tbi = 216.65;
LinP(mask) = (deltai.*(1+((KTi/Tbi).*(H(mask)-Hbi))).^(expterm/KTi));
ConstP(mask) = (deltai.*exp(-((expterm)/Tbi).*(H(mask)-Hbi)));
plot(H, LinP)
You did not have a final "elseif" giving a value for the situation where H was not in either range, so I assigned NaN for that case.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!