Plot inside a "for" loop

3 ビュー (過去 30 日間)
Cristian
Cristian 2013 年 4 月 19 日
Hello. I am trying to write a program that generates the pressure-volume diagram for a vehicle engine. I came up with this idea:
nn=length(0:10:720);
P=zeros(nn,1);
V=zeros(nn,1);
for alfa=0:10:720
cont=alfa/10+1;
cos_alfa=cosd(alfa);
x(cont)=76.8*((1-cos_alfa)/2+0.28/4-(0.28/4)*cos_alfa^2);
if alfa<=180
P(cont)=0.9;
elseif alfa>180 & alfa<=360
P(cont)=320.612/((x(cont)+8.93023)^1.32);
elseif alfa>360 & alfa<=540
P(cont)=1119.9/((x(cont)+8.93023)^1.24);
elseif alfa>540
P(cont)=1.1;
end
V(cont)= 0.0448882+x(cont)*pi*(80^2)/4*(10^(-6));
end
fprintf('\n For alfa %g [degrees], cos_alfa is %g, x is %g, P is %g [bar], V is %g [l] \n',alfa,cos_alfa,x,P,V);
figure(1)
plot(V,P,'-r'),title('Diagram P-V'),xlabel('Volume [l]'),ylabel('Pressure [bar]'),grid;
fprintf('\n')
But something in not quite right. The values that are generated by the program are incorrectly calculated. I cannot figure it out. Please help me. I also made an excel table and these are the correct values:
# α Cos(α) x P[bar] V[l]
# 0 1 0 0,9 0,044888245
# 10 0,984807753 0,74548852 0,9 0,048635479
# 20 0,939692621 2,944675899 0,9 0,0596898
# 30 0,866025404 6,488624495 0,9 0,077503629
# 40 0,766044443 11,20512708 0,9 0,101211357
# 50 0,64278761 16,87172209 0,9 0,12969477
# 60 0,5 23,232 0,9 0,161665014
# 70 0,342020143 30,01355396 0,9 0,195752822
# 80 0,173648178 36,94580374 0,9 0,23059811
# 90 6,12574E-17 43,776 0,9 0,264930421
# 100 -0,173648178 50,28198379 0,9 0,297633062
# 110 -0,342020143 56,28070097 0,9 0,327785904
# 120 -0,5 61,632 0,9 0,354684466
# 130 -0,64278761 66,23781051 0,9 0,377835795
# 140 -0,766044443 70,03734031 0,9 0,396934315
# 150 -0,866025404 72,99937551 0,9 0,411823128
# 160 -0,939692621 75,11306918 0,9 0,422447711
# 170 -0,984807753 76,37872395 0,9 0,428809586
# 180 -1 76,8 0,9 0,43092715
# 190 -0,984807753 76,37872395 0,905871259 0,428809586
# 200 -0,939692621 75,11306918 0,923921965 0,422447711
# 210 -0,866025404 72,99937551 0,955514834 0,411823128
# 220 -0,766044443 70,03734031 1,003106372 0,396934315
# 230 -0,64278761 66,23781051 1,070571086 0,377835795
# 240 -0,5 61,632 1,163761373 0,354684466
# 250 -0,342020143 56,28070097 1,291446563 0,327785904
# 260 -0,173648178 50,28198379 1,466886302 0,297633062
# 270 -1,83772E-16 43,776 1,710494123 0,264930421
# 280 0,173648178 36,94580374 2,054404795 0,23059811
# 290 0,342020143 30,01355396 2,550357131 0,195752822
# 300 0,5 23,232 3,283087245 0,161665014
# 310 0,64278761 16,87172209 4,391348961 0,12969477
# 320 0,766044443 11,20512708 6,091903227 0,101211357
# 330 0,866025404 6,488624495 8,664639763 0,077503629
# 340 0,939692621 2,944675899 12,23115402 0,0596898
# 350 0,984807753 0,74548852 16,02792799 0,048635479
# 360 1 0 17,81724439 0,044888245
# 361 0,999847695 0,007485963 74,07242254 0,044925873
# 362 0,999390827 0,029940075 73,84231701 0,04503874
# 364 0,99756405 0,119699901 72,93523792 0,045489922
# 365 0,996194698 0,186960353 72,2686271 0,04582801
# 366 0,994521895 0,269098467 71,46935741 0,046240881
# 368 0,990268069 0,477834722 69,50837079 0,047290104
# 370 0,984807753 0,74548852 67,13210745 0,048635479
# 380 0,939692621 2,944675899 52,07581649 0,0596898
# 390 0,866025404 6,488624495 37,66976556 0,077503629
# 400 0,766044443 11,20512708 27,05627337 0,101211357
# 410 0,64278761 16,87172209 19,8942875 0,12969477
# 420 0,5 23,232 15,13799475 0,161665014
# 430 0,342020143 30,01355396 11,94082367 0,195752822
# 440 0,173648178 36,94580374 9,745657003 0,23059811
# 450 3,06287E-16 43,776 8,204814666 0,264930421
# 460 -0,173648178 50,28198379 7,102113193 0,297633062
# 470 -0,342020143 56,28070097 6,301157072 0,327785904
# 480 -0,5 61,632 5,714101556 0,354684466
# 490 -0,64278761 66,23781051 5,283192082 0,377835795
# 500 -0,766044443 70,03734031 4,969825313 0,396934315
# 510 -0,866025404 72,99937551 4,748002461 0,411823128
# 520 -0,939692621 75,11306918 4,600380691 0,422447711
# 530 -0,984807753 76,37872395 4,515899646 0,428809586
# 540 -1 76,8 4,488399041 0,43092715
# 550 -0,984807753 76,37872395 1,1 0,428809586
# 560 -0,939692621 75,11306918 1,1 0,422447711
# 570 -0,866025404 72,99937551 1,1 0,411823128
# 580 -0,766044443 70,03734031 1,1 0,396934315
# 590 -0,64278761 66,23781051 1,1 0,377835795
# 600 -0,5 61,632 1,1 0,354684466
# 610 -0,342020143 56,28070097 1,1 0,327785904
# 620 -0,173648178 50,28198379 1,1 0,297633062
# 630 -4,28802E-16 43,776 1,1 0,264930421
# 640 0,173648178 36,94580374 1,1 0,23059811
# 650 0,342020143 30,01355396 1,1 0,195752822
# 660 0,5 23,232 1,1 0,161665014
# 670 0,64278761 16,87172209 1,1 0,12969477
# 680 0,766044443 11,20512708 1,1 0,101211357
# 690 0,866025404 6,488624495 1,1 0,077503629
# 700 0,939692621 2,944675899 1,1 0,0596898
# 710 0,984807753 0,74548852 1,1 0,048635479
# 720 1 0 1,1 0,044888245
  5 件のコメント
the cyclist
the cyclist 2013 年 4 月 19 日
This is a lot of code and output to look at. Can you be much more specific about what is wrong? Can you post the smallest possible example that illustrates the problem?
Ahmed A. Selman
Ahmed A. Selman 2013 年 4 月 20 日
So interesting.
I've ran the code you posted, and got some nice figure. Then I've copied your data into a text file, imported it to Excel, re-imported it to Matlab, and draw P vs. V, and got the very same, very nice, figure. SO where is the problem.. :)
If you think there is some problem with the code, then re-check your math, and your excel data as well.. please.

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

採用された回答

bym
bym 2013 年 4 月 19 日
Please see comments in code. I checked a couple of values and they agree with your excel table, but it is up to you to verify that it is indeed providing the correct calculations
clc;clear,close all
nn=length(0:10:720);
P=zeros(nn,1);
V=zeros(nn,1);
for alfa=0:10:720
cont=alfa/10+1;
cos_alfa=cosd(alfa);
x(cont)=76.8*((1-cos_alfa)/2+0.28/4-(0.28/4)*cos_alfa^2);
if (alfa<=180)
P(cont)=0.9;
elseif (alfa<=360) % you do not need the && statement, if it greater than 180 don't test for it
P(cont)=320.612/((x(cont)+8.93023)^1.32);
elseif (alfa<=540)
P(cont)=1119.9/((x(cont)+8.93023)^1.24);
else % last statement is an 'else' since other options are exhausted
P(cont)=1.1;
end
V(cont)= 0.0448882+x(cont)*pi*(80^2)/4*(10^(-6));
end
%fprintf('\n For alfa %g [degrees], cos_alfa is %g, x is %g, P is %g [bar], V is %g [l] \n',alfa,cos_alfa,x,P,V);
figure(1)
plot(V,P,'-r'),title('Diagram P-V'),xlabel('Volume [l]'),ylabel('Pressure [bar]'),grid;
fprintf('\n')
fprintf('x\t\t\tP[bar]\t\t\tV[l]\n') %table output might be clearer
fprintf('%-6.4f\t\t\t%-6.4f\t\t\t%-6.4f\n',[x' P V]')

その他の回答 (1 件)

Sysenso Systems
Sysenso Systems 2013 年 4 月 20 日
You program generates P and V values for the alfa=0:10:720. That has an increment of 10. But the Excel table has the expected values for alfa=[0:10:360 361 362 364 365 366 368 370:10:720].
Here are the two solutions: 1.If you compare the generated data with the Excel file by ignoring the rows for alfa = 361 362 364 365 366 368, then everything works fine!
2. Modify the code to work for alfa = 361 362 364 365 366 368. Here is the modified code.
nn=length([0:10:360 361 362 364 365 366 368 370:10:720]);
P=zeros(nn,1);
V=zeros(nn,1);
cont = 1;
for alfa=[0:10:360 361 362 364 365 366 368 370:10:720]
cos_alfa=cosd(alfa);
x(cont)=76.8*((1-cos_alfa)/2+0.28/4-(0.28/4)*cos_alfa^2);
if alfa<=180
P(cont)=0.9;
elseif alfa>180 & alfa<=360
P(cont)=320.612/((x(cont)+8.93023)^1.32);
elseif alfa>360 & alfa<=540
P(cont)=1119.9/((x(cont)+8.93023)^1.24);
elseif alfa>540
P(cont)=1.1;
end
V(cont)= 0.0448882+x(cont)*pi*(80^2)/4*(10^(-6));
cont = cont +1;
end
fprintf('\n For alfa %g [degrees], cos_alfa is %g, x is %g, P is %g [bar], V is %g [l] \n',alfa,cos_alfa,x,P,V);
figure(1)
plot(V,P,'-r'),title('Diagram P-V'),xlabel('Volume [l]'),ylabel('Pressure [bar]'),grid;
fprintf('\n')
By the way, there is no Plot inside a "for" loop in the code!!!
  1 件のコメント
INDRANIL RANJIT SALVI
INDRANIL RANJIT SALVI 2017 年 11 月 15 日
Please give me some pointers on how to start plotting cylinder pressure vs swept volume graph for which we have the cylinder pressure and the total swept volume.How to incorporate engine load settings

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

カテゴリ

Help Center および File ExchangeCustomizations and Preferences についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by