Newton's Method

Hi,
I have a written the code for Newton's method by using the Pseudocode from my professor. The problem is I can't see why it doesn't provide the output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 #include<iostream>
#include<cmath>
#include<cfloat>
#include<cstdint>
#include <vector>
using namespace std;

double f(double x)
{
    return x*x*x - x*x + 2;
}
double df(double x)
{
    return 3*x*x - 2*x;
}

double Newton(double f (double x_i), double df(double x_i), double x_i, double epsilon, int intmax_t)
{
    epsilon= DBL_EPSILON;
    int n=0;
    double r = 0.0;
    int i=0;
    do
    {
     double x=x_i;
     double d= df(x);
     if(d==0)
    {
        cout << "Problem is ill defined!";
        return 1;
    }
        x=x_i- f(x_i)/d;
        if(abs(x-x_i)<=epsilon)
        {
            cout << "Success!";
            r=x;
            return r;
        }
        if(i==intmax_t)
        {
            cout << "Too many iterations!";
            return 1;
        }
        i++;
        x=x_i;
        d=df(x_i);
        x=x_i- f(x_i)/d;
        r=x;
    }  while(i<n);
    cout << "The value of the root is= " << r;
    return r;
}
        

// Driver program to test above
int main()
    {
        int n=0;
        double epsilon= DBL_EPSILON;
        int intmax_t=0;
        for (int i=0; i<n; i++)
    {
        double x_i;
        x_i = -20;
        Newton(f, df, x_i, epsilon, intmax_t);
    }
        return 0;
    }
Hi,

Where do you change the value of n , in the Newton function?

At the moment you have the equivalent of: do {/*.. */} while i <0

In the main function, nothing happens for the same reason on line 61.
Last edited on
I see your point and you're totally right. I guess I want to define n as the maximum number of the iterations that Newton's method can take. I didn't know how to do that, so I just included n as an integer. n=0 is because my program said it should be initialized, which is the same reason for r=0.0.
So you need to initialise variables to sensible values. :+)

And be careful with unnecessary redefining with new values.

Check out using a debugger, one can see what values are and how they change as one executes the program 1 line at a time. There should be one in your IDE.
But how can debugging omit the use of initialised values?

Does there exist a counter for sequence iterations? I tried to do this with intmax_t, but that would be the maximum of the width of the data type, not the iterations, even though n is actually an integer.
debugging just shows you what is happening. it does not omit anything.

i is the number of iterations. you can look at i in your debugger.
you can also change the code to limit it. Newtons will converge on an answer that is good enough to test with in just a few iterations if you put in a decent guess; Sir Issac didn't run 20k iterations by hand.. pass in like 10 or 20 for the number of iterations and see what pops out.

do you know what the roots are? Start by finding them by hand. Then plug in something near them. Then test it that way with a few iterations.
Topic archived. No new replies allowed.