在C语言中用二分法求解已知,但如何对非线形方程的二分法求根进行改进?
#include<stdio.h>
#include<math.h>
#include<malloc.h>
void main()
{
int i,j,k;
double x,e,a,b;
double *A,*B,*X,*f;
printf("qing shu ru qu jian de xia jie he shang jie:");
scanf("%lf%lf",&a,&b);
printf("qing shu ru shi yan wu cha:");
scanf("%lf",&e);
k=(int)(log((b-a)/e)/log(2)-1)+1;
printf("k=%d\n",k);
A=(double *)malloc(k*sizeof(double ));
B=(double *)malloc(k*sizeof(double ));
f=(double *)malloc((k-1)*sizeof(double ));
X=(double *)malloc(k*sizeof(double ));
A[0]=a;
B[0]=b;
for(i=0;i<k;i++)
{
X[0]=(A[0]+B[0])/2;
if((pow(X[0],3+0.0)-X[0]-1)==0.0) x=X[0];
else if((pow(A[i],3+0.0)-A[i]-1.0)*(pow(X[i],3+0.0)-X[i]-1.0)<0)
{
A[i+1]=A[i];
B[i+1]=X[i];
X[i+1]=(A[i+1]+B[i+1])/2;
}
else if((pow(B[i],3+0.0)-B[i]-1)*(pow(X[i],3+0.0)-X[i]-1)<0)
{
A[i+1]=X[i];
B[i+1]=B[i];
X[i+1]=(A[i+1]+B[i+1])/2;
}
}
/* for(i=0;i<=k;i++)
{
printf("%lf\n",A[i]);
printf("%lf\n",B[i]);
printf("%lf\n",X[i]);
} */
printf("yong er fen fa qiu de de jie guo wei:\n");
printf("%lf\n",x=X[6]);
}
如何对非线形方程的二分法求根进行改进?