MATLAB: Plotting and adding elements to an array

adding elements to an arrayplotting

%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.

Best Answer

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.