Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with this code (if(elseif/else statements) please!!!

Status
Not open for further replies.

MassiveAerospacial

Aerospace
Oct 25, 2011
2
0
0
Dear Matlab users,

I am currently writing a code to output air temperature, pressure, and density based on the geometric altitude of an aircraft. The code is below.

What I would like to focus your attention on is my "if" statements. Basically, I have a table of values and equations in front of me which can calculate temperature/pressure/density in specific altitude ranges. What I tried doing in the code is telling the computer that if my altitude (Z) is in a particular range, then execute this formula and assign this value to, say, temperature. However, my first problem is I get a squiggly red line underneath each "Z" value for temperature "if" statements saying "Variable Z might be set by a non-scalar operator", and also the code skips all if statements and only executes the last "if" statement for temperature even if Z is not in that range of the last "if" statement!

Some help/advice would be VASTLY appreciated!

Code:
%% Comments

% Chapter 1, question 1.5 of "Mechanics of Flight
% H=geometric altitude
% Z=geopotential altitude
% T=temperature
% p=pressure
% rho=air density

%% Input H

H=input('Geometric altitude is: ');

%% Calculating Z

RE=6356766;
Z=(RE*H)./(RE+H);

%% Calculating temperature

if 0<= Z <=11000
    T=288.150-0.0065*(Z-0);
end

if 11000<= Z <=20000
    T=216.650;
end

if 20000<= Z <=32000
    T=216.650+0.0010*(Z-20000);
end

if 32000<= Z <=47000
    T=228.650+0.0028*(Z-32000);
end

if 47000<= Z <=52000
    T=270.650;
end

if 52000<= Z <=61000
    T=270.650-0.0020*(Z-52000);
end

if 61000<= Z <=79000
    T=252.650-0.0040*(Z-61000);
end

if 79000<= Z <=90000
    T=180.650;
end

if Z>90000
    error('Geopotential altitude out of range (max=90000m), please try a lower altitude');
end

%% Calculating pressure

p0=101325;
p1=22632.04912;
p2=5474.881674;
p3=868.0168756;
p4=110.9059745;
p5=59.00074835;
p6=18.21000498;
p7=1.037706534;

if 0<= Z <=11000
    p=p0*((288.150-0.0065*(Z-0))/288.150)^(-9.806645/(287.0528*-0.0065));
end

if 11000<= Z <=20000
    p=p1*exp(-(9.806645*(Z-11000))/(287.0528*216.650));
end

if 20000<= Z <=32000
    p=p2*((216.650+0.0010*(Z-20000))/216.650)^(-9.806645/(287.0528*0.0010));
end

if 32000<= Z <=47000
    p=p3*((228.650+0.0028*(Z-32000))/228.650)^(-9.806645/(287.0528*0.0028));
end

if 47000<= Z <=52000
    p=p4*exp(-(9.806645*(Z-47000))/(287.0528*270.650));
end

if 52000<= Z <=61000
   p=p5*((270.650-0.0020*(Z-52000))/270.650)^(-9.806645/(287.0528*-0.0020));
end

if 61000<= Z <=79000
      p=p6*((252.650-0.0040*(Z-61000))/252.650)^(-9.806645/(287.0528*-0.0040));
end

if 79000<= Z <=90000
    p=p7*exp(-(9.806645*(Z-79000))/(287.0528*180.650));
end

if Z>90000
    error('Geopotential altitude out of range (max=90000m), please try a lower altitude');
end

%% Calculating air density

rho=p/(287.0528*T);

%% Displaying results

disp('Temperature: '); disp(T);
disp('Air pressure: '); disp(p);
disp('Air density: '); disp(rho);
 
Replies continue below

Recommended for you

or your TA

TTFN
faq731-376
7ofakss
 
Status
Not open for further replies.
Back
Top