Algorithms and Subroutines for Solving Differential Equations
Routine Name: newtons_method
Author: Philip Nelson
Language: C++
newtons_method
, In numerical analysis, Newton’s method (also known as the Newton–Raphson method), named after Isaac Newton and Joseph Raphson, is a method for finding successively better approximations to the roots (or zeroes) of a real-valued function. It is one example of a root-finding algorithm. 1
newtons_method(T x0, T y0, T x, T dt, F f)
requires:
F f
- the functionT dt
- the timestepT x0
- the initial guessconst uint MAX_ITERATONS
- the number of iterationsThe zero of the function at x
.
template <typename T, typename F>
T newtons_method(F f, T dt, T x0, const unsigned int MAX_ITERATONS = 100)
{
auto tol = maceps<T>().maceps, i = 0u;
while (std::abs(f(x0) - 0) > tol && ++i < MAX_ITERATONS)
{
x0 = x0 - f(x0) / ((f(x0 + dt) - f(x0)) / dt);
}
return x0;
}
Last Modification date: 3 April 2018