Plot doesn't show anything

2 views (last 30 days)
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 Comments
Geoff Hayes
Geoff Hayes on 16 Mar 2020
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 on 16 Mar 2020
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.

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 16 Mar 2020
Edited: Cris LaPierre on 16 Mar 2020
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 Comments
Dimitris Moutzouris
Dimitris Moutzouris on 16 Mar 2020
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 on 16 Mar 2020
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).

Sign in to comment.

More Answers (1)

Mario Malic
Mario Malic on 16 Mar 2020
Edited: Mario Malic on 16 Mar 2020
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 Comments
Dimitris Moutzouris
Dimitris Moutzouris on 16 Mar 2020
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 on 16 Mar 2020
plot(P,fi, '*')
This one provided what you wanted, but with valuable insights about loops from Cris, that answer is certainly better than mine.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!