solution of quadratic equations -- incorrect solution



Krishna,

The problem you are describing has to do with the sign ambiguity of the
square root operator. Neither solutions are "right" or "wrong" per se; they
are in fact equivalent up to the sign of sqrt. However, only one sign choice
yields valid solutions of the original equation. Which sign is the right one
depends on the numerical values of r1 and r2.

Consider the following two cases:

	r1=1.2, r2=1.8,

and

	r1=2.2, r2=-1.8.

Note how in the first case, it's sola that is the "right" solution of e1a,
whereas in the second case, it's solb:

(%i1) display2d:false$
(%i2) e1a: x^2+y^2=r1^2$
(%i3) e2a: (x-1)^2+(y-1)^2=r2^2$
(%i4) sola: solve([e1a,e1a-e2a],[x,y])$
(%i5) radcan(ev([e1a,e2a],sola[1]));
(%o5) [r1^2 = r1^2,r2^2 = r2^2]
(%i6) radcan(ev([e1a,e2a],sola[2]));
(%o6) [r1^2 = r1^2,r2^2 = r2^2]
(%i7) e1b: x^2+y^2=(r1-err)^2$
(%i8) e2b:(x-1)^2+(y-1)^2=(r2-err)^2$
(%i9) solb: solve([e1b,e1b-e2b],[x,y])$
(%i10) radcan(ev([e1b,e2b],solb[1]));
(%o10) [r1^2-2*err*r1+err^2 = r1^2-2*err*r1+err^2,
        r2^2-2*err*r2+err^2 = r2^2-2*err*r2+err^2]
(%i11) radcan(ev([e1b,e2b],solb[2]));
(%o11) [r1^2-2*err*r1+err^2 = r1^2-2*err*r1+err^2,
        r2^2-2*err*r2+err^2 = r2^2-2*err*r2+err^2]
(%i12) radcan(sola[1]-ev(solb[1],err=0));
(%o12) [0 = 0,0 = 0]
(%i13) radcan(sola[2]-ev(solb[2],err=0));
(%o13) [0 = 0,0 = 0]
(%i14) n1a:sola,r1=1.2,r2=1.8$
(%i15) n1b:solb,r1=1.2,r2=1.8,err=0$
(%i16) e1a,n1a,r1=1.2;
(%o16) 1.44 = 1.44
(%i17) e1a,n1b,r1=1.2;
(%o17) 1.270589256539026 = 1.44
(%i18) n2a:sola,r1=2.2,r2=-1.8$
(%i19) n2b:solb,r1=2.2,r2=-1.8,err=0$
(%i20) e1a,n2a,r1=2.2;
(%o20) .2721120854381727 = 4.84
(%i21) e1a,n2b,r1=2.2;
(%o21) 4.84 = 4.84

In reality, neither of these solutions is right or wrong, one just needs to
take a precise look at the branch cut of the sqrt function and how it
applies to the problem at hand. The numerical difference between the two
solutions arises because of the order in which Maxima evaluates the square
roots and takes the principal value.

In many cases, calling radcan can help; it will not guarantee (of course)
that the solution you find is the "right" one for a given set of values, but
at least it improves consistency between expressions.


Viktor



-----Original Message-----
From: maxima-bounces at math.utexas.edu [mailto:maxima-bounces at math.utexas.edu]
On Behalf Of Krishna Myneni
Sent: Tuesday, July 03, 2012 1:00 PM
To: Viktor T. Toth
Cc: maxima at math.utexas.edu
Subject: Re: [Maxima] solution of quadratic equations -- incorrect solution

On 07/03/2012 07:16 AM, Viktor T. Toth wrote:
> If you check your y-solutions, you will find that the differences are due
to
> trivial algebra and the solutions are, in fact, identical when you set
> err=0. You have expressions that are formally just like
sqrt(A-B)*sqrt(C-D)
> vs. sqrt(B-A)*sqrt(D-C), which may appear different at first but they
really
> aren't. You can actually verify this using Maxima itself, as well as
verify
> that the solutions that were obtained indeed solve your system of
equations:
>
> (%i1) display2d:false$
> (%i2) e1a: x^2+y^2=r1^2$
> (%i3) e2a: (x-1)^2+(y-1)^2=r2^2$
> (%i4) sola: solve([e1a,e1a-e2a],[x,y])$
> (%i5) radcan(ev([e1a,e2a],sola[1]));
> (%o5) [r1^2 = r1^2,r2^2 = r2^2]
> (%i6) radcan(ev([e1a,e2a],sola[2]));
> (%o6) [r1^2 = r1^2,r2^2 = r2^2]
> (%i7) e1b: x^2+y^2=(r1-err)^2$
> (%i8) e2b:(x-1)^2+(y-1)^2=(r2-err)^2$
> (%i9) solb: solve([e1b,e1b-e2b],[x,y])$
> (%i10) radcan(ev([e1b,e2b],solb[1]));
> (%o10) [r1^2-2*err*r1+err^2 = r1^2-2*err*r1+err^2,
>          r2^2-2*err*r2+err^2 = r2^2-2*err*r2+err^2]
> (%i11) radcan(ev([e1b,e2b],solb[2]));
> (%o11) [r1^2-2*err*r1+err^2 = r1^2-2*err*r1+err^2,
>          r2^2-2*err*r2+err^2 = r2^2-2*err*r2+err^2]
> (%i12) radcan(sola[1]-ev(solb[1],err=0));
> (%o12) [0 = 0,0 = 0]
> (%i13) radcan(sola[2]-ev(solb[2],err=0));
> (%o13) [0 = 0,0 = 0]
>
>
> Viktor
>
>
>
>
>
>
> -----Original Message-----
> From: maxima-bounces at math.utexas.edu
[mailto:maxima-bounces at math.utexas.edu]
> On Behalf Of Krishna Myneni
> Sent: Monday, July 02, 2012 7:39 PM
> To: maxima at math.utexas.edu
> Subject: solution of quadratic equations -- incorrect solution
>
> Previously, I posted that Maxima's solve() was unable to solve a system of
> two quadratic equations directly. Several workarounds were suggested to
find
> the solution to the following system of equations:
>
> e1:   (x - a)^2 + (y - b)^2 = r1^2
>
> e2:  (x - c)^2 + (y - d)^2 = r2^2
>
> While the workarounds permit Maxima's solve() to obtain analytic solutions
> to the above equations, we have found that Maxima gives an incorrect
> solution to a slight variant of the above equations. Instead of the
general
> equations above, it is simpler to illustrate the problem with the
following
> eqns:
>
> e1:  x^2 + y^2 = (r1 - err)^2
>
> e2: (x - 1)^2 + (y - 1)^2 = (r2 - err)^2
>
> If we first find the analytic solutions to the set, [x^2 + y^2 = r1^2,
> (x-1)^2 + (y-1)^2 = r2^2], and then find the solutions to the above
> equations with "err", substituting err=0 into the latter gives solutions
> which are not the same as the former, as shown below. Comparing the
> algebraic expressions with err=0 show that the x-solutions are the same,
but
> the y-solutions have a sign error in product terms r1*r2.
>
>
>
> (%i1) e1: x^2 + y^2 = r1^2;
>                                    2    2     2
> (%o1)                            y  + x  = r1
> (%i2) e2: (x-1)^2 + (y-1)^2 = r2^2;
>                                    2          2     2
> (%o2)                      (y - 1)  + (x - 1)  = r2
>
> (%i4) solve([e1,e1-e2],[x,y]);
>
>                         4        2        2     4       2          2     2
>                sqrt(- r2  + (2 r1  + 4) r2  - r1  + 4 r1  - 4) + r2  - r1
-
> 2
> (%o4) [[x = -
> ---------------------------------------------------------------,
>                                               4
>
>                2               2             2               2          2
> 2
> y = (sqrt(- r2  + 2 r1 r2 - r1  + 2) sqrt(r2  + 2 r1 r2 + r1  - 2) - r2  +
> r1 + 2)/4],
>
> [x =
>           4        2        2     4       2          2     2
> sqrt(- r2  + (2 r1  + 4) r2  - r1  + 4 r1  - 4) - r2  + r1  + 2
> ---------------------------------------------------------------,
>                                 4
>
>                  2               2             2               2
2
> y = - (sqrt(- r2  + 2 r1 r2 - r1  + 2) sqrt(r2  + 2 r1 r2 + r1  - 2) + r2
>
>               2
>           - r1  - 2)/4]
>
> ]
>
> Generalizing these equations slightly, and finding the new solutions, we
> have,
>
> (%i15) e1: x^2 + y^2 = (r1-err)^2;
>                                2    2             2
> (%o15)                       y  + x  = (r1 - err)
> (%i16) e2: (x-1)^2 + (y-1)^2 = (r2-err)^2;
>                                2          2             2
> (%o16)                 (y - 1)  + (x - 1)  = (r2 - err)
> (%i17) sol: solve([e1,e1-e2],[x,y]);
>
> (%o17) [[x = - (sqrt(- r2  + 4 err r2  + (2 r1  - 4 err r1 - 4 err  + 4)
r2
>                2        2                    4           3             2
2
>   + (- 4 err r1  + 8 err  r1 - 8 err) r2 - r1  + 4 err r1  + (4 - 4 err )
r1
>                     2          2                2
>   - 8 err r1 + 8 err  - 4) + r2  - 2 err r2 - r1  + 2 err r1 - 2)/4,
>
>                2                          2                   2
> y = (sqrt(- r2  - 2 r1 r2 + 4 err r2 - r1  + 4 err r1 - 4 err  + 2)
>          2               2          2                2
>   sqrt(r2  - 2 r1 r2 + r1  - 2) - r2  + 2 err r2 + r1  - 2 err r1 + 2)/4],
>   
>                 4           3        2                   2        2
> [x = (sqrt(- r2  + 4 err r2  + (2 r1  - 4 err r1 - 4 err  + 4) r2
>                2        2                    4           3             2
2
>   + (- 4 err r1  + 8 err  r1 - 8 err) r2 - r1  + 4 err r1  + (4 - 4 err )
r1
>                     2          2                2
>   - 8 err r1 + 8 err  - 4) - r2  + 2 err r2 + r1  - 2 err r1 + 2)/4,
>
>                  2                          2                   2
> y = - (sqrt(- r2  - 2 r1 r2 + 4 err r2 - r1  + 4 err r1 - 4 err  + 2)
>          2               2          2                2
>   sqrt(r2  - 2 r1 r2 + r1  - 2) + r2  - 2 err r2 - r1  + 2 err r1 - 2)/4]]
>
>
>
> If we set err = 0, the equations to be solved are identical to the
original
> equations. However, setting err = 0 in the solutions does not give the
same
> results as before. The x-solutions are the same, but the y-solutions have
> product terms r1*r2 which have a different sign from the solutions of the
> original equations. The solutions to the problem are incorrect.
>
>
> Krishna
>
>
>
>

Victor,

I reproduce your maxima output (Maxima 5.24), but the two solutions, 
sola and solb with err=0, are not the same. This can be seen 
algebraically and numerically.

(%i16) ev(sola[1],[r1=1,r2=1]);

(%o16) [x = 0,y = 1]
(%i17) ev(solb[1],[r1=1,r2=1,err=0]);

(%o17) [x = 0,y = 0]


The x-solutions are the same, but the y-solutions are not. If you print 
the solutions, the comparison is not

sqrt(A-B)*sqrt(C-D) vs. sqrt(B-A)*sqrt(D-C)

but

sqrt(A-B)*sqrt(C-D) vs. sqrt(A+B)*sqrt(C+D)


(%i18) sola[1];

(%o18) [x = -(sqrt(-r2^4+(2*r1^2+4)*r2^2-r1^4+4*r1^2-4)+r2^2-r1^2-2)/4,
         y =
(sqrt(-r2^2+2*r1*r2-r1^2+2)*sqrt(r2^2+2*r1*r2+r1^2-2)-r2^2+r1^2+2)
           /4]

(%i20) ev(solb[1],err=0);

(%o20) [x = -(sqrt(-r2^4+(2*r1^2+4)*r2^2-r1^4+4*r1^2-4)+r2^2-r1^2-2)/4,
         y =
(sqrt(-r2^2-2*r1*r2-r1^2+2)*sqrt(r2^2-2*r1*r2+r1^2-2)-r2^2+r1^2+2)
           /4]




Regards,
Krishna

_______________________________________________
Maxima mailing list
Maxima at math.utexas.edu
http://www.math.utexas.edu/mailman/listinfo/maxima