Plotting and adding elements to an array

3 views (last 30 days)
%Tc = input('PLease enter the critical temperature'); %Critical Temp. in Kelvins
Tc = 154.6;
%Pc = input('Please enter the critical pressure'); %Critical Pressure in MPa
Pc = 50.46;
%omega = input('Please enter the omega value of the Peng_robinson EOS');
omega = 0.021;
k = 0.37464 + (1.54226 - 0.26992*omega)*omega;
R = 0.08314; %gas constant in MPa*m3/mol*K
%v = input('Please enter a vector of pressure');
v = 1:10:100;
%n = input('Please enter a vector of temperature');
n = [173 198 223 248 273 298 323 348 373 398 423];
%n = 173;
b = 0.077796074*R*Tc/Pc;
x=[];
for P = v
for T = n
alpha = (1 + k*(1-sqrt((T/Tc))))^2;
a = 0.457235529*(R*Tc)^2/Pc*alpha;
B = b.*P/(R*T);
A = a.*P/((R*T)^2);
ZZ = ZZroot(A,B);
V = ZZ*R*T./P
end
x(end+1)=V;
end
What I am trying to do is, if possible, is for each element n, I add the values V computed for each corresponding different P(small v) to an array. For example,
[V(1) ; V(2) ; V(3); ...].
| v(1) v(2) v(3) ...| <- pressure small v
n(1)| V(1) V(2) V(3) ...|
n(2)| V(1) V(2) V(3) ...|
n(3)| V(1) V(2) V(3) ...|
.
.
.
Once I do that, I would like to plot each row as its own line in the same graph. x axis would be V and y axis v(small v).
Hope that makes sense. Thanks in advance.
  2 Comments
Guillaume
Guillaume on 24 Aug 2015
Am I understanding correctly that you already know the y-axis values (the small v you start with), and that you are actually calculating the x-axis values (the big V) with your code?
And in the end, there is only one set of y-axis values but several sets of x-axis value (of for each n). Therefore all the curves have the same y-coordinates but for different x coordinates?
Abdullah Al-Alawi
Abdullah Al-Alawi on 25 Aug 2015
Edited: the cyclist on 25 Aug 2015
Yes. Exactly. y-axis(small v) is the same for each line. My main goal is to be able to produce a graph like this one:
blue lines are the n values(isotherms). y-axis(pressure aka small v). x-axis(Volume V). I know how to plot each line, if the user enters one Temperature(n), but I'm having issues in plotting for each isotherm. By the way, since the difference is big between pressure(small v) and V, we are going to convert the values natural log scale. That's the way to get the shape of each line to look like the one in the picture provided.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 25 Aug 2015
Note that you don't need the loops. You can meshgrid your starting vectors and apply your equations straight onto matrices. If ZZroot can't work directly with matrices, you can always use bsxfun to apply it to each element of the matrices:
%starting arrays: n and v
%Tc, Pc, k, R, b are scalar constants
[P, T] = meshgrid(v, n); %n is row of matrices, v is column.
alpha = (1 + k*(1-sqrt((T/Tc)))).^2;
a = 0.457235529*(R*Tc)^2/Pc*alpha;
B = b*P./(R*T);
A = a.*P./((R*T).^2);
%may work:
ZZ = ZZroot(A,B);
%guaranteed to work:
ZZ = bsxfun(@ZZroot, A, B);
V = ZZ*R*.T./P; %rows of v correspond to each n, columns to each v
%plot the result:
plot(V', v); %transposing V is optional, unless the matrix is square.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!