"Young Hong" <hong_young11@yahoo.com> wrote in message <ht4okr$gvp$1@fred.mathworks.com>...
> Hi,
>
> I'm trying to plot a 3D graph of Pr,z,Tr. My program goes into infinite loops and doesn't plot a graph either. I am wondering if something gone wrong or made an error some where.
>
> Main function file:
> %main program, Cubic Equation of State
> clear all; clc
> global A B R T P
> R= 0.08206; Tc= 512.6; Pc= 80.9; w=0.559;
>
>
> %loop for pressure
> countA=0;
> for j=[1:2:200];
> countA=countA+1;
> countB=0;
> for i = [300:5:800];
> countB = countB+1;
> press(countA,countB)= j;
> P=press(countA, countB);
> Temp(countA,countB) = i;
> T = Temp(countA, countB);
>
> m = 0.37464+ 1.54226*w0.26992*(w^2);
> alpha = (1+m*(1(T/Tc)^0.5))^2;
> a = 0.45724*(((R*Tc)^2)/Pc);
> b = 0.07780*R*Tc/Pc;
> A= a*alpha*P/((R^2)*(T^2));
> B= b*P/R*T;
>
> Pr=P./Pc;
> Tr=T./Tc;
>
> zseed = P/(T*R);
> z(countA, countB) = fzero('algebraic',zseed);
>
> end
> end
>
> surf(Tr,z,Pr)
>
>
> %function file
> function y=algebraic(z)
> global A B ;
> y= z^3(1B)*z^2+(A2*B3*B^2)*z+(A*B+B^2+B^3);
> end
>
> Thank you.
There was no infinite loop on my computer for this program  it took only a couple of minutes. I can tell you why you got no surf plot, though. You didn't save Tr and Pr in arrays to match the mesh array of z. The single values that were present in Tr and Pr at the time you called 'surf' were the last ones calculated in your forloops. Naturally 'surf' doesn't like that.
The cubic you define in 'algebraic' has three roots, apparently all real, which depend on A and B, and your call to 'fzero' finds only one of them each time, based on the estimate given by 'zseed'. The surface plot using it looked very smooth and consistent without any discontinuities. Do you have some way of knowing that this will produce the root you are seeking?
Roger Stafford
