In numerical analysisNewton'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.

{\displaystyle x:f(x)=0\,.}x:f(x)=0\,.

The Newton–Raphson method in one variable is implemented as follows:

The method starts with a function f defined over the real numbers x, the function's derivative f ′, and an initial guess x0 for a root of the function f. If the function satisfies the assumptions made in the derivation of the formula and the initial guess is close, then a better approximation x1 is

{\displaystyle x_{1}=x_{0}-{\frac {f(x_{0})}{f'(x_{0})}}\,.}x_{1}=x_{0}-{\frac {f(x_{0})}{f'(x_{0})}}\,.

Geometrically, (x1, 0) is the intersection of the x-axis and the tangent of the graph of f at (x0f (x0)).

The process is repeated as

{\displaystyle x_{n+1}=x_{n}-{\frac {f(x_{n})}{f'(x_{n})}}\,}x_{n+1}=x_{n}-{\frac {f(x_{n})}{f'(x_{n})}}\,

until a sufficiently accurate value is reached.

具体实现过程如下:

#include <iostream>
#include<cmath>
using std:: cin;
using std::cout;
using std::endl;
#define EPSILON 1e-6

double  f (double x)
{
 return 2*pow(x,3)+4*pow(x,2)+3*x-6;
}
double f_prime(double x)
{
  return 6*pow(x,2)-8*x+3;
}
 double new (double(*f)(double),double(*f_frime)(double))
{
   double x=1.5;
    while(fabs((*f)(x))>EPSILON)
   {
      x=x-(*f)(x)/(*f_prime(x));
    }
   return x;
}

int main()
{
  cout<<newton(f,f_prime)<<endl;
   returen 0;
}

  

posted on 2018-03-27 11:01  未完代码  阅读(641)  评论(0编辑  收藏  举报