#include<stdio.h>
#include<conio.h>
#include<math.h>
#define ESP 0.001
#define F(x) (x)*(x)*(x) + 2*(x)*(x) + 10*(x) - 20
void main()
{
double x1,x2,x3,x4_1,x4_2,fx1,fx2,fx3,
h1,h2,h3_1,h3_2,h4,D,d1,d2,a1,a2,a0;
int i=1;
clrscr();
printf("\nEnter the value of x1: ");
scanf("%lf",&x1);
printf("\nEnter the value of x2: ");
scanf("%lf",&x2);
printf("\nEnter the value of x3: ");
scanf("%lf",&x3);
fx1 = F(x1);
printf("\n\n f(x1) = %lf",fx1);
getch();
fx2 = F(x2);
printf("\n\n f(x2) = %lf",fx2);
getch();
fx3 = a0 = F(x3);
printf("\n\n f(x3) = %lf",fx3);
getch();
h1 = x1-x3;
h2 = x2-x3;
d1 = fx1-fx3;
d2 = fx2-fx3;
D = h1*h2*(h1-h2);
a1 = (d2*h1*h1 - d1*h2*h2)/D;
a2 = (d1*h2 - d2*h1)/D;
h3_1 = -((2*a0)/(a1 + sqrt(fabs(a1*a1 - (4*a2*a0)))));
h3_2 = -((2*a0)/(a1 - sqrt(fabs(a1*a1 - (4*a2*a0)))));
if( (a1 + sqrt(fabs(a1*a1 - (4*a2*a0)))) >
((a1 - sqrt(fabs(a1*a1 - (4*a2*a0))))) )
{
h4 = h3_1;
}
else
{
h4 = h3_2;
}
x4_1 = x3 + h4;
printf("\n\n\n x4 = %lf \n",x4_1);
x1=x2;
x2=x3;
x3=x4_1;
printf("\n\nx1 = %lf",x1);
printf("\n\nx2 = %lf",x2);
printf("\n\nx3 = %lf",x3);
getch();
do
{
fx1 = F(x1);
fx2 = F(x2);
fx3 = a0 = F(x3);
h1 = x1-x3;
h2 = x2-x3;
d1 = fx1-fx3;
d2 = fx2-fx3;
D = h1*h2*(h1-h2);
a1 = (d2*h1*h1 - d1*h2*h2)/D;
a2 = (d1*h2 - d2*h1)/D;
h3_1 = -((2*a0)/(a1 + sqrt(fabs(a1*a1 - (4*a2*a0)))));
h3_2 = -((2*a0)/(a1 - sqrt(fabs(a1*a1 - (4*a2*a0)))));
if( (a1 + sqrt(fabs(a1*a1 - (4*a2*a0)))) >
(a1 - sqrt(fabs(a1*a1 - (4*a2*a0)))) )
{
h4 = h3_1;
}
else
{
h4 = h3_2;
}
x4_2 = x3 + h4;
printf("\n\n\n x4 = %lf \n",x4_2);
getch();
if(fabs(x4_1 - x4_2) < ESP)
{
printf("\n\nREAL ROOT = %.3lf",x4_2);
i=0;
}
else
{
x4_1=x4_2;
x1=x2;
x2=x3;
x3=x4_1;
printf("\n\nx1 = %lf",x1);
printf("\n\nx2 = %lf",x2);
printf("\n\nx3 = %lf",x3);
}
}while(i!=0);
getch();
}
/*
____________________________
OUT PUT
____________________________
Enter the value of x1: 0
Enter the value of x2: 1
Enter the value of x3: 2
f(x1) = -20.000000
f(x2) = -7.000000
f(x3) = 16.000000
x4 = 1.354066
x1 = 1.000000
x2 = 2.000000
x3 = 1.354066
x4 = 1.368647
x1 = 2.000000
x2 = 1.354066
x3 = 1.368647
x4 = 1.368808
REAL ROOT = 1.369
*/