### Newton Raphson question

Hello, I am trying to write a program that gives me a root of the function using Newton Rhapson method. Currently I am trying to write it in the int main() function to make it work. Afterwards I want to transfer the iteration to root function as thats what my assignment asks. A problem is this : write equation f(x) = 0 solution program. Show program's success by finding the root of function f(x) = 1 - x^2. The program should be made of three functions int main(), void rood(double& x, double eps) and double fk(double x). So, below is what I wrote. It doesn't seem to be a hard problem, but when I run the program I get the root to be equal to nan , which I know is a mistake. Can someone point me out to the right direction where I might be doing it wrongly trying to first write an iteration in function int main()? Thank you for all the help. All suggestions are welcome. Also, how I can find a derivative of a function without writing it as a different function? Is it possible?

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` ``````#include #include #include using namespace std; void root(double& x, double eps); double fk(double x); double fk_p(double x); int main() { // Data: accuracy eps and some x double x, x_1, eps; printf("x_1 = "); scanf("%lf", &x_1); printf("eps = "); scanf("%lf", &eps); do { x = x_1; x_1 = x - (fk(x) / fk_p(x)); printf("%lf \n", x_1); } while (abs(x_1 - x) >= eps ); printf("Exact root is: %lf\n", x_1); //root(x, eps); // Result: x } void root(double& x, double eps) { //Iteration double f = fk(x); // Function } double fk(double x) { return 1 - x * x; } double fk_p(double x) { return 2 * x; }``````
Last edited on
I changed my function and it works now, but I am curious to know how to write derivatives in C++ without calling a function? Is it possible in my case?

Code:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include #include #include using namespace std; void root(double& x, double eps); double fk(double x); double fk_p(double x); int main() { // Data: accuracy eps and some x double x, eps; printf("x = "); scanf("%lf", &x); printf("eps = "); scanf("%lf", &eps); root(x, eps); printf("Exact root is: %lf\n", x); } void root(double& x, double eps) { // Iteration double x_1; do { x_1 = x; x = x_1 - fk(x_1) / fk_p(x_1); printf("%lf \t %lf \n", x, eps); } while (abs(x - x_1) >= eps ); } double fk(double x) { return (1.0 - x * x); } double fk_p(double x) { return (- 2.0 * x); }``````
Last edited on
You can get an estimate of the derivative from the actual function itself by finite differences (relating directly to the definition of a derivative):
 `df/dx ~ ( f(x+dx) - f(x-dx) ) / ( 2 * dx )`

where dx is "small".

In code, replace your current derivative function by, e.g.
 ``12345`` ``````double fk_p(double x) { const double SMALL = 1.0e-10; return ( fk( x + SMALL ) - fk( x - SMALL ) ) / ( 2.0 * SMALL ); }``````
Last edited on
Thanks, lastchance. However, I would like to keep fk(x) function and remove fk_p() which basically gives the derivative of fk(x). I want the derivative to be calculated in root() function without fk_p(x) function available or used. Any ideas?
if you know the function you can just do it on paper and code the dx.

for example if you have ax*x + bx + c
you can code in
2*a*x + b yourself. This is probably the best way when the function is known.

 I would like to keep fk(x) function and remove fk_p() which basically gives the derivative of fk(x). I want the derivative to be calculated in root() function without fk_p(x) function available or used. Any ideas?

So, in root(), where you update x, just put
 ``123`` `````` const double SMALL = 1.0e-10; double derivative = ( fk( x + SMALL ) - fk( x - SMALL ) ) / ( 2.0 * SMALL ); x = x_1 - fk(x_1) / derivative;``````

Then you don't need function fk_p() at all and you only have to change fk() if the function changes.
Last edited on
Topic archived. No new replies allowed.