// EQN -> 3*x + sin(x) - exp(x) = 0
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<process.h>
// Formulae Declaration
#define EPS 0.00005
#define f(x) 3*x + sin(x) - exp(x)
int n;
void FAL_POS();
void main()
{
clrscr();
printf("\n Solution by ITERATION METHOD ");
printf("\n\n Equation is -> x*x*x - 2*x + 1 = 0\n");
printf("\n Enter the no. of iterations ");
scanf("%d",&n);
FAL_POS();
getch();
}
void FAL_POS()
{
long float x1,x2,x0;
float f1,f2,f0;
int itr=n,i;
for(x1=0.0;;)
{
f1=f(x1);
if(f1>0)
break;
else
x1=x1+0.1;
}
x0=x1-0.1;
f0=f(x0);
printf("\n\t\t----------------------------------------------------");
printf("\n \t\tITERATION\t x2\t\t\t F(X)\n");
printf("\n\t\t----------------------------------------------------\n");
for(i=0;i<itr;i++)
{
x2=x0-((x1-x0)/(f1-f0))*f0;
f2=f(x2);
if(f0*f2>0)
{
x1=x2;
f1=f2;
}
else
{
x0=x2;
f0=f2;
}
if(fabs(f(2))>EPS)
printf("\n\t\t %d \t\t %f \t\t %f \n",i+1,x2,f2);
}
printf("\n\n\n\t\t----------------------------------------------------");
printf("\n\t\t ROOT = %f ",x2);
printf("\n\t\t----------------------------------------------------");
}
No comments:
Post a Comment