-1

I'm havinga Problem with a method, which takes a Polynom like f(x)=x²+1 and calculates possible zero points with the newton algorithm.
I have given requirements for specific variables so even if the naming is not good or a variable is not needed I have to use them :/
The Polynom I give my method as a parameter is a double-array: For f(x)=x²+1 it would be {1.0,0.0,1.0} so its constructed like 1.0*x^0 + 0.0*x^1+1.0*x^2

For my Code:
x0 is the start value for the newton algorithm and eps is for the accuracy of the calculation

I followed my given Instructions and got the following code working:

public static double newton(double[] a, double x0, double eps) { 
    double z;
    double xn; 
    double xa = x0; 
    double zaehler;
    double nenner;

    do {
        zaehler = horner(a, xa);
        nenner = horner(ableit(a), xa);
        if(nenner == 0) {
            return Double.POSITIVE_INFINITY;
        }
        xn = xa - (zaehler/nenner);
        xa = xn;
    } while((Math.abs(horner(a, xn))) >= eps);
    z = xn;
    return 0;
}

the method horner() calculates the y-Value of a given function for a given x-Value.

My Problem is if the Function doesn't has a zero-point like x²+1 and I start with x0=1 and eps=0.1 I get Infinity returned.
But If I start with x0=10 and eps=0.1for example I create an endless loop.

How can I deal with this or is this a general Problem with the Newton Algorithm?!
Is the only way to set a fixed maximum of Iterations? The Code is working for Polynoms that have at least one zero-point!

Robin Green
  • 29,408
  • 13
  • 94
  • 178
KilledByCheese
  • 570
  • 5
  • 23
  • 1
    You may count the times of loop, or track the result which expect they are running closer. – Geno Chen Dec 02 '18 at 07:56
  • Possible duplicate of [Infinite Loop : Determining and breaking out of Infinite loop](https://stackoverflow.com/questions/9738633/infinite-loop-determining-and-breaking-out-of-infinite-loop) – Robin Green Dec 02 '18 at 10:36
  • The variable names make your code quite difficult for non-German (?) speakers to read. – Sneftel Dec 02 '18 at 13:16
  • Always look at a graph of your function to try and gain some insight into it. I use Wolfram Alpha a lot for this kinds of questions. – President James K. Polk Dec 02 '18 at 20:34

1 Answers1

2

The Newton–Raphson method requires the existence of a real root x such that f(x)=0. The function you use x^2+1 has no real roots, so your algorithm will not work in this case (nor in others where there is no root).

Since x^2+1 >= 1 for all real x this implies horner(a, xn) >= 1, so the loop

while((Math.abs(horner(a, xn))) >= eps)

will not terminate for eps < 1.

Maybe before starting to iterate, you should check the existence of a zero. E.g. if the highest (according to the power of x) nonzero coefficient is odd then there will be a real zero.

Or extend your algorithm such that it previously tries to find some real aand b such that f(a)f(b) <= 0 (then between a and b there is a root).

Maksim
  • 491
  • 4
  • 8