#include<iostream>#include<cmath>usingnamespace std;doublefunc(double x){return x * x * x * x -3* x * x * x +1.5* x * x -4.0;}doubledfunc(double x){return4* x * x * x -9* x * x +3* x ;}intNewtonmethod(double&x ,int maxcyc ,double precision ){double x0 ,x1 ;int i ;
x0 = x ;
i =0;while( i < maxcyc ){
i++;if(dfunc(x0)==0){
cout <<"迭代过程中导数为0 "<< endl;return0;}
x1 = x0 -func(x0)/dfunc(x0);if(abs( x1 - x0 )< precision &&abs(func( x1 ))< precision ){
x = x1 ;return1;}else{
x0 = x1 ;}}printf("迭代次数超过预期值!仍没有达到精度!\n");return0;}intmain(void){double x =2.0;double precision =1e-12;double result =Newtonmethod( x ,1000, precision );if( result )
cout <<"方程根为"<< x << endl;else
cout <<"迭代失败"<< endl;}
#include<iostream>#include<cmath>using namespace std;double func(double x){ return x * x * x * x - 3 * x * x * x + 1.5 * x * x - 4.0;}double dfunc(double x){ return 4 * x * x * x - 9 * x * x + 3 * x ;}int Newtonmethod( double &x