help in plotting variable from loop
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Arjun Paudel
el 7 de Feb. de 2019
Comentada: Arjun Paudel
el 8 de Feb. de 2019
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)
0 comentarios
Respuesta aceptada
João
el 7 de Feb. de 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')
Más respuestas (0)
Ver también
Categorías
Más información sobre Line Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!