Newton's solve equation

How to create A program that has algorithm that can be used to solve an equation of form f(x) = 0 using a sequence of approximation
eg. To get the next term in the sequence,
x1= x0 -f(x0)/ f'(x0)
In general the ( i+1)st approximation is given by
xi+1 = xi - f(xi)/ f'(xi)

Store the coefficients of the polynomial in an array
Last edited on
we're not doin your homework :D
lol Chipp!!
Is not homework, but project which i have no idea what to do
Start by programming the functions

double f(double x) and double df(double x)

these will be hard coded functions so you have to decide what you want to solve.

you will need to find the derivative function by hand (i.e. algebraically)

when you have these it should be easy to write the iteration.
Is this Include in Your Numerical Computing course.??
Sorry if I'm clueless but, store the coefficients for what polynomial?
The xi are just intermediate results, they would not be stored. Is f(x) being given a polynomial representation?

If so, then it's easy to write the functions for f(x) and for df/dx(x).
I assume that the iteration to find a root continues until some given precision is reached ( f(xi) close enough to 0 ).
Store the coefficients of the polynomial in an array

This is good. Since the function is a polynomial, the rules for obtaining the derivative are straightforward. So much so that you could get your program to find the coefficients for f' rather than do it yourself.

Then, the usual approach is to input an x value close to the root, and all being well the program will find successive approximations which are closer and closer to the root. But choose a value too far away and the approximations may move in a different direction. It can be helpful to sketch a graph of the function, to give an idea of where the roots are. If you want a fun challenge you could program the graph-plotting too. Or maybe use a spreadsheet to do it for you.
I did my best
but if is having few errors which am not sure it is right any ideas.

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
#include<iostream>
#include<cstdlib>
#include<cmath>

using namespace std;

double f(double[], double);
double f'(double[], double);

int main()
{
    cout << " Enter the highest degree of your polynomial for x. " << endl;
    double degree;
    cin >> degree;
    
    double num[degree];        // creates an array of length of degree
    
    double i=1;
    while (i <= degree)     // if user wish to enter 3 polynomial, creat an array of size 4 to store
   {                        // eg. 2-5x^2 + 4x^3    represented in array as { 2, 0, -5, 4} "0" since x^0 = 1
        int num;
        cout << "Enter the coefficients, power of x-values and it's size." << i << ": " ;
        cin >> num[i-1];
        i++;
    }
    
   cout << " For f(x)= 0 in  " << degree << " the solution is x= " << f(num, degree) << endl;
   
   system ("Pause");
   return 0;
}

 double f( double a[], int x)
 
 {
        int i = 0;
        int f = 0;
        while ( i < f)
        {
            f+= a[i];
            i++;
        }
        return (f);
 }
 
 double f'(double a[], int x)
 
 {
        int i = 0;
        int f' = 0;
        while ( i < f')
        {
            f' += a[i];
            i++;
        }
        return ( f');
 }
 
 
That is quite a train wreck! Where to start?
Line 8:
double f'(double[], double);

You can't use a ' in a function name. Try dfdx or something.

Line 16:double num[degree];. Not legal with degree variable. If your compiler will accept it (some do) and they are teaching it in school, OK then. I think you want array size = degree+1.

Lines 18-25:
1
2
3
4
5
6
7
8
double i=0;// start at 0
    while (i <= degree)     // if user wish to enter 3 polynomial, creat an array of size 4 to store
   {                        // eg. 2-5x^2 + 4x^3    represented in array as { 2, 0, -5, 4} "0" since x^0 = 1
 //       int num; this hides the actual array
        cout << "Enter coefficient " << i << ": " ;
        cin >> num[i];
        i++;
    }


Now the data is collected. Your f(x) function is bad.
1
2
3
4
5
6
7
8
9
10
11
12
double f( double a[], int x)
 
 {
        int i = 0;
        int f = 0;
        while ( i < f)
        {
            f+= a[i];
            i++;
        }
        return (f);
 }

This will return 0 every time. Do you see why?

If you can get this to the point where f(x) is being evaluated correctly then I will help some more.
Good luck.

EDIT: Hint for f(x):
It will need 3 arguments: the array, the size of the array and the value of x (a double)
ie:
double f(double* a, int Nterms, double x);// where Nterms = degree+1
Last edited on
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
Biggest problem so far. my program says "Missing terminating character" 
I don't know what is that supposed to mean?


#include<iostream>
#include<cstdlib>
#include<cmath>

using namespace std;

void f(double[], int);
void df/dx(double[], int);

int main()
{
    cout << " Enter the highest degree of your polynomial for x. " << endl;
    double degree;
    cin >> degree;
    
    double num[degree];        // creates an array of length of degree
    
    double i=0;
    while (i <= degree)     // if user wish to enter 3 polynomial, creat an array of size 4 to store
   {                        // eg. 2-5x^2 + 4x^3    represented in array as { 2, 0, -5, 4} "0" since x^0 = 1
 
        cout << "Enter coefficient " << i << ": " ;
        cin >> i;
        i++;
    }
    
   cout << " For f(x)= 0 in  " << degree << " the solution is x= " << f(num, degree) << endl;
   
   system ("Pause");
   return 0;
}

 void f( double a[], int x)
 
 {
        int i = 0;
        int f = 0;
        while ( i < f)
        {
            f+= a[i];
            i++;
        }
        return f;
 }
 
 void (df/dx)(( double a[], int x))
 
 {
        int i = 0;
        int f' = df/dx;
        while ( i < f')
        {
            f' += a[i];
            i++;
        }
        return ( f');
 }
 

 
void df/dx(double[], int);

The compiler sees you started a function declaration with void df, and then you tried to divide it by something before the declaration ended. You need to use alphabetic, numeric or underscore characters in the name.
how to solve in the situation it compiles but doesn't give the right solution for x

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<iostream>
#include<cstdlib>
#include<cmath>

using namespace std;

double f(double, double[],int);
double deriv(double,double[],int);

int main()
{
    cout<<"What is the degree of the polynomial equation of the form f(x)=0?";
    int d;
    cin>>d;
    
    double coef[d+1];
    int i=d;
    while(i<=d && i>=0)
    {
        if(i>0)
        {
            cout<<"Enter the coefficient of the x^"<<i<<" term: ";
            cin>>coef[i];
            i--;
        }
        if (i==0)
            {
                cout<<"Enter the constant term: ";
                cin>>coef[i];
                i--;
            }
    }
  
    cout<<"Enter a guess for the solution: ";
    double x0;
    cin>>x0;
    
    //cout<<"the derivative of f(x0)= "<< deriv(x0,coef,d+1)<<endl;
    //cout<<"the value of f(x0)= "<<f(x0, coef,d+1)<<endl;
    
    double x1=x0-f(x0, coef,d+1)/deriv(x0,coef,d+1);
    //cout<<x1-x0<<endl;
    

    int n=1, j=1;
    
    char t='y';
    while(t=='y')
    {        
        while(n<1000*j)
        {
            double temp=x1;
            x1=temp-f(temp, coef,d+1)/deriv(temp,coef,d+1);      
            n++;
            //cout<<n<<" "<<x1<<endl;
            if( abs(x1-temp)<0.001)  
            {
                cout<<"One solution is "<<x1<<endl;
                system("Pause");
                return 2;
            }         
        }
        cout<<"I have gone through "<<n<<" times of the process, and haven't found a solution."<<endl;
        cout<<"Do you want me go through some more? enter y for yes, anything else for no.";
        cin>>t;
        if (t!='y')
            cout<<"No solution has been found.";
        if(t=='y')
            j++;       
    }   
           
    cout<<endl;
   
    system("Pause");
    return 4;
}

double f(double x, double a[], int n)
{
       double sum=0;
       while(n>0)
       {
           sum+=a[n-1]*pow(x, n-1);
           n--;
       }
       return sum;                       
}

double deriv(double x, double a[], int n)
{
    double deriv=0;
    while(n>0)
    {
       deriv+=a[n-1]*(n-1)*pow(x,n-2);
       n--;
    }
    return deriv;
} 



Topic archived. No new replies allowed.