Advertisement
rootUser

Find all roots

Jun 12th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.15 KB | None | 0 0
  1. package findallroots1;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class FINDALLROOTS1
  7. {
  8.     //public static double ESP = 0.001;
  9.     public static double F(double x)
  10.     {
  11.         double result = (Math.pow(x, 2))-(4*x)-10; //x2-4x-10=0
  12.         return result;
  13.     }
  14.     public static double F(double a,double b,double c,double x)
  15.     {
  16.         double result = (a*Math.pow(x, 2))+(b*x)+c; //x2-4x-10=0
  17.         return result;
  18.     }
  19.     public static double sumAll(List<Double> list)
  20.     {
  21.         double sum=0;
  22.         for(int i=0;i<list.size();i++)
  23.         {
  24.            sum=sum+list.get(i);
  25.         }
  26.         return sum;
  27.     }
  28.     public static double maximumPoint(double a,double b,double c)
  29.     {
  30.         double x = Math.ceil(Math.sqrt(Math.pow((b/a),2)-(2*(c/a))));
  31.         return x;
  32.     }
  33.     public static boolean checkSign(double x)
  34.     {
  35.         if(x<0)
  36.         {
  37.             return false;
  38.         }
  39.         return true;
  40.     }
  41.     public static void bisection(double x1,double x2)
  42.     {
  43.         //x2-4x-10
  44.         int i = 1;
  45.         double x0;
  46.         double f2,f0,f1,t;
  47.         double ESP = 0.001;
  48.        
  49.         System.out.println("\n__________________________________________________________________\n");
  50.         System.out.println("\niteration\t x1\t       x2\t x0\t   f1\t   f2\t   f0");
  51.         System.out.println("\n___________________________________________________________________\n");
  52.         do
  53.         {
  54.             x0=(x1+x2)/2;
  55.             f1=F(x1);
  56.             f2=F(x2);
  57.             f0=F(x0);
  58.             System.out.println("\n"+" "+i+" "+x1+" "+x2+" "+x0+" "+f1+" "+f2+" "+f0);
  59.             if(f1*f0<0)
  60.             {
  61.                 x2=x0;
  62.             }
  63.             else
  64.             {
  65.                 x1=x0;
  66.             }
  67.             i++;
  68.         }
  69.         while(Math.abs(f0)>ESP);
  70.         System.out.println("\n__________________________________________________________\n");
  71.         System.out.println("\n\nRoot = "+x0);
  72.     }
  73.     public static void main(String[] args)
  74.     {
  75.         // a=1,b=-4,c=-10
  76.         double initialStart=maximumPoint(1,-4,-10);
  77.         double initialEnd=-maximumPoint(1,-4,-10);
  78.         double stepSize = 1;
  79.         //create a chart of x and y=F(x)
  80.         List<Double> listX =new ArrayList();
  81.         List<Double> listFX = new ArrayList();
  82.         for(double i=initialEnd;i<=initialStart;i=i+stepSize)
  83.         {
  84.             double X = i;
  85.             double FX = F(X);
  86.             listX.add(X);
  87.             listFX.add(FX);
  88.         }
  89.         //now find each two points that bracket root
  90.         List<Double> listBracket = new ArrayList();
  91.         for(int i=0;i<listX.size()-1;i++)
  92.         {
  93.             boolean previous = checkSign(listFX.get(i));
  94.             boolean next = checkSign(listFX.get(i+1));
  95.             if(previous!=next)
  96.             {
  97.                 listBracket.add(listX.get(i));
  98.                 listBracket.add(listX.get(i+1));
  99.             }
  100.         }
  101.         //finally do bisection between them
  102.         for(int i=0;i<listBracket.size();i=i+2)
  103.         {
  104.             bisection(listBracket.get(i),listBracket.get(i+1));
  105.         }
  106.     }
  107.    
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement