help in plotting variable from loop

2 views (last 30 days)
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)

Accepted Answer

João
João on 7 Feb 2019
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')

More Answers (0)

Community Treasure Hunt

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

Start Hunting!