How to change the input of the function

1 Ansicht (letzte 30 Tage)
Nick Gray
Nick Gray am 29 Mär. 2022
Kommentiert: Sam Chak am 30 Mär. 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 Kommentar
Davide Masiello
Davide Masiello am 29 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Arif Hoq
Arif Hoq am 29 Mär. 2022
Bearbeitet: Arif Hoq am 29 Mär. 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 Kommentare
Arif Hoq
Arif Hoq am 30 Mär. 2022
Bearbeitet: Arif Hoq am 30 Mär. 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 am 30 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by