-1

I was trying to figure out the Newton's method to find the root of equation. And this bug came out and I couldn't handle it.

double fn(double n){
    return sin(n)+log(n)-1;
}

double f1n(double n){
    return cos(n)+1/n;
}

double operation(double n){
    n=n-fn/f1n;

    while(fn>0.000001){
        n=n-fn/f1n;
    }

    return n;
}
Joehl
  • 3,671
  • 3
  • 25
  • 53
Hao Xu
  • 7
  • 2
  • fn and f1n are functions. You are dividing one function by another which makes no sense. You probably want to invoke those functions using fn(n) and f1n(n) and use the answers – Mike Vine Sep 29 '15 at 13:36
  • ``n=n-fn/f1n;`` What do you try to achieve with dividing two memory addresses (the 2 functions)? – BitTickler Sep 29 '15 at 13:36
  • You *do* know how to call functions, don't you? Well obviously you do, as you do it in the code you show (like you call the `cos` or `sin` functions), why don't you do it with your own functions? – Some programmer dude Sep 29 '15 at 13:37
  • better to impose additional restrictions on the number of iterations, the algorithm will not always converge. –  Sep 29 '15 at 14:04

1 Answers1

1

You forgot to pass n to your functions.

double operation(double n)
{
    n = n - fn(n) / f1n(n);
    while(fn(n) > 0.000001)
    {
        n = n - fn(n) / f1n(n);
    }
    return n;
}
rozina
  • 4,120
  • 27
  • 49