help in plotting variable from loop
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
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')
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!