C language programming - content: Newton iterative method to find the root of a cubic equation. Requirements: by the main function call root sub function, thank you

C language programming - content: Newton iterative method to find the root of a cubic equation. Requirements: by the main function call root sub function, thank you


Newton iterative method
Newton iterative method, also known as Newton tangent method, uses the following methods to find the root: first, arbitrarily set a value x0 close to the real root as the first approximate root, then find out f (x0) from x0, cross (x0, f (x0)) point to make the tangent of F (x), cross X axis to x1, take it as the second approximate root, then find out f (x1) from x1, then cross (x1, f (x1)) point to make the tangent of F (x), cross X axis to X2, then find out f (x2), and then make the tangent This continues until it is close enough to the real X
Where f '(x0) is the slope of the function at x0, that is, the derivative at x0
The code is as follows:
#include
#include
float f(float a,float b,float c,float d,float x)
{
float f;
f=((a*x+b)*x+c)*x+d;
return f;
}
float f1(float a,float b,float c,float x)
{
float f;
f=(x*3*a+2*b)*x+c;
return f;
}
float root(float a,float b,float c,float d)
{
float x0,x1=1;
do
{
x0=x1;
x1=x0-f(a,b,c,d,x0)/f1(a,b,c,x0);
}while(fabs(x1-x0)>=1e-6);
return x0;
}
void main()
{
float a,b,c,d,x;
printf("input four float numbers:\n");
scanf("%f%f%f%f",&a,&b,&c,&d);
x=root(a,b,c,d);
printf("%.1fX^3+%.1fX^2+%.1fX+%.1f=0 its root near x=1.5 is :%.4f\n",a,b,c,d,x);
getch();
}



C + + Programming: Newton iterative method for solving approximate roots of equations
Newton iteration method is used to find the root of the equation f (x) = x * x-3 * x-e (x power) + 2 = 0 near x = 0; the error is not more than 0.5 * 10 (- 5 power)
Master help!


It's very simple. You can write it yourself and give you the following tips:
Add the following to the header file:
#include
Function:
f(x) = x*x - 3.0 * x - exp(x) + 2.0;
First derivative:
f2(x) = 2.0 * x - 3.0 -exp(x);
Iterative formula:
x1 = x0 - f(x0) / f2(x0);
Initial value:
x0 = 0.0;
The convergence conditions are as follows
If (Fabs (x1-x0) < 0.5e-05) {success;}
else {
x0 = x1;
Go back and iterate again
}



Newton iteration method is used to find the real roots of the equation f (x) = x ^ 6-x-1 = 0 in the interval [1,2], and f (x (k)) is required


#include
#include
#define eps 1e-8
void main()
{
double a=1,b=2;
double t,t0,f0,f00,m,n;
t0=(a+b)/2;
m=pow(t0,5);
n=pow(t0,4);
f0=6*m-1;
f00=30*n;
t=t0-f0/f00;
while(fabs(t-t0)>eps)
{
t0=t;
m=pow(t0,5);
 n=pow(t0,4);
f0=6*m-1;
 f00=30*n;
 t=t0-f0/f00;
printf("t0=%12.10lf,t=%12.10lf\n",t0,t);
}
printf (:% 12.10lf, n ", t) obtained by Newton tangent method;
}
The results were as follows
t0=1.2065843621,t=0.9809945654
t0=0.9809945654,t=0.8207881793
t0=0.8207881793,t=0.7300742137
t0=0.7300742137,t=0.7013898132
t0=0.7013898132,t=0.6988457773
t0=0.6988457773,t=0.6988271198
t0=0.6988271198,t=0.6988271188
Using Newton tangent method: 0.6988271188
Press any key to continue