| princesslumy (36) | |
|
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
|
|
| chipp (506) | |
| we're not doin your homework :D | |
|
|
|
| princesslumy (36) | |
|
lol Chipp!! Is not homework, but project which i have no idea what to do | |
|
|
|
| mik2718 (295) | |
|
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. | |
|
|
|
| Awais Tahir (17) | |
| Is this Include in Your Numerical Computing course.?? | |
|
|
|
| fun2code (1063) | |
|
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 ). | |
|
|
|
| Chervil (812) | ||
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. | ||
|
|
||
| princesslumy (36) | |||
|
I did my best but if is having few errors which am not sure it is right any ideas.
| |||
|
|
|||
| fun2code (1063) | ||||||
|
That is quite a train wreck! Where to start? Line 8:
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:
Now the data is collected. Your f(x) function is bad.
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
|
||||||
| princesslumy (36) | |||
| |||
|
|
|||
| Chervil (812) | |
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.
| |
|
|
|
| princesslumy (36) | |||
how to solve in the situation it compiles but doesn't give the right solution for x
| |||
|
|
|||