/*False Position Method*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<math.h>
#include<process.h>
float **e,n;
float value(float);
void main()
{
float x,v,v1,v2,xr,xu,xl,xo=0,ea,es;
int i,p,it=50;
char ch;
clrscr();
printf("Enter degree\n");
scanf("%f",&n);
e=(float**)malloc(((n+1)*2)*4);
puts("------------------------------------------");
printf("Enter coeff.in decreasing order of degree\n");
for(i=0,p=n;i<=n;i++,p--)
{
scanf("%f",&e[i][0]);
e[i][1]=p;
}
puts("------------------------------------------");
printf("Enter value of xl and xu\n");
scanf("%f %f",&xl,&xu);
puts("------------------------------------------");
puts("What would you like to Enter\nEs or Number of significant fig or Iteration .=>E/S/I");
ch=getche();
if(ch=='E'||ch=='e')
{
printf("\nEnter value of Es\n");
scanf("%f",&es);
}
else if(ch=='i'||ch=='I')
{printf("Enter no. of Iteration"); scanf("%d",&it);
}
else
{
printf("\nEnter no. of sig. fig\n");
scanf("%d",&p);
es=0.5*pow(10,(2-p));
}
puts("-------------------------------------------");
i=0;
printf("Iteration\tX\t\tEa");
while(1)
{
i++; /*-- Iteration number --*/
v1=value(xl);
v2=value(xu);
if(v1*v2>=0)
{
printf("Invalid Value of xl and xu");
getch();
exit(0);
}
xr=xu-(v2*(xl-xu)/(v1-v2));
ea=(xr-xo)*100/xr;
if(ea<0)
ea=ea*-1;
v=value(xr);
if(v==0||ea<es)
{
printf("\n%3d\t\t%6.5f\t\t%5.4f",i,xr,ea);
puts("\n-----------------------------------------------");
getch();
exit(0);
}
printf("\n%3d\t\t%6.5f\t\t%5.4f",i,xr,ea);
if(v1*v<0)
xu=xr;
else
xl=xr;
xo=xr; }
}
float value(float x)
{
int i;
float s=0;
for(i=0;i<=n;i++)
{
s=s+e[i][0]*pow(x,e[i][1]);
}
return s;
}
Output Of Program:-
First:-
Enter degree
2
------------------------------------------
Enter coeff.in decreasing order of degree
-0.4 2.2 4.7
------------------------------------------
Enter value of xl and xu
5 10
------------------------------------------
What would you like to Enter
Es or Number of significant fig.or Iteration=>E/S/I
e
Enter value of Es
1
-------------------------------------------
Iteration X Ea
1 6.50000 100.0000
2 6.97727 6.8404
3 7.10297 1.7696
4 7.13435 0.4399
-----------------------------------------------
Second:-
Enter degree
2
------------------------------------------
Enter coeff.in decreasing order of degree
-0.5 2.5 4.5
------------------------------------------
Enter value of xl and xu
5 10
------------------------------------------
What would you like to Enter
Es or Number of significant fig.or Iteration=>E/S/I
I
Enter no. of Iteration
4
-------------------------------------------
Iteration X Ea
1 5.90000 100.0000
2 6.23853 5.4265
3 6.35184 1.7838
4 6.38825 0.5700
-----------------------------------------------
No comments:
Post a Comment