You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Thomas Neidhart <th...@gmail.com> on 2013/05/21 17:01:06 UTC

[math] NewtonRaphsonSolver

Hi,

based on the question on the user-ml, I did an experiment with the
NewtonRaphson solver, and I get some strange results, see below:

    @Test
    public void testSquare() {

        final UnivariateDifferentiableFunction d = new
UnivariateDifferentiableFunction() {

            public double value(double x) {
                return x*x;
            }

            public DerivativeStructure value(DerivativeStructure t)
                throws DimensionMismatchException {
                return t.multiply(t);
            }
        };

        NewtonRaphsonSolver solver = new NewtonRaphsonSolver();
        UnivariateDifferentiableFunction sin = new Sin();
        double result = solver.solve(1000, sin, -4, 1);
        System.out.println(result + " " + sin.value(result)); // prints
12.566370614359172 -4.898587196589413E-16
                                                              // but there
should be a root at -PI and 0

        result = solver.solve(1000, d, -4, 1);
        System.out.println(result + " " + d.value(result));   // prints
-7.152557373046875E-7 5.115907697472721E-13

        result = solver.solve(1000, d, -1, 1);
        System.out.println(result + " " + d.value(result));   //
TooManyEvaluationsException
    }

For the case of sin, I expected to get either -PI or 0 as root.
For x^2, the result depends also on the bounds. Is there something wrong
with my test, or is the solver broken?

Thomas

Re: [math] NewtonRaphsonSolver

Posted by Luc Maisonobe <Lu...@free.fr>.
Le 03/06/2013 13:13, Thomas Neidhart a écrit :
> Any feedback on this?
> 
> I am really curious if there is a misunderstanding from my side or if there
> is a bug.
> 
> Thomas
> 
> 
> On Tue, May 21, 2013 at 5:01 PM, Thomas Neidhart
> <th...@gmail.com>wrote:
> 
>> Hi,
>>
>> based on the question on the user-ml, I did an experiment with the
>> NewtonRaphson solver, and I get some strange results, see below:
>>
>>     @Test
>>     public void testSquare() {
>>
>>         final UnivariateDifferentiableFunction d = new
>> UnivariateDifferentiableFunction() {
>>
>>             public double value(double x) {
>>                 return x*x;
>>             }
>>
>>             public DerivativeStructure value(DerivativeStructure t)
>>                 throws DimensionMismatchException {
>>                 return t.multiply(t);
>>             }
>>         };
>>
>>         NewtonRaphsonSolver solver = new NewtonRaphsonSolver();
>>         UnivariateDifferentiableFunction sin = new Sin();
>>         double result = solver.solve(1000, sin, -4, 1);
>>         System.out.println(result + " " + sin.value(result)); // prints
>> 12.566370614359172 -4.898587196589413E-16
>>                                                               // but there
>> should be a root at -PI and 0

This one is normal, it is a classical problem with Newton-Raphson.
The algortihm starts computing f(x0) for x0 at the center of the search
interval, i.e. at x0 = -1.5 :
  f (-1.5) = -0.9974949866040544
  f'(-1.5) =  0.0707372016677029

Sonce f' is close to 0, the next point is way out of the search
interval: x1 = 12.60141994717172. From this point, it converges to a
close root which is 4 PI.

The algorithm has no real notion of search interval or bracketing, it is
a one point algorithm that wanders around following the tangent.

>>
>>         result = solver.solve(1000, d, -4, 1);
>>         System.out.println(result + " " + d.value(result));   // prints
>> -7.152557373046875E-7 5.115907697472721E-13
>>
>>         result = solver.solve(1000, d, -1, 1);
>>         System.out.println(result + " " + d.value(result));   //
>> TooManyEvaluationsException
>>     }

For x^2, the first point is x0 = 0 which is the root but unfortunately
also a root of the derivative (f(0) = f'(0) = 0), so there is a division
by zero and hence NaN appears.

>>
>> For the case of sin, I expected to get either -PI or 0 as root.
>> For x^2, the result depends also on the bounds. Is there something wrong
>> with my test, or is the solver broken?

No, the algorithm itself is not robust.

Luc

>>
>> Thomas
>>
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Re: [math] NewtonRaphsonSolver

Posted by Thomas Neidhart <th...@gmail.com>.
Any feedback on this?

I am really curious if there is a misunderstanding from my side or if there
is a bug.

Thomas


On Tue, May 21, 2013 at 5:01 PM, Thomas Neidhart
<th...@gmail.com>wrote:

> Hi,
>
> based on the question on the user-ml, I did an experiment with the
> NewtonRaphson solver, and I get some strange results, see below:
>
>     @Test
>     public void testSquare() {
>
>         final UnivariateDifferentiableFunction d = new
> UnivariateDifferentiableFunction() {
>
>             public double value(double x) {
>                 return x*x;
>             }
>
>             public DerivativeStructure value(DerivativeStructure t)
>                 throws DimensionMismatchException {
>                 return t.multiply(t);
>             }
>         };
>
>         NewtonRaphsonSolver solver = new NewtonRaphsonSolver();
>         UnivariateDifferentiableFunction sin = new Sin();
>         double result = solver.solve(1000, sin, -4, 1);
>         System.out.println(result + " " + sin.value(result)); // prints
> 12.566370614359172 -4.898587196589413E-16
>                                                               // but there
> should be a root at -PI and 0
>
>         result = solver.solve(1000, d, -4, 1);
>         System.out.println(result + " " + d.value(result));   // prints
> -7.152557373046875E-7 5.115907697472721E-13
>
>         result = solver.solve(1000, d, -1, 1);
>         System.out.println(result + " " + d.value(result));   //
> TooManyEvaluationsException
>     }
>
> For the case of sin, I expected to get either -PI or 0 as root.
> For x^2, the result depends also on the bounds. Is there something wrong
> with my test, or is the solver broken?
>
> Thomas
>