I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
COST (GBP)
this unit 3.97
sub units 0.00
+
0

Newton

Calculates the zeros of a function using Newton's method.
Controller: CodeCogs

add cart

Interface

C++

Newton

 
doublenewtondouble(*f)(double)[function pointer]
double(*df)(double)[function pointer]
doublex = 0
doubleeps = 1E-10
intmaxit = 1000 )
This is a well-known iterative method for approximating the zeros of a function. Starting with a given initial approximation x_0, a sequence x_1, x_2, x_3, \ldots is computed where

This iterative process can be stopped when |h_n| has become less than the largest error one is willing to permit in the root. This method is only locally convergent and will converge to complex zeros only if the initial approximation is complex. When it does converge, the convergence is quadratic to roots that are simple.

To give you a better idea on the way this method works, the following graph shows different iterations in the approximation process.

1/newton-378.png
+
This algorithm finds the roots of the user-defined function f starting with an initial guess x and iterating the sequence above until either the accuracy eps is achieved or the maximum number of iterations maxit is exceeded. Also required is the derivative of the user-defined function df.

References:

  • Jean-Pierre Moreau's Home Page, http://perso.wanadoo.fr/jean-pierre.moreau/
  • F.R. Ruckdeschel, "BASIC Scientific Subroutines", Vol. II, BYTE/McGRAWW-HILL, 1981

Example 1

#include <codecogs/maths/rootfinding/newton.h>
 
#include <iostream>
#include <iomanip>
#include <cmath>
 
double f(double x) {
    return sin(x);
}
 
double df(double x) {
    return cos(x);
}
 
int main()  {
 
  double x = Maths::RootFinding::newton(f, df, 3);
 
  std::cout << "The calculated zero is X = " << std::setprecision(15) << x << std::endl;
  std::cout << "The associated ordinate value is Y = " << f(x) << std::endl;
  return 0;
}
Output:
The calculated zero is X = 3.14159265358979
The associated ordinate value is Y = 1.22460635382238e-016

Parameters

fthe user-defined function
dfthe derivative of f
xDefault value = 0
epsDefault value = 1E-10
maxitDefault value = 1000

Authors

Lucian Bentea (August 2005)
Source Code

Source code is available when you buy a Commercial licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.