class-projects

Computer Science Coursework

View the Project on GitHub

Table of Contents

Newton’s Method

Routine Name: newtons_method

Author: Philip Nelson

Language: C++

Description

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

Input

newtons_method(T x0, T y0, T x, T dt, F f) requires:

Output

The zero of the function at x.

Code

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