Plotting Results of a While Loop

Hello,
I want to iteratively plot the results of my while loop, however, I end up with either one point on the graph as the output, or a blank graph.
Any help would be greatly appreciated,
Thank you
% ---------------------------------------------------------
% AIR DENSITY AS A FUNCTION OF PRESSURE AND TEMPERATURE
% ---------------------------------------------------------
clc
clear
h = 0;
while h <= 11000
T = 15.04 - 0.00649 * h;
P = 101.29 * ((T + 273.15)/288.08).^(5.256);
rho = P/(0.2869*(T + 273.15));
h = h + 10;
end
plot(h,rho)

Réponses (2)

madhan ravi
madhan ravi le 22 Fév 2019
h = 0 : 10 : 11000;
T = 15.04 - 0.00649 * h;
P = 101.29 * ((T + 273.15)./288.08).^(5.256);
rho = P./(0.2869*(T + 273.15));
plot(h,rho) % if you want to see the movement use comet() instead of plot()
Yasasvi Harish Kumar
Yasasvi Harish Kumar le 22 Fév 2019
Modifié(e) : Yasasvi Harish Kumar le 25 Fév 2019
Hi,
Use an array to store the values so that you can plot them.
% ---------------------------------------------------------
% AIR DENSITY AS A FUNCTION OF PRESSURE AND TEMPERATURE
% ---------------------------------------------------------
clc
clear
h = 0;
i = 1;
while h <= 11000
T = 15.04 - 0.00649 * h;
P = 101.29 * ((T + 273.15)/288.08).^(5.256);
x(i) = h;
rho(i) = P/(0.2869*(T + 273.15));
h = h + 10;
i = i+1;
end
plot(x,rho)
Regards

5 commentaires

Jared Guzman
Jared Guzman le 23 Fév 2019
I am still getting a blank graph...?
I am sorry, I forgot to increment i.
% ---------------------------------------------------------
% AIR DENSITY AS A FUNCTION OF PRESSURE AND TEMPERATURE
% ---------------------------------------------------------
clc
clear
h = 0;
i = 1;
while h <= 11000
T = 15.04 - 0.00649 * h;
P = 101.29 * ((T + 273.15)/288.08).^(5.256);
x(i) = h;
rho(i) = P/(0.2869*(T + 273.15));
h = h + 10;
i = i+1;
end
plot(x,rho)
This should work.
madhan ravi
madhan ravi le 25 Fév 2019
If your plotting outside the loop then loop is not required it can be vectorized.
Yasasvi Harish Kumar
Yasasvi Harish Kumar le 25 Fév 2019
I agree @madhan ravi. Its a better approach.
Lea Dratwa
Lea Dratwa le 7 Déc 2021
Thank you very much!

Connectez-vous pour commenter.

Catégories

En savoir plus sur 2-D and 3-D Plots dans Centre d'aide et File Exchange

Tags

Commenté :

le 7 Déc 2021

Community Treasure Hunt

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

Start Hunting!

Translated by