You are on page 1of 1

/*18.program to find roots of the equation using bisection method.

Name : Shivam Gupta


Roll No : 424
M.Sc OR Sem 2
*/

import java.io.*;
import java.lang.Math;

class bsctn
{
public static double fun(double a,double b,double c,double d,double e,double
f,double x)
{
return(a*Math.pow(x,5)+b*Math.pow(x,4)+c*Math.pow(x,3)+d*Math.pow(x,2)+e*x+f);
}

public static void main(String args[])


throws IOException
{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


double a,b,c,d,e,f,x1,x2,x3,epsilon;
String str;
System.out.println("Enter a");
a=Double.parseDouble(br.readLine());
System.out.println("Enter b");
b=Double.parseDouble(br.readLine());
System.out.println("Enter c");
c=Double.parseDouble(br.readLine());
System.out.println("Enter d");
d=Double.parseDouble(br.readLine());
System.out.println("Enter e");
e=Double.parseDouble(br.readLine());
System.out.println("Enter f");
f=Double.parseDouble(br.readLine());

do
{
System.out.println("Enter the first approximation");
x1=Double.parseDouble(br.readLine());
System.out.println("Enter the second approximation");
x2=Double.parseDouble(br.readLine());
}while(fun(a,b,c,d,e,f,x1)*fun(a,b,c,d,e,f,x2)<0);

System.out.println("Enter the epsilon");


epsilon=Double.parseDouble(br.readLine());

do
{
x3=(x1+x2)/2;
if(fun(a,b,c,d,e,f,x1)*fun(a,b,c,d,e,f,x3)<0)
x2=x3;
else
x1=x3;
}while(Math.abs(x1-x2)>epsilon);
System.out.println("Approximate root ="+x3);
}
}

You might also like