Graph Not Plotting for the For Loop Code
Show older comments
I'm trying to plot a graph of air density against altitude using the following density altitude eq on the NASA website:
This is the code I've written:
global rho
for h = 0:10:40000
if (h < 11000)
T = 25.05 - h.*0.00649;
p = 101.29* ((T + 273.15)./288.08)^5.256;
else
if (11000 < h) & (h< 250000)
T = -56.46;
p = 22.65* exp(1.73 - h.*0.000157);
else
if (h > 25000)
T = -131.21 + h.*0.00299;
p = 2.488*((T + 273.15)/216.6)^-11.3888;
end
end
end
rho = p / (0.2869*(T + 273.15));
end
plot (h,rho)
xlabel('height')
ylabel('Density')
But nothing is showing up on the graph plot. What am I missing?
Accepted Answer
More Answers (2)
H=linspace(0,40000,4001)
rho=zeros(1,numel(H))
for i=1:numel(H)
h = H(i);
if h > 25000
T = -131.21 + h.*0.00299;
p = 2.488*((T + 273.15)/216.6)^-11.3888;
elseif h<= 25000 && h>11000
T = -56.46;
p = 22.65* exp(1.73 - h.*0.000157);
else
T = 25.05 - h.*0.00649;
p = 101.29* ((T + 273.15)./288.08)^5.256;
end
rho(i) = p / (0.2869*(T + 273.15));
end
plot(H,rho)
1 Comment
Asit Rahman
on 21 Feb 2022
Arif Hoq
on 21 Feb 2022
your output is a single value. if your code is going well then try this with a marker
plot (h,rho, 'o')
2 Comments
G = 6.67408e-11; % Universal Gravitational Constant
M = 5.9722e24; % Mass of the earth
R = 6371e3; % Radius of the earth(m)
A = 21*pi; % Area of the rocket
Cd = 0.4; % Drag Coefficient for the rocket
t = 0:1:4000;
%h = 9.8*t.^2;
%h = 0:100:5000;
for h = 0:10:4000
if (h < 11000)
T = 25.05 - h.*0.00649;
p = 101.29* ((T + 273.15)./288.08)^5.256;
else
if (11000 < h) & (h< 250000)
T = -56.46;
p = 22.65* exp(1.73 - h.*0.000157);
else
if (h > 25000)
T = -131.21 + h.*0.00299;
p = 2.488*((T + 273.15)/216.6)^-11.3888;
end
end
end
rho = p / (0.2869*(T + 273.15));
end
plot (h,rho, 'o')
xlabel('height')
ylabel('Density')
Asit Rahman
on 21 Feb 2022
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
