6
2. Program to solve non-linear equation using the false position method.
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float);
void main(){
int i = 0;
float x1, x2, x3, f1, f2, f3, epsilon, delta;
clrscr();
printf("\nEnter two initial approximations x1, x2: ");
scanf("%f %f", &x1, &x2);
printf("\nEnter two very small number epsilon and delta: ");
scanf("%f %f", &epsilon, &delta);
f1 = f(x1);
f2 = f(x2);
printf("\nf(%.2f) = %9f, f(%.2f) = %9f", x1, f1, x2, f2);
if(f1 * f2 > 0){
printf("\nInitial approximations x1 = %f, x2 = %f are not proper.",
x1, x2);
exit(0);
}
do{
if(fabs(f2 - f1) < delta){
printf("\nSlope curve is too small.");
exit(0);
}
x3 = ((x1 * f2) - (x2 * f1))/ (f2 - f1);
i++;
f3 = f(x3);
printf("\nNext approximation after %2d iteration is %f, f(x) =
%9f", i, x3, f3);
if(f1 * f3 < 0){
x2 = x3;
f2 = f3;
}
else{
x1 = x3;
f1 = f3;
}
}while((fabs(f3) > epsilon) && (f3 != 0));
printf("\nRoot = %.4f", x3);
getch();
}