Plot doesn't show anything

2 次查看(过去 30 天)
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
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
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
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 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by