Matlab help, shouldn't be hard...
Ok everyone, I'm pretty rusty with matlab so I was wondering if I could get some help... I have a program that bascially is defining different parts of an equation and requires the input of a temperature and a pressure (P,T) Rather than individually inputing a pressure at a certain temp and finding the roots I want to put in a range of temperatures, say 200 K - 500K in 25 intervals and for each temperature I want to have it solve for the roots when P goes to 1 to 150 bar, and finally plot the roots. I tried doing the whole 1:1:150 for temp but I'm having problems with the matrix not being sqare and the whole bit. Here is what I have so far. Thanks for the help in advance!
w = 0.345; %accentric factor
M = 220.55; %critical pressure (bar)
N = 647.1; %critical temperature (K)
R = 83.14472; %bar.cm³/(mol.K)
T = 400; %Isotherm
P = input('Pressure:');
k = 0.480 + 1.574*w - 0.17*(w^2);
alpha_t = (1+k*(1-sqrt(T/N)))^2;
a_t = 0.42748*((R*N)^2)*alpha_t/M;
A = a_t*(P/((R*T)^2));
b = 0.08664*((R*N)/M);
B = (b*P)/(R*T);
beta = A-B-B^2;
gamma = -A*B;
Z = [1 -1 beta gamma]; %Root finder for cubic RSK EOS
G = roots(Z);
disp(G)
0
Comments
e.g. matrix_P .* matrix_T (multiplies each element of matrix_P by the element of the same index in matrix_T as long as both are the same size).
You'll get the matrix not square error if you omit the dot because then it tries to compute the vector product.
-drasnor
A = a_t*(P/((R*T)^2));
Ok here's an updated code. It still gives me the matrix isn't square. Any ideas?
-drasnor
As a reminder the ultimate goal is for the program to... set a temperature, find all the roots at a range of pressures, then set a new temp, find all the roots at the same range of pressure, etc. Ultimately I want to plot it out.
Note: whenever I get the program to "work" I just get a bunch of imaginary values, I should have A LOT more real values, I've put in seperate pressures individually and I get more reals. I don't think it's proccessing the way I want it to
I'll have a good answer for you tonight, after I get off work. Just making sure that you don't need this before then.
Nope I have plenty of time, thank you.
w = 0.345; %accentric factor M = 220.55; %critical pressure (bar) N = 647.1; %critical temperature (K) R = 83.14472; %bar.cm³/(mol.K) T = 200:25:600; %Isotherm P = 1:0.5:150; %Pressure Values P=P'; %Transpose to make matrix math nice k = 0.480 + 1.574*w - 0.17*(w^2); alpha_t = (1+k*(1-sqrt(T/N))).^2; a_t = 0.42748*((R*N)^2)*alpha_t/M; A = P*(a_t.*((R*T).^2).^(-1)); b = 0.08664*((R*N)/M); B = (b*P)*(R*T).^(-1); beta = A-B-B.^2; gamma = -A.*B; Z=zeros(length(P), 2*length(T)+2); %Initialize arrays G=zeros(2*length(T)+1,length(P)); for i=1:length(P) Z(i,:) = [1 -1 beta(i,:) gamma(i,:)]; %Root finder for cubic RSK EOS G(:,i) = roots(Z(i,:)); end disp(G)I'm getting a lot of imaginary values too, but I don't really know what cubic RSK EOS is. The answers are consistant with the values I get when I run single values for pressure through. eg, running P=1 through, then P=1.5 gives me the same values as running P=[1 1.5]. Basically, the matrix math is correct, but I don't know if your formulae are.
Here's what the new code is. My plot may be wrong. I want to plot Pressure vs the roots.
w = 0.345; %accentric factor M = 220.55; %critical pressure (bar) N = 647.1; %critical temperature (K) R = 83.14472; %bar.cm³/(mol.K) q1 = input('Temperature Range (1):'); q2 = input('Temperature Range (2):'); p1 = input('Pressure Range (1):'); p2 = input('Pressure Range (2):'); T = q1:10:q2; %Isotherm P = p1:0.5:p2; %Pressure Values P=P'; %Transpose to make matrix math nice k = 0.480 + 1.574*w - (0.17*(w^2)); alpha_t = (1+k*(1-sqrt(T/N))).^2; a_t = 0.42748*((R*N)^2)*alpha_t/M; A = P*(a_t.*((R*T).^2).^(-1)); b = 0.08664*((R*N)/M); B = (b*P)*(R*T).^(-1); beta = A-B-B.^2; gamma = -A.*B; Z=zeros(length(P), 2*length(T)+2); %Initialize arrays G=zeros(2*length(T)+1,length(P)); for i=1:length(P) Z(i,:) = [1 -1 beta(i,:) gamma(i,:)]; %Root finder for cubic RSK EOS G(:,i) = roots(Z(i,:)); end disp(G) plot(G,P)The code is 100% correct. I compaired values by going to this site:
http://www.cheng.cam.ac.uk/~pjb10/thermo/pure.html
The other problem is that you're trying to graph 3 dimensions of info (P,Z,T) using a 2-d graph (P,Z).
You can try a couple things:
1) only do it for one value of T
3) plot it using a 3-d plot
1 takes less work than 3, 3 is cooler than 1, but I prefer 2.