help in plotting variable from loop

2 ビュー (過去 30 日間)
Arjun Paudel
Arjun Paudel 2019 年 2 月 7 日
コメント済み: Arjun Paudel 2019 年 2 月 8 日
i am a beginner in matlab and was trying to plot a function needed for a exercise.
i am using a loop to calculate the value of the variable p1 for 10 times. the code calculates and displays the value but i also want to get the plot of p1 w.r.t number of iterations(i.e. i)
Thank you in advance for the help.
clc; clear all; close all;
% pressure decrease rate
p1 = 1000000; % starting pressure in the tank 10 bars equivalent in pascals
V = 0.0055; % volume of the tank 5 liters in m^3
M = 0.022897; % molar mass of air in kilograms
R = 8.3144598; %gas constant
T = 298.15; % environment temperature
m1 = 0;
v= 0.112e-3;
for i = 1:1:10
m = (M*V*p1)/(R*T);
m1 = (M*v*p1)/(R*T);
p1 = (R*T*(m-m1))/(V*M)
end
plot(i, p1)

採用された回答

João
João 2019 年 2 月 7 日
Try this:
figure(1)
for i = 1:1:10
m = (M*V*p1)/(R*T);
m1 = (M*v*p1)/(R*T);
p1 = (R*T*(m-m1))/(V*M);
disp(['p1 = ' ,num2str(p1)])
plot(i, p1, 'ko')
hold on
end
Or this (instead of plotting during the loop, you can save the p1 values and then do the plot:
% preallocate p1
p1 = zeros();
for i = 1:1:10
m = (M*V*p1)/(R*T);
m1 = (M*v*p1)/(R*T);
p1(i) = (R*T*(m-m1))/(V*M);
disp(['p1 = ' ,num2str(p1)])
end
figure(1)
plot(i, p1, 'k')
  1 件のコメント
Arjun Paudel
Arjun Paudel 2019 年 2 月 8 日
Thank you! Its helpful.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by