Plot doesn't show anything
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Dimitris Moutzouris
le 16 Mar 2020
Commenté : Mario Malic
le 16 Mar 2020
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 commentaires
Geoff Hayes
le 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.
Réponse acceptée
Cris LaPierre
le 16 Mar 2020
Modifié(e) : Cris LaPierre
le 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 commentaires
Cris LaPierre
le 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).
Plus de réponses (1)
Mario Malic
le 16 Mar 2020
Modifié(e) : Mario Malic
le 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 commentaires
Mario Malic
le 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.
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!