Graph Not Plotting for the For Loop Code
이전 댓글 표시
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?
채택된 답변
추가 답변 (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)
Arif Hoq
2022년 2월 21일
your output is a single value. if your code is going well then try this with a marker
plot (h,rho, 'o')
댓글 수: 2
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
2022년 2월 21일
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

