Advertisement
rootUser

Bisection

Jun 1st, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.44 KB | None | 0 0
  1. package bisection;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main
  6. {
  7.     static double F(double x)
  8.     {
  9.         double result = (x)*(x)*(x) + (x)*(x) + (x) + 7 ;
  10.         return result;
  11.     }
  12.     public static void main(String[] args)
  13.     {
  14.         int i = 1;
  15.         double x1,x2,x0;
  16.         double f2,f0,f1,t;
  17.         double ESP = 0.001;
  18.        
  19.         Scanner s = new Scanner(System.in);
  20.  
  21.         System.out.println("\nEnter the value of x1: ");
  22.          x1 = s.nextDouble(); //-2
  23.         System.out.println("\nEnter the value of x2: ");
  24.         x2 = s.nextDouble(); //-3
  25.         System.out.println("\n__________________________________________________________________\n");
  26.         System.out.println("\niteration\t x1\t       x2\t x0\t   f1\t   f2\t   f0");
  27.         System.out.println("\n___________________________________________________________________\n");
  28.         do
  29.         {
  30.             x0=(x1+x2)/2;
  31.             f1=F(x1);
  32.             f2=F(x2);
  33.             f0=F(x0);
  34.             System.out.println("\n"+" "+i+" "+x1+" "+x2+" "+x0+" "+f1+" "+f2+" "+f0);
  35.             if(f1*f0<0)
  36.             {
  37.                 x2=x0;
  38.             }
  39.             else
  40.             {
  41.                 x1=x0;
  42.             }
  43.             i++;
  44.         }
  45.         while(Math.abs(f0)>ESP);
  46.         System.out.println("\n__________________________________________________________\n");
  47.         System.out.println("\n\nApp.root = "+x0);
  48.  
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement