//牛顿法求解方程根
#include<stdio.h>
#include<math.h>
float function1(float x);
float function2(float x);
int main()//主函数
{
float x0=4;
float x1;
float a,b;
do{
x0=x1;
a=function1(x0);
b=function2(x0);
x1=x0-a/b;
printf("%f,%f\n",x0,x1);
}while(abs(x1-x0)>=0.000001);//这里使用的是步长;
printf("方程的根为%le",x0);
printf("方程的根为%le",x1);
x1=-1.650609;
x0=function1(x1);
printf("x0 value is %f",x0);
return 0;
}
float function1(float x)//功能函数求值
{
float fx;
fx=1*pow(x,3)+2*pow(x,2)+3*x+4;//待求根的方程
return fx;
}
float function2(float x)//功能函数求导数
{
float f_x;
f_x=3*pow(x,2)+4*pow(x,2)+3;
return f_x;
}
求解方程根
最新推荐文章于 2024-09-07 11:53:47 发布