4.24 1.6

发布时间 2023-04-24 18:59:48作者: 席

一、问题描述

 二、分析


(1)在1附近找任一实数作为x0的初值x0=1.5
(2)用初值x0代入方程中计算此时的f(x0)及导。程序中用变量f描述方程的值,用fd描述方程求导之后的值
(3)计算增量 h=ffd
(4) 计算下一个x,x=x0-h.
(5)用新产生的x替换原来的x0
(6)如果|x-xo|>=le-5,,则转到第(3)步,否则转到(7)
(7)所求就是方程的解

三、代码

#include<iostream>
using namespace std;

int main ()
{
float solution (float a, float b, float , float d) ;
float a,b,c,d,x;

cout<<"请输入方程的系数:";
cin>>a>>b>>c>>d;

x=solution (a, b, c, d) ;
cout<<"所求方程的根为 x="<<x;
}
float solution (float a, float b, float c, float d)
{
float x0,x=1.5,f,fd,h;
do
{
x0=x;

f=a*x0*x0*x0+b*x0*x0+c*x0+d;
fd=3*a*x0*x0+2*b*x0+c;
h=f/fd;
x=x0-h;
}while(fabs(x-x0)>=1e-5);
return x;
}

四、结果