Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package findallroots1;
- import java.util.ArrayList;
- import java.util.List;
- public class FINDALLROOTS1
- {
- //public static double ESP = 0.001;
- public static double F(double x)
- {
- double result = (Math.pow(x, 2))-(4*x)-10; //x2-4x-10=0
- return result;
- }
- public static double F(double a,double b,double c,double x)
- {
- double result = (a*Math.pow(x, 2))+(b*x)+c; //x2-4x-10=0
- return result;
- }
- public static double sumAll(List<Double> list)
- {
- double sum=0;
- for(int i=0;i<list.size();i++)
- {
- sum=sum+list.get(i);
- }
- return sum;
- }
- public static double maximumPoint(double a,double b,double c)
- {
- double x = Math.ceil(Math.sqrt(Math.pow((b/a),2)-(2*(c/a))));
- return x;
- }
- public static boolean checkSign(double x)
- {
- if(x<0)
- {
- return false;
- }
- return true;
- }
- public static void bisection(double x1,double x2)
- {
- //x2-4x-10
- int i = 1;
- double x0;
- double f2,f0,f1,t;
- double ESP = 0.001;
- System.out.println("\n__________________________________________________________________\n");
- System.out.println("\niteration\t x1\t x2\t x0\t f1\t f2\t f0");
- System.out.println("\n___________________________________________________________________\n");
- do
- {
- x0=(x1+x2)/2;
- f1=F(x1);
- f2=F(x2);
- f0=F(x0);
- System.out.println("\n"+" "+i+" "+x1+" "+x2+" "+x0+" "+f1+" "+f2+" "+f0);
- if(f1*f0<0)
- {
- x2=x0;
- }
- else
- {
- x1=x0;
- }
- i++;
- }
- while(Math.abs(f0)>ESP);
- System.out.println("\n__________________________________________________________\n");
- System.out.println("\n\nRoot = "+x0);
- }
- public static void main(String[] args)
- {
- // a=1,b=-4,c=-10
- double initialStart=maximumPoint(1,-4,-10);
- double initialEnd=-maximumPoint(1,-4,-10);
- double stepSize = 1;
- //create a chart of x and y=F(x)
- List<Double> listX =new ArrayList();
- List<Double> listFX = new ArrayList();
- for(double i=initialEnd;i<=initialStart;i=i+stepSize)
- {
- double X = i;
- double FX = F(X);
- listX.add(X);
- listFX.add(FX);
- }
- //now find each two points that bracket root
- List<Double> listBracket = new ArrayList();
- for(int i=0;i<listX.size()-1;i++)
- {
- boolean previous = checkSign(listFX.get(i));
- boolean next = checkSign(listFX.get(i+1));
- if(previous!=next)
- {
- listBracket.add(listX.get(i));
- listBracket.add(listX.get(i+1));
- }
- }
- //finally do bisection between them
- for(int i=0;i<listBracket.size();i=i+2)
- {
- bisection(listBracket.get(i),listBracket.get(i+1));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement