作业帮 > 综合 > 作业

求高手帮我看看程序.是求一元方程的根.用牛顿法和二分法的!有哪些错误

来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/09/21 00:28:27
求高手帮我看看程序.是求一元方程的根.用牛顿法和二分法的!有哪些错误
#include
#include
#include
#include
int n,i,k;
int a[50];
double function(double x);
double dfunction(double x);
double diedai(double x0,int *n,double jingdu);
double erfen(double x1,double x2);
int main()
{
n=4;
k=1;
a[0]=0,a[1]=0,a[2]=-2,a[3]=1;
double x=1,x1,x2;
double jingdu=1e-6;
printf("利用二分法求一元方程的根\n");
scanf("%lf %lf",&x1,&x2);
printf("根的区间为:[%lf ,%lf]\n",x1,x2);
if(erfen(x1,x2))
printf("方程的根为:%.3lf\n",erfen(x1,x2));
printf("利用牛顿迭代法求一元方程的根\n");
x=diedai(x,&n,jingdu);
printf("x=%.7lf\ty=%.7lf\n",x,function(x));
getch();//截取程序,使其停留
return 0;
}
/*
牛顿迭代法解方程组的解
x0为迭代的初值,n为迭代次数,jingdu为精度
function为求根代数式,dfunctoin为其导数
返回最终符合一定精度的根
*/
double diedai(double x0,int *n,double jingdu)
{
double x,temp;
temp=dfunction(x0);
if(fabs(temp)>1e-10)
{
x=x0-function(x0)/temp;
printf("n=%d\tx=%.5lf\n",*n,x);
}
else
{
printf("error:div 0:\nPress any key to exit:");
getch();
exit(1);//判断结尾
}
temp=function(x);
if (fabs(temp)0)
{
printf("区间内无解!\n");
return 0;
}
do
{
x=(x1+x2)/2;
f0=function(x);
if(function(x)==0)
break;
if(f1*f0>0)
{
x1=x;
f1=function(x1);
}
else
x2=x;
}while(fabs(f0)>=1e-10);
return x;
}
求高手帮我看看程序.是求一元方程的根.用牛顿法和二分法的!有哪些错误
牛顿第一运动定律,又称惯性定律,它科学地阐明了力和惯性这两个物理概念,正确地解释了力和运动状态的关系,并提出了一切物体都具有保持其运动状态不变的属性——惯性,它是物理学中一条基本定律.