#include <stdio.h>
#include <math.h>
#define thshold 2 * pow(10,-4)
double sinfx(double x){
return (x*x - sin(x));
}
void false_position(double xl, double xu)
{
int i;
double q0, q1, q,p;
double num = 40;
double xr2;
q0 = sinfx(xl);
q1 = sinfx(xu);
i = 1;
p = xu - q1 * (xu - xl) / (q1 - q0);
q = sinfx(p);
double e0=100.0;
printf("%d\t%lf\t%lf\t%lf\t%lf\n", i, xl,xu,p, e0);
while(1)
{
p = xu - q1 * (xu - xl) / (q1 - q0);
q = sinfx(p);
if (e0 < thshold)
{
break;
}
else
{
if((q*q1) < 0.)
{
xl = xu; q0 = q1;
}
xu = p; q1 = q;
}
xr2 = (xl+xu)/2;
e0 = fabs(q);
printf("%d\t%lf\t%lf\t%f\t %lf\n", ++i, xu,xl,p,e0);
}
printf("%lf\n", p);
}
int main()
{
false_position(0.5,1);
return 1;
}