Solving a cubic function to get one numeric result?
显示 更早的评论
I am trying to solve a cubic function from which I would like to get one numeric result as my variable. I've been able to do this by other means, but not with MATLAB and that's what I would need to do. I am not very good with Matlab for Im new with it so any help would be much appreciated!
My equation is; Solve for, V: V^3 - x * V^2 + y * V - z = 0.
In reality x = b+ ((R*T)/p), y = a/p and z = (a*b)/p. I know all these variables/results of the calculations so I only need to find out what V is.
So in one case the equation can be expressed as: V^3 - 0.10933127 * V^2 + 0.00082152 * V - 0.00002193 = 0
I have tried the following means and failed:
syms V
eqn = V^3 - 0.30993828*V^2 + 0.002598122*V - 0.0000693698 == 0;
S = solve(eqn,V,'Real',true)
With the first one I get symbolic results and cant open them by doubleclicking, I dont know if this is normal?
And
a = 1;
b = 0.30993828;
c = 0.002598122;
d = 0.0000693698;
function sols = solve_cubic(a, b, c, d)
syms x
sols = solve(a*x^3 - b*x^2 + c*x - d, x)
end
For some reason this does not give me anything for x in workspace, only values a,b,c,d that I already list.
And
p = [1 -0.10933127 0.00082152 -0.00002198];
r = roots(p)
With this I get a column vector with each row being "0.XXX + 0.XXXi".
If there is someone with extra knowledge thank you in advance!
Best regards,
-Tarmo
采纳的回答
更多回答(1 个)
You aren't going to solve this properly by getting an output of one numerical solution because a cubic equation does actually have three solutions.
Of your options I would use the roots function. My hunch is that if those aren't the correct solutions to the cubic then you have probably not defined your coefficients correctly before putting them into roots.
If you are after a single solution then you may need to take the output and apply some other functions to pick the one you want. For example if you wanted the real valued solution only then you could use:
r=real(roots(p))
or something to that effect.
类别
在 帮助中心 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!