### 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.??
closed account (D80DSL3A)
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.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` ``````#include #include #include 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'); } ``````
closed account (D80DSL3A)
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:
 ``12345678`` ``````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++; }``````

 ``123456789101112`` ``````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
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364`` ``````Biggest problem so far. my program says "Missing terminating character" I don't know what is that supposed to mean? #include #include #include 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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102`` ``````#include #include #include 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^"<>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)<>t; if (t!='y') cout<<"No solution has been found."; if(t=='y') j++; } cout<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.