How do i solve a cubic function giving the answer in a matrix

Im trying to solve this cubic equation to give me the intersection points at Pr and give me the anser in a matrix format. Doing this only gives me one answer not three
Pr=input('Pr=');
solve(0==((8*0.9)/((8*Z)-1))-(27/(64*(Z^2)))-Pr,Z);
Z=subs(Z,Pr)

2 Comments

its pressure. its the y value of the graph that intersects the the cubic function to give me the three intersection points of Z.

Sign in to comment.

 Accepted Answer

Try this:
syms Z
Pr = 42; % Choose A Number ...
Zs = solve(0==((8*0.9)/((8*Z)-1))-(27/(64*(Z^2)))-Pr,Z)
Zroots = vpa(Zs)
Zrootsd = double(Zroots)
produces:
Zs =
root(z^3 - (41*z^2)/280 + (9*z)/896 - 9/7168, z, 1)
root(z^3 - (41*z^2)/280 + (9*z)/896 - 9/7168, z, 2)
root(z^3 - (41*z^2)/280 + (9*z)/896 - 9/7168, z, 3)
Zroots =
0.0036611845692819380187451079457217 - 0.094934986290941940021998513244942i
0.0036611845692819380187451079457217 + 0.094934986290941940021998513244942i
0.13910620229000755253393835553713
Zrootsd =
0.00366118456928194 - 0.0949349862909419i
0.00366118456928194 + 0.0949349862909419i
0.139106202290008 + 0i

4 Comments

Thanks this worked well but i how do i index the min, max and the middle value from three answers that are given.
As always, my pleasure.
how do i index the min, max and the middle value from three answers that are given
That depends on how you define those terms. The MATLAB max and min functions define them in terms of magnitude for complex results, not the real or imaginary components considered individually.
The sort function works differently. From the documentation:
Sort the elements of a complex vector by their real parts. For elements with equal real parts, sort breaks the tie based on their imaginary parts.’
So here, sorting from greatest to least:
ZrootSorted = sort(Zrootsd, 'descend')
ZrootSorted =
0.139106202290008 + 0i
0.00366118456928194 + 0.0949349862909419i
0.00366118456928194 - 0.0949349862909419i
If you need to compare them, I would use the sort function, and go from there.

Sign in to comment.

More Answers (1)

This is not a cubic polynomial. Fact. A cubic polynomial is of the form:
a + b*x + c*x^2 + d*x^3
What you have is a rational polynomial. Hint: a cubic polynomial does not have any points where the function goes to infinity for a real finite input. What happens in your function?
syms Z Pr
pretty(((8*0.9)/((8*Z)-1))-(27/(64*(Z^2)))-Pr)
36 27
----------- - Pr - -----
(8 Z - 1) 5 2
64 Z
So when Z==1/8 or Z==0, thig function is undefined, with a singularitiy at those locations. Again, is it a cubic pollynomial? No.
Now, as long as you are willing to assume that Z is NEVER going to be exactly 0 or 1/8, you can multiply by Z^2*(8*Z-1), converting the relation into one that is cubic polynomial in nature. It looks like solve does this for you.
Prvals = 1:5;
vpa(subs(solve(36/(5*(8*Z - 1)) - Pr - 27/(64*Z^2),Z),Pr,Prvals),5)
ans =
[ 0.21067, 0.19186, 0.18239, 0.17615, 0.17155]
[ 0.40717 - 0.29075i, 0.19157 - 0.31738i, 0.1213 - 0.28576i, 0.086927 - 0.2594i, 0.066727 - 0.23881i]
[ 0.40717 + 0.29075i, 0.19157 + 0.31738i, 0.1213 + 0.28576i, 0.086927 + 0.2594i, 0.066727 + 0.23881i]
And since you can the do a subs into the result for a set of values for Pr, that works. Be careful though, because for any value of Pr that would have yielded a solution of 1/8 for Z, that solution is actually spurious.

Tags

Community Treasure Hunt

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

Start Hunting!