×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

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

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

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

(OP)
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 --> matlab

%% 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); 

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

My advice is to ask your professor.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources