简体   繁体   中英

Sub plotting a function with one input and 3 outputs in MATLAB

I'm attempting to plot three different graphs. h vs T, h vs P, and h vs rho. When i run the program i am getting three different graphs but no data on it. I'm not sure what im doing wrong.

Here is my code for the plot

for h=0:1:1000
[ T,P,rho ] = stdatm( h );
hold all 
subplot(1,3,1)
plot(T,h,'r')

subplot(1,3,2)
plot(P,h)

subplot(1,3,3)
plot(rho,h)
end

here is my function code

function [ T,P,rho ] = stdatm( h )
T0=288.16;
P0=101.325;
rho0=1.225;
a=-6.5*10^-3;
b=3*10^-3;
c=-4.5*10^-3;
d=4.0*10^-3;
R=286.9;
g=9.81;

T1=T0+a*11000;
P1=P0*(T1/T0)^(-g/(a*R));
rho1=rho0*(T1/T0)^((-g/(a*R))-1);

T2=T1;
P2=P1*exp((-g/(R*T2))*(25000-11000));
rho2=rho1*exp((-g/(R*T2))*(25000-11000));

T3=T2+b*(47000-25000);
P3=P2*(T3/T2)^(-g/(b*R));
rho3=rho2*(T3/T2)^((-g/(b*R))-1);

T4=T3;
P4=P3*exp((-g/(R*T4))*(53000-47000));
rho4=rho3*exp((-g/(R*T4))*(53000-47000));

T5=T4+c*(79000-53000);
P5=P4*(T5/T4)^(-g/(c*R));
rho5=rho4*(T5/T4)^((-g/(c*R))-1);

T6=T5;
P6=P5*exp((-g/(R*T6))*(90000-79000));
rho6=rho5*exp((-g/(R*T6))*(90000-79000));

T7=T6+d*(100000-90000);
P7=P6*(T7/T6)^(-g/(d*R));
rho7=rho6*(T7/T6)^((-g/(d*R))-1);

if h<=11000 
T=T0+a*h;
P=P0*(T/T0)^(-g/(a*R));
rho=rho0*(T/T0)^((-g/(a*R))-1);
elseif h>11000 && h<=25000
T=T1;
P=P1*exp((-g/(R*T))*(h-11000));
rho=rho1*exp((-g/(R*T))*(h-11000));
elseif h>25000 && h<=47000 
T=T2+b*(h-25000);
P=P2*(T/T2)^(-g/(b*R));
rho=rho2*(T/T2)^((-g/(b*R))-1);
elseif h>47000 && h<=53000
T=T3;
P=P3*exp((-g/(R*T3))*(h-47000));
rho=rho3*exp((-g/(R*T3))*(h-47000));
elseif h>53000 && h<=79000
T=T4+c*(h-53000);
P=P4*(T/T4)^(-g/(c*R));
rho=rho4*(T/T4)^((-g/(c*R))-1);
elseif h>79000 && h<=90000
T=T5;
P=P5*exp((-g/(R*T))*(h-79000));
rho=rho5*exp((-g/(R*T))*(h-79000));
elseif h>90000 && h<=100000
T=T6+d*(h-90000);
P=P6*(T/T6)^(-g/(d*R));
rho=rho6*(T/T6)^((-g/(d*R))-1);

end

Try the hold per each of the subplots:

for h=0:1:1000
[ T,P,rho ] = stdatm( h );
subplot(1,3,1)
plot(T,h,'r')
hold on 
subplot(1,3,2)
plot(P,h)
hold on 
subplot(1,3,3)
plot(rho,h)
hold on 
end
hold off

在此处输入图片说明

Vectorizing the function stdatm would be a better solution, though.

When i run my code I'm only getting vectors of one dimension for h,T,P,rho. Thus one data pt on each plot.

for h=0:1:100
[ T,P,rho ] = stdatm( h );
end
hold on
subplot(1,3,1)
plot(T,h)

subplot(1,3,2)
plot(P,h)

subplot(1,3,3)
plot(rho,h)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM