#include <cmath>
long double NR(long double sample)
{
return (abs(f(sample)))<=1e-10 ? sample: NR( (sample - f(sample)/fp(sample)));
}
long double f(long double sample)
{
return 2.5*exp(-sample)-3*sin(sample);
}
long double fp(long double sample)
{
return -2.5*exp(-sample)-3*cos(sample);
}
I'm trying to write code for a Newton-Raphson algorithm. I have it in recursive form but since it finds roots in estimations f(x) doesn't really equate to zero, so instead I want it to stop calculation at a certain precision.
Can anyone help me to find a way to do this? I wish to implement something more general so as to be able to handle any f(x), that is provision for if f(x) has an asymptote at 0. At the moment I have a quick fix to check if f(x) is sufficiently small so as to be considered a root.
In future I will implement code to handle polynomials, but at the moment the functions are hardcoded. But that is another story.
P.S.
If I let it go and allow it to calculate until "f(x) == 0" forcefully overstack flow error occurs.