求高手帮我看看程序.是求一元方程的根.用牛顿法和二分法的!有哪些错误#include#include#include#includeint n,i,k;int a[50];double function(double x);double dfunction(double x);double diedai(double x0,int *n,double jingdu);dou
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/20 03:38:08
求高手帮我看看程序.是求一元方程的根.用牛顿法和二分法的!有哪些错误#include#include#include#includeint n,i,k;int a[50];double function(double x);double dfunction(double x);double diedai(double x0,int *n,double jingdu);dou
求高手帮我看看程序.是求一元方程的根.用牛顿法和二分法的!有哪些错误
#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;
}
求高手帮我看看程序.是求一元方程的根.用牛顿法和二分法的!有哪些错误#include#include#include#includeint n,i,k;int a[50];double function(double x);double dfunction(double x);double diedai(double x0,int *n,double jingdu);dou
牛顿第一运动定律,又称惯性定律,它科学地阐明了力和惯性这两个物理概念,正确地解释了力和运动状态的关系,并提出了一切物体都具有保持其运动状态不变的属性——惯性,它是物理学中一条基本定律.