C program for bisection method to find a root of the nonlinear function
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{ float fun(float m);
float x1,x2,x3,p,q,r;
int i=0;
clrscr();
l10: printf("\nequation:x*(exp(x)-1) ");
printf("\nenter the app value of x1,x2:");
scanf("%f %f",&x1,&x2);
if(fun(x1)*fun(x2)>0)
{
printf("\n wrong values entered...enter again:\n");
goto l10;}
else
printf("\n the root lies b/w %f & %f",x1,x2);
printf("\n n x1 x2 x3 f(x1) f(x2) f(x3)");
l15: x3=(x1+x2)/2;
p=fun(x1);
q=fun(x2);
r=fun(x3);
i=i++;
printf("\n%d %f %f %f %f %f %f",i,x1,x2,x3,p,q,r);
if((p*r)>0)
x1=x3;
else
x2=x3;
if((fabs((x2-x1)/x2))<=0.001)
{
printf("\n root of the equ is %f",x3);
getch();
exit(0);
}
else goto l15;
}
float fun(float m)
{
float g;
g=(m*(exp(m))-1);
return(g);
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{ float fun(float m);
float x1,x2,x3,p,q,r;
int i=0;
clrscr();
l10: printf("\nequation:x*(exp(x)-1) ");
printf("\nenter the app value of x1,x2:");
scanf("%f %f",&x1,&x2);
if(fun(x1)*fun(x2)>0)
{
printf("\n wrong values entered...enter again:\n");
goto l10;}
else
printf("\n the root lies b/w %f & %f",x1,x2);
printf("\n n x1 x2 x3 f(x1) f(x2) f(x3)");
l15: x3=(x1+x2)/2;
p=fun(x1);
q=fun(x2);
r=fun(x3);
i=i++;
printf("\n%d %f %f %f %f %f %f",i,x1,x2,x3,p,q,r);
if((p*r)>0)
x1=x3;
else
x2=x3;
if((fabs((x2-x1)/x2))<=0.001)
{
printf("\n root of the equ is %f",x3);
getch();
exit(0);
}
else goto l15;
}
float fun(float m)
{
float g;
g=(m*(exp(m))-1);
return(g);
}
OUTPUT:
No comments:
Post a Comment