Solving 3rd order equation by solve.



Instead of Maxima's solve, you might consider the trigonometric solution:

http://home.pipeline.com/~hbaker1/cubic3realroots.htm

In your example, the 3 solutions are:

x = 1+r*cos(alpha)
x = 1+r*cos(alpha+2*%pi/3)
x = 1+r*cos(alpha-2*%pi/3),

where r=2*sqrt(5/3), alpha=atan(sqrt(257/243))/3.

If you start with

eqn1:(z-(1+r*cos(a)))*(z-(1+r*cos(a+2*%pi/3)))*(z-(1+r*cos(a-2*%pi/3)));

Then use trigexpand, expand, trigsimp, expand, trigsimp, expand, trigreduce, expand to get:

                                  2                     3      2
                    3      2   3 r  z         cos(3 a) r    3 r
(%o124)            z  - 3 z  - ------ + 3 z - ----------- + ---- - 1
                                 4                 4         4

(Why we need to do trigsimp twice, I don't know.)

Then substitute r=2*sqrt(5/3), followed by trigreduce & expand to get:

                                              3/2
                          3      2         2 5    cos(3 a)
(%o125)                  z  - 3 z  - 2 z - --------------- + 4
                                                 3/2
                                                3

Now, a=atan(sqrt(257/243))/3 to get:
                                   3      2
(%o126)                           z  - 3 z  - 2 z + 1

At 02:13 AM 2/26/2013, Pepe Sanchez wrote:
>>I want to know general methods to avoid a small imaginary error.
>
>   I guess that you need somethingh similar to the "chop" operator in Mathematica.
>
>You can try this.
>
>(%i1)      fpprintprec: 5$     /* just to avoid too much figures in output */
>
>(%i2)     s: float(rectform(solve(x^3-3*x^2-2*x+1,x)));       
>
>(%o2)  :  [x=.34338-2.2204*10^-16*%i,x=2.2204*10^-16*%i-.83424,x=1.1102*10^-16*%i+3.4909]
>
> (%i3)  zero: 1e-8 $   /* your arbitrary threshold */
> (%i4)  for i:1 thru length(s) do 
>                     (if imagpart(rhs(s[i])) <= 1e-8 then s[i]: x=realpart(rhs(s[i])) else false ) ;     
>
> (%i5)  s;
>(%o5) [x=.34338,x=-.83424,x=3.4909]
>
>Best
>________________________________________________________________
>Jose Sanchez-Marin.
> Universitat de Valencia.
> Institut de Ciencia Molecular (ICMol).
> Cat.  Jose Beltran Martinez, 2Phone: +34 96 354 4444
>E-46980 PaternaFAX:   +34 96 354 3576            
> Spain    e-mail: Jose.Sanchez at uv.es