Title Description
The root is obtained by Newton iterative method. The equation is ax3+bx2+cx+d=0. The value of the coefficient a,b,c,d is 1,2,3,4 once and is entered by the main function. Ask for an x in a real roots near 1. After finding the root, it is output by the main function. The result retains two decimal places.
Input
The value of the coefficient a,b,c,d
Output
X a real roots near the 1
Sample input
1 2 3 4
Sample output
-1.65
Tips
The main function is given below, and it is not required to include the following main function when committing
/* C code */
int main ()
{
Double Solut (double, double, double, double);
Double a,b,c,d;
scanf ("%lf%lf%lf%lf", &a,&b,&c,&d);
printf ("%.2f", Solut (A,b,c,d));;
return 0;
}
/* C + + code */
int main ()
{
Double Solut (double, double, double, double);
Double a,b,c,d;
cin>>a>>b>>c>>d;
Cout<<setiosflags (ios::fixed);
Cout<<setprecision (2);
Cout<<solut (a,b,c,d) <<endl;
return 0;
}
The code is as follows:
#include <iostream> #include <iomanip> #include <cmath>using namespace std;double solut (double A, Double b,double c,double D) { double x=1,x0,f,f1; Do { x0=x; F= ((a*x0+b) *x0+c) *x0+d; f1= (3*a*x0+2*b) *x0+c; X=X0-F/F1; } while (Fabs (x-x0) >=1e-2); return x;} int main () {double Solut (double, double, double, double), double a,b,c,d; cin>>a>>b>>c>>d; cout& Lt;<setiosflags (ios::fixed); Cout<<setprecision (2); Cout<<solut (a,b,c,d) <<endl; return 0;}
Operation Result:
Still some do not understand, not the code, but the problem of mathematics, high number Ah, I should take you how to do
"Newton's Iterative method for root" of OJ brush problem