Problem description given three times function f (x) =ax3+bx2+cx+d 4 coefficients a,b,c,d, and a number z, use Newton iterative method to find the root of function f (x) =0 near Z, and give the number of iterations required.
The principle of Newton's iterative method is as follows (reference):
The XK is a guessing solution near the exact solution of the equation F (x) =0 x*, and the point of PK (Xk,f (XK)) is the tangent of f (x). The tangent and the intersection of the x-axis are closer to the exact solution x* of the equation than the XK.
The iteration formula is: xk+1= xk-f (XK)/F ' (XK), which ends the iteration when the absolute value of f (x) is small enough.
Note: For the subject given function f (x), F ' (x) =3ax2+2bx+c, and when |f (x) | ≤10-7, you can assume that x is the root of f (x) =0.
Input format input total 2 lines.
The first behavior is 4 integers, separated by a space for every 2 numbers, respectively, a,b,c,d
The second behavior is a real number Z. Output format a total of one row. Contains 2 numbers, separated by a space, the first number is the real x, which represents the root of the order, accurate to 3 digits after the decimal point, and the second number is an integer n, which indicates the number of iterations required to obtain the above root. Sample Input 2-9 5 3
3 Sample Output 3.719 7 when you need to use a floating-point number in your program, use the double type "analyze" to record it.
/*The Song Dynasty su Shi "Riverside fairy • Night Drink Dongpo wake up drunk" night drink Dongpo wake up drunk, return as if shift. Positive nasal polyps were thundering. Knock on the door should not, leaning staff listen to the river sound. Long hate this body not I have, when to forget the camp. The night stop wind static grain grain flat. The boat is gone, and the river ships for the rest. */#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<vector>#include<iostream>#include<string>#include<ctime>#defineLOCALConst DoublePi = ACOs (-1.0);Const intMAXN =200000+Ten;Const DoubleEPS = 1e-7;using namespaceStd;typedefLong Longll;DoubleFintAintBintCintDDoublex) {returnA * x * x * x + b * x * x + c * x +D;}DoubleFdintAintBintCDoublex) {return 3* A * x * x +2* b * x +C;}intMain () {intA, B, C, D; DoubleZ; scanf ("%d%d%d%d", &a, &b, &c, &d); scanf ("%LF", &z); intCNT =1; Doublex =Z; while(f (A, B, C, D, x) > EPS | | f (A, B, C, D, X) <-EPS) {x= X-f (A, B, C, D, x)/FD (A, B, C, X); CNT++; } printf ("%.3LF%d\n", X, CNT); return 0;}
View Code
"Qing Orange A1094" "Newton Iterative Method" Newton iterative method to find the root of the equation