Plot doesn't show anything

조회 수: 4 (최근 30일)
Dimitris Moutzouris
Dimitris Moutzouris 2020년 3월 16일
댓글: Mario Malic 2020년 3월 16일
Hello everyone, I am trying to make the following code work where I want to plot the variables fi and P together for each T value. I think I am doing something wrong regarding the "for" loop but I can't seem to solve it. I would appreciate your help.
clear,clc,clf,
R = 83.1446;
b = 29;
i = 200:10:900
for T = i + 273.15;
d = (-8374 + (19.437*T) - (8.148*0.001*T.^2))*10^6;
c = (290.78 - (0.30276*T) + (1.4774*0.0001*T.^2))*10^6;
e = (76600 - (133.9*T) + (0.1071*T.^2))*10^6;
for i =20:1:100;
v=i;
y = b./(4*v);
a = c + d./v + e./(v.^2);
P = (R*T.*(1 + y + y.^2 - y.^3))./(v.*(1-y).^3)- a./(sqrt(T)*v.*(v+b));
Z = (1+y+y.^2-y.^3)./((1-y).^3) - a./(R*T^1.5.*(v+b));
lnf =(8*y-9*y.^2+3*y.^3)./((1-y).^3)- log(Z)- c./(R*T^1.5.*(v+b))-d./(R*T^1.5.*v.*(v+b))
f=exp(lnf);
fi = f.*P
plot(P,fi)
drawnow
hold on
end
end
hold off
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2020년 3월 16일
Dimitris - when I run your code, I do see the axes being updated with "something" (though I don't know if it is correct or not). I do wonder about your loops though. You assign i to be an array
i = 200:10:900
which you iterate over (?) in the outer loop as
for T = i + 273.15;
Is this what you want? And then in your inner for loop, you re-use i as the step variable
for i =20:1:100;
v = i;
I recommend that you use another variable so as not to conflict with the array that you've already created.
Dimitris Moutzouris
Dimitris Moutzouris 2020년 3월 16일
Geoff, the first iteration is not needed but I use it in order to remember some things as the vectors symbolize some physical parametrs.
As for you second point,it is my fault, thank you.

댓글을 달려면 로그인하십시오.

채택된 답변

Cris LaPierre
Cris LaPierre 2020년 3월 16일
편집: Cris LaPierre 2020년 3월 16일
One potential issue - you have used the variable i twice. You should probably come up with a different counter variable for your second for loop.
You are also only plotting a single point at a time. This is likely why you can't see anything. Try changing your marker to something bigger.
You calculations are also returning imaginary numbers. I don't think there should be imaginary numbers in this calculation.
Here's a slightly reworked version.
R = 83.1446;
b = 29;
for temp = 200:10:900
T = temp + 273.15;
d = (-8374 + (19.437*T) - (8.148*0.001*T.^2))*10^6;
c = (290.78 - (0.30276*T) + (1.4774*0.0001*T.^2))*10^6;
e = (76600 - (133.9*T) + (0.1071*T.^2))*10^6;
for v =20:1:100
y = b/(4*v);
a = c + d./v + e./(v^2);
P = (R*T.*(1 + y + y.^2 - y.^3))./(v*(1-y)^3)- a./(sqrt(T)*v*(v+b));
Z = (1+y+y^2-y^3)./((1-y)^3) - a./(R*T.^1.5*(v+b));
lnf =(8*y-9*y^2+3*y^3)/((1-y)^3)- log(Z)- c/(R*T.^1.5*(v+b))-d/(R*T.^1.5*v*(v+b));
f=exp(lnf);
fi = abs(f.*P);
plot(P,fi,'o')
hold on
end
end
hold off
  댓글 수: 3
Dimitris Moutzouris
Dimitris Moutzouris 2020년 3월 16일
Greetings Cris, your solution totally worked. At some point ,I was getting an "imaginary numbers" error but I wasn't able to understand what I was doing wrongly. Thank you very much, I still have a lot of practice to do!
Cris LaPierre
Cris LaPierre 2020년 3월 16일
Welcome! I hope the answer makes sense. In the second one, I've created a 71x81 matrix. This is accomplished by defining temp as a column vector (71x1) and v as a row vector (1x81). When plotted, MATLAB treats each column as a data series (same color in the plot).

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Mario Malic
Mario Malic 2020년 3월 16일
편집: Mario Malic 2020년 3월 16일
clear,clc,clf,
R = 83.1446;
b = 29;
i = 200:10:900
for T = i + 273.15;
d = (-8374 + (19.437*T) - (8.148*0.001*T.^2))*10^6;
c = (290.78 - (0.30276*T) + (1.4774*0.0001*T.^2))*10^6;
e = (76600 - (133.9*T) + (0.1071*T.^2))*10^6;
for i =20:1:100;
v=i;
y = b./(4*v);
a = c + d./v + e./(v.^2);
P = (R*T.*(1 + y + y.^2 - y.^3))./(v.*(1-y).^3)- a./(sqrt(T)*v.*(v+b));
Z = (1+y+y.^2-y.^3)./((1-y).^3) - a./(R*T^1.5.*(v+b));
lnf =(8*y-9*y.^2+3*y.^3)./((1-y).^3)- log(Z)- c./(R*T^1.5.*(v+b))-d./(R*T^1.5.*v.*(v+b))
f=exp(lnf);
fi = f.*P
y_curve(i) = fi; % Obtains values for range of i from 20 to 100 and saves it as array
x_curve(i) = P; % same
end
hold on
plot(x_curve,y_curve)
end
I hope this is what you wanted to get. In your code, you were ploting for each point. So if you want to have curves on your plot like this code provides, improve it (since those two lines are unnecessary) and use it. Otherwise, change your plot line like shown below.
plot(P,fi, '*')
  댓글 수: 2
Dimitris Moutzouris
Dimitris Moutzouris 2020년 3월 16일
Thank you for your answer Mario. This solution wasn't what I was trying to find but you gave me some food for thought with the curves option!
Mario Malic
Mario Malic 2020년 3월 16일
plot(P,fi, '*')
This one provided what you wanted, but with valuable insights about loops from Cris, that answer is certainly better than mine.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by