Program to Find roots using Bisection Method
// Bisection Method
#include<stdio.h>
#include<math.h>
#define f(a,b,c,x) (a*x*x)+(b*x)+c
using namespace std;
int main()
{
int a,b,c,i,j;
float z,n,x,y,x1,x2,m;
printf("Enter the values of a,b and c\n");
scanf("%d%d%d",&a,&b,&c);
printf("Enter the range where you can find the roots\n");
scanf("%f%f",&x1,&x2);
//x=(a*x1*x1)+(b*x1)+c;
x=f(a,b,c,x1);
//y=(a*x2*x2)+(b*x2)+c;
y=f(a,b,c,x2);
n=20;
if(x*y>=0)
printf("No roots b/w them\n Exiting...");
else
{
while(n--)
{
m=(x1+x2)/2;
#include<stdio.h>
#include<math.h>
#define f(a,b,c,x) (a*x*x)+(b*x)+c
using namespace std;
int main()
{
int a,b,c,i,j;
float z,n,x,y,x1,x2,m;
printf("Enter the values of a,b and c\n");
scanf("%d%d%d",&a,&b,&c);
printf("Enter the range where you can find the roots\n");
scanf("%f%f",&x1,&x2);
//x=(a*x1*x1)+(b*x1)+c;
x=f(a,b,c,x1);
//y=(a*x2*x2)+(b*x2)+c;
y=f(a,b,c,x2);
n=20;
if(x*y>=0)
printf("No roots b/w them\n Exiting...");
else
{
while(n--)
{
m=(x1+x2)/2;
//z=(a*m*m)+(b*m)+c;
z=f(a,b,c,m);
//x=(a*x1*x1)+(b*x1)+c;
x=f(a,b,c,x1);
//y=(a*x2*x2)+(b*x2)+c;
y=f(a,b,c,x2);
if(z*x<0)
z=f(a,b,c,m);
//x=(a*x1*x1)+(b*x1)+c;
x=f(a,b,c,x1);
//y=(a*x2*x2)+(b*x2)+c;
y=f(a,b,c,x2);
if(z*x<0)
x2=m;
else if(z*y<0)
else if(z*y<0)
x1=m;
}
printf("\nRoot is %f",m);
}
return 0;
}
}
printf("\nRoot is %f",m);
}
return 0;
}
HAPPY CODING :)
No comments:
Post a Comment