#include <stdio.h>
#include <conio.h>
#include <math.h>
#define SIZE 10
void FalsePosition(float[]);
char ask();
void readArray1D(float [SIZE],int,char);
void printArray1D(float [SIZE], int , char);
void title(char []);
void main()
{
int i,j,k,n,n1,obs;
float X[SIZE],Y[SIZE],dUx[SIZE][SIZE],h,sum,s,x,u;
char ch,ch1;
clrscr();
do
{ clrscr();
printf("\n\t\t 2. False Position Method : \n");
printf("\n\t\t 5. Exit : \n");
flushall();
ch1=ask();
switch(ch1)
{
case '2':
title("False-Position Method");
FalsePosition(X);
break;
case '5':
printf("\nHit a key to terminate the programme...");
getch();
exit(0);
default:
printf("\n Invalid choice");
break;
}
}while(ch1=='y' || ch1=='Y');
}
void CheckObservation(int n)
{
if(n<=0)
{
printf("\n\t\t Value of observation is too low...");
getch();
exit(0);
}
}
char ask()
{
char ch='\0';
printf("\n\nEnter your choice :");
scanf("%c",&ch);
return ch;
}
void title(char title[])
{
printf("\t\t\t\t ");
puts(title);
}
// For reading 1-Dimensional array.
void readArray1D(float X[],int n,char c)
{
int i;
printf("\n\n Enter value for the following: \n");
for(i=0;i<n;i++)
{
printf("\n%c[%d] : ",c,i);
scanf("%f",&X[i]);
}
}
// For printing 1-Dimensional array
void printArray1D(float X[],int n,char c)
{
int i;
printf("\n\n");
for(i=0;i<n;i++)
{
printf("%c[%d] : %f",c,i,X[i]);
}
}
//To find the factorial
double fact(int no)
{
int i;
float f=1;
for(i=no;i>=1;i--)
{
f*=i;
}
return(f);
}
// To calculate value of a polynomial [f(x)] for given x.
//if f(x)=2x^3 ,where x=1
//then f(1)=2(1)^3 = 8
float f(float X[],int n,float x)
{
float sum=0;
int i;
for(i=0;i<=n;i++)
{
sum = sum + X[i] * pow(x,i);
}
return(sum);
}
//"False position"Or Regular-falsi position method.
// This is the Oldest Method to find the root of the polynomial..
// x3 = ( (x1*f2) - (x2*f1) ) / ( f2-f1 );
void FalsePosition(float X[SIZE])
{
float x1,x2,epsilon=0.0001,x3=0,x3p,delta=0.001,f1,f2,f3;
int n,i=0;
char ch;
clrscr();
title("False-Position Method");
printf("\n False position is the Oldest Method to find the root of the polynomial");
printf("\n Two approximation are required.(x1 and x2)");
getch();
clrscr();
title("False-Position Method");
while((ch=getch())!=27)
{
if (ch == '@') break; //Just so that we have a known exit.
printf("\n\nEnter the degree of polynomial:");
scanf("%d",&n);
CheckObservation(n);
readArray1D(X,n+1,'X');
printf("\nEnter first point of the search interval:");
scanf("%f",&x1);
printf("\nEnter second point of the search interval:");
scanf("%f",&x2);
f1 = f( X,n,x1 );
f2 = f( X,n,x2 );
if( f(X,n,x1) == 0 )
{
printf("\n\n\t\t THE REQUIRED ROOT OF f(x) = 0 IS %.4f\n",x1);
getch();
return ;
}
if( f(X,n,x2) == 0 )
{
printf("\n\n\t\t THE REQUIRED ROOT OF f(x) = 0 IS %.4f\n",x2);
getch();
return ;
}
if(f1*f2 > 0)
{
printf("\nInitial approximation are unsuitable \n");
getch();
return ;
}
clrscr();
printf("\n \t\t\t FALSE POSITION METHOD\n");
printf("\n i\t\t x1\t \t x2\t\t x3");
do
{
i++;
if(fabs(f2-f1)<=delta)
{
printf("\nSlope of funtion becomes too small \n");
printf("\nEntered roots(x1,x2) are improper..!!");
printf("\nEnter any key to continue... ");
getch();
break;
}
x3p = x3;
x3 = ( x1*f2-x2*f1 ) / ( f2-f1 );
f3 = f( X,n,x3 );
if( f1*f3 < 0 )
{
x2 = x3;
f2 = f3;
}
else
{
x1 = x3;
f1 = f3;
}
printf ("%d\t|\t%5.2f\t|\t%5.2f\t|\t%5.2f\t\n",i,x1,x2,x3);
} while( fabs( (double) ( (x3-x3p) / x3 ) ) > epsilon );
printf("\n\n\t\t THE REQUIRED ROOT OF f(x) = 0 IS %.4f\n",x3);
}
}