Strange result of symbolically solving cubic equation?
Show older comments
I am trying to use solve() to solve the cubic equation w*t^3 - 3*w*t^2 + 3*w*t - r = 0 for t, where 0<=t<=1, using the following code:
clear;
syms w t r;
expr = w*t^3 - 3*w*t^2 + 3*w*t - r;
% symbolic roots of t
tval = solve(expr, t);
% validate t
stval = subs(tval, {w}, {20});
ft = subs(stval, r, 17.5);
I could get the correct result of t = 0.5, just as ft shows. However, if I replace w by b-a, like the following code,
clear;
syms a b w t r;
w = b-a;
expr = w*t^3 - 3*w*t^2 + 3*w*t - r;
% symbolic roots of t
tval = solve(expr, t);
% validate t
stval = subs(tval, {a b}, {0 20});
ft = subs(stval, r, 17.5);
the result of t becomes incorrect ( ft=1 ).
It seems that the tval in later code has items like (d^2)^(1/2), where d is negative, so the item would be simplified as -d. But in the earlier code, these items are simplified to d which is correct.
Does anybody have any ideas about it?
Thanks so much!
1 Comment
Walter Roberson
on 30 Jan 2011
Maple generates identical results for the two situations.
Answers (1)
Andrew Newell
on 30 Jan 2011
I don't know why this is happening. I tried the same equations in Maple and got the right answer both ways, so maybe it is a bug in the Matlab implementation. The question you're probably thinking is: How can I be sure I'm getting the right answer? I can suggest two approaches to improve your chances. First, try substituting the solutions back into the equation:
simplify(subs(expr,t,tval(1))) % also tval(2), tval(3)
This gives zero for your first example but not for your second. My other suggestion is to substitute first, then solve:
clear
syms w t r
expr = subs(w*t^3 - 3*w*t^2 + 3*w*t - r,{w r},{20 17.5});
tval = solve(expr,t)
tval =
1/2
5/4 - (3^(1/2)*i)/4
5/4 + (3^(1/2)*i)/4
Categories
Find more on Common Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!