To find the solutions of cubic equation in Matlab, we can use the roots
function. However, we need to convert the equation to a specific form before that. Here's how you can do that:
w
as (-b + sqrt(b^2 - 4ac + 3*a*(-c + sqrt(c^2 - (b^2/(3*a))^3))))^(1/3)
.s
and t
as (2*w^2 - b)/(3*a)
and (-w^2 - b + sqrt(3)*i*w + 2*sqrt(3)*sqrt(w^2 - c))/(3*a)
.x = s + t + (-1/3)*b/a
.Here's the complete code that implements this method:
main.m339 chars14 lines
Note that this method only works for real coefficients. If your coefficients are complex, you will need to use the sym
function to convert them to symbolic form and then use the solve
function instead of roots
.
gistlibby LogSnag