Smart questions
Smart people
 Find A ForumFind An Expert
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Remember Me

Are you an
Engineering professional?
Join Eng-Tips now!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

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

Just copy and paste the

#### Feedback

"...I think the site is just incredible....I am learning more here than anywhere else before and am looking forward to being able to help someone .... this is my idea of what the Internet is supposed to do..."

#### Geography

Where in the world do Eng-Tips members come from?

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

 Forum Search FAQs Links Jobs Whitepapers MVPs
 MassiveAerospacial (Aerospace) 2 Jul 12 9:44
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); ```
 Brad1979 (Electrical) 2 Jul 12 16:41
 IRstuff (Aerospace) 2 Jul 12 18:26

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!