<> principle
Reference link Newton method —— Know . For example, I want to find a function f ( x ) = 0 f(x)=0 f(x)=0 Solution of , Using Newton iterative method, it can be constructed as follows :
x n + 1 = x n − f ( x n ) f ′ ( x n ) x_{n+1}=x_n-\frac{f(x_n)}{f'(x_n)} xn+1
=xn−f′(xn)f(xn)
<> code
Below C Language implementation code
#include <math.h> #include <stdio.h> #include <stdlib.h> #define N 200 // Maximum number of iterations
#define EPS 0.000001 // Convergence error double newton(double (*fun)(double), double (*
partialfun)(double), double); // Newton iterative method , Includes two function pointers double fun(double x); // Primitive function
double partialfun(double x); // Derivative function int main(int argc, char* argv[]) { double x0,
root; x0 = 1; root = newton(fun, partialfun, x0); printf(" initial values :%lf\n root :%lf\n",
x0, root); return 0; } double fun(double x) // Primitive function { return cos(x) - x * x * x; }
double partialfun(double x) // Derivative of original function { return -sin(x) - 3 * x * x; } double
newton(double (*fun)(double), double (*partialfun)(double), double xn) { double
xn1; for (int i = 0; i < N; i++) { xn1 = -(*fun)(xn) / (*partialfun)(xn) + xn;
if (fabs(xn1 - xn) < EPS) { printf(" Number of iterations :%d\n", i+1); return xn1; } xn = xn1; }
printf(" Iterative divergence !"); exit(0); }
<> Operation results
Technology