zoukankan      html  css  js  c++  java
  • C++函数式编程实现牛顿法

    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;
    }
    

      

  • 相关阅读:
    模块化
    ES6中的let
    ES6中的块级作用域
    Mobile 移动端
    H5离线缓存
    nginx 配置步骤
    虚拟路径的配置
    Apache和php的相关配置
    TCP/IP协议
    PHP中的文件操作
  • 原文地址:https://www.cnblogs.com/fuhang/p/8656337.html
Copyright © 2011-2022 走看看