How to change the input of the function

2 views (last 30 days)
Nick Gray
Nick Gray on 29 Mar 2022
Commented: Sam Chak on 30 Mar 2022
I am trying to write this code, with the input of altitude (h), to give me the temp, pressure, density. I'm not sure how to make it so that i can change the value of h. The function also seems to return the same numbers when h<11000, no matter what the input number is.
function [T, P, rho] = question1 (h)
T1 = 288.15;
L = -0.0065;
R = 287;
PSL = 101325;
g = 9.81;
rhoSL = 1.225;
if h<11000
T = T1 + L*R;
P = PSL * (T/T1)^((-g)/(L*R));
rho = rhoSL * (T/T1)^(((-g)/(L*R)) +1);
else 99
end
end
  1 Comment
Davide Masiello
Davide Masiello on 29 Mar 2022
The function returns the same values when h < 11000 because the equations in your conditional statement do not depend on h, but rather on constants.
If you could disclose your governing equations maybe a solution could be suggested.

Sign in to comment.

Answers (1)

Arif Hoq
Arif Hoq on 29 Mar 2022
Edited: Arif Hoq on 29 Mar 2022
%define your variable and call your function
T1 = 288.15;
L = -0.0065;
R = 287;
PSL = 101325;
g = 9.81;
rhoSL = 1.225;
[T, P, rho]=question1 (T1,L,R,PSL,g,rhoSL)
T = 286.2845
P = 9.7923e+04
rho = 1.1762
% your function
function [T, P, rho] = question1 (T1,L,R,PSL,g,rhoSL)
h=8000; % change your h
if h<=11000
T = T1 + L*R;
P = PSL * (T/T1)^((-g)/(L*R));
rho = rhoSL * (T/T1)^(((-g)/(L*R)) +1);
else
T=0;
P=0;
rho=0;
end
end
  5 Comments
Arif Hoq
Arif Hoq on 30 Mar 2022
Edited: Arif Hoq on 30 Mar 2022
See the result now. I have changed the value of h, so result also changed.
in additon, i have defined an anynomous value of conditional else . so please define your conditional else value according to your calculation.
%define your variable and call your function
T1 = 288.15;
L = -0.0065;
R = 287;
PSL = 101325;
g = 9.81;
rhoSL = 1.225;
[T, P, rho]=question1 (T1,L,R,PSL,g,rhoSL)
T = 0
P = 0
rho = 0
% your function
function [T, P, rho] = question1 (T1,L,R,PSL,g,rhoSL)
h=12000; % change your h
if h<=11000
T = T1 + L*R;
P = PSL * (T/T1)^((-g)/(L*R));
rho = rhoSL * (T/T1)^(((-g)/(L*R)) +1);
else
T=0;
P=0;
rho=0;
end
end
Sam Chak
Sam Chak on 30 Mar 2022
I remember my Aeronautics Professor showed me that the pressure P and density ρ of the air changes with altitude, h. Perhaps these formulas are helpful in the MATLAB code.
Then from the Ideal Gas Law, we can compute the Temperature at which pressure is calculated.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!