举例:
求解如下问题
首先不要管这个0.0001什么意思,其实就是后面进行do~while循环判断的一个界值。
编写程序如下,注释都写了:
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
double a=1,b=-0.495,c=-0.198; //分别代表该一元多次方程的每项系数的值,分别是1,0.495,0.198
double f(double x)
{
double y;
y=a*x*x*x*x*x+b*x+c; //把原函数表示出来
return(y);
}
double f1(double x)
{
double y;
y=5*a*x*x*x*x+b; //把原函数一阶导数表示出来
return(y);
}
int main()
{
double x0=1.0,x1;
x1=1;
do
{
x0=x1;
x1=x0-f(x0)/f1(x0);
}
while(fabs(x1-x0)>0.0001);
printf("%f",x1);
}
最后输出:0.918151