C语言中用递归算法实现二分法求方程在(0,5)的近似解?
重点改动:
scanf("%lf,%lf",&x1,&x2);
#include <stdio.h>
#include <math.h>
int main()
{ double calculate(double y1,double y2,double fy1,double fy2);
double x1,x2,fx1,fx2;
double a;
printf("enter x1 & x2:");
scanf("%lf,%lf",&x1,&x2);
fx1=(((x1+3)*x1-8)*x1+12)*x1-10;
fx2=(((x2+3)*x2-8)*x2+12)*x2-10;
printf("%f %f %f %f\n",x1,x2,fx1,fx2);
a=calculate(x1,x2,fx1,fx2);
printf("x=%lf\n",a);
return 0;
}
double calculate(double y1,double y2,double fy1,double fy2)
{ double fy0,y0;
double a;
y0=(y1+y2)/2;
fy0=(((y0+3)*y0-8)*y0+12)*y0-10;
printf("%f ",y0);
if(fabs(fy0)>=1e-10)
{ if ((fy0*fy1)<0)
{ y2=y0;
fy2=fy0;
}
else
{ y1=y0;
fy1=fy0;
}
a=calculate(y1,y2,fy1,fy2);
}
else
{ a=y0;
printf("%6.2f",a);
}
return(a);
}