How to extract only the real roots of a cubic equation

3 views (last 30 days)
I am resolving a cubic virial equation of state with a root method in 3 loops for (for Pressure, Temperature and type of Gas). So I have complexe and real solutions. I want, though, only the real ones. Here is the part of the code that doesn't work.
........
for j=1:numel(P)
coef1(i,j,k)=(Rg*T(i))/(P(j))
coef2(i,j,k)=(B2(i,k))*((Rg*T(i))/(P(j)));
coef3(i,j,k)=(B3(i,k))*((Rg*T(i))/(P(j)));
racines{i,j,k}=((roots([coef3(i,j,k), coef2(i,j,k), coef1(i,j,k), -1])));
imagi{i,j,k}=imag(racines{i,j,k})
vraiRacines{i,j,k}=racines{imag(racines{i,j,k})==0}
celldisp(vraiRacines)
end
end
end
This gives:
vraiRacines{2,1,1} =
-8.3654e+04
5.4637e+01
vraiRacines{3,1,1} =
-8.3654e+04
5.4637e+01
vraiRacines{1,2,1} =
-8.3654e+04
5.4637e+01
vraiRacines{2,2,1} =
-8.3654e+04
5.4637e+01
vraiRacines{3,2,1} =
-8.3654e+04
5.4637e+01
So it actually extracts the real solutions but it gives the same real value for all {i,j,k}.... 5.4637e+01.
So I think it enters the loop once and then does the same thing for all i,j and k...
Would you have a solution for me please?

Answers (0)

Categories

Find more on Function Creation 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!