Advertisement
rootUser

fixed point

Jun 12th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.04 KB | None | 0 0
  1. package fixedpoint;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class FIXEDPOINT
  6. {
  7.     static double E = 0.001;
  8.    
  9.     public static double F(double x)
  10.     {
  11.         double result = Math.pow(x, 3) - x - 1 ; // x3-x-1=0
  12.         return result;
  13.     }
  14.    
  15.     public static double G1(double x)
  16.     {
  17.         double result = Math.pow(x,3)-1 ; // x3-1
  18.         return result;
  19.     }
  20.    
  21.     public static double G2(double x)
  22.     {
  23.         double result = Math.pow((x+1),1/3) ; // (x+1)^(1/3)
  24.         return result;
  25.     }
  26.        
  27.     public static double G3(double x)
  28.     {
  29.         double result = 1/(Math.pow(x,2)-1) ; // 1/(x2-1)
  30.         return result;
  31.     }
  32.    
  33.     public static double dG1(double x)
  34.     {
  35.         double result = 3*Math.pow(x,2); //3x2
  36.         return result;
  37.     }
  38.    
  39.     public static double dG2(double x)
  40.     {
  41.         double result = 1/(3*Math.pow((x+1),2/3)); //  1/(3*(x+1)^(2/3))
  42.         return result;
  43.     }
  44.    
  45.     public static double dG3(double x)
  46.     {
  47.         double result = (-2*x)/(Math.pow(Math.pow(x,2)-1,2)); // -2x/(x2-1)^2
  48.         return result;
  49.     }
  50.    
  51.     public static int convergence(double x)
  52.     {
  53.         if(dG1(x)<1)
  54.         {
  55.             return 1;
  56.         }
  57.         else if(dG2(x)<1)
  58.         {
  59.             return 2;
  60.         }
  61.         else if(dG3(x)<1)
  62.         {
  63.             return 3;
  64.         }
  65.         return 0;
  66.     }
  67.  
  68.     public static void main(String[] args)
  69.     {
  70.         System.out.println("Enter initial guess : ");
  71.         Scanner scanner = new Scanner(System.in);
  72.         double root = scanner.nextDouble();
  73.         double update ;
  74.         int iteration = 1;
  75.         //determine which conversion will go
  76.         int choice = convergence(root);
  77.         if(choice==1)
  78.         {
  79.             while(true)
  80.             {
  81.                 update = G1(root);
  82.                 if(Math.abs(update-root)<E)
  83.                 {
  84.                     break;
  85.                 }
  86.                 System.out.println("Iteration : "+iteration+" X = "+update);
  87.                 iteration++;
  88.                 root=update;
  89.             }
  90.         }
  91.         else if(choice==2)
  92.         {
  93.             while(true)
  94.             {
  95.                 update = G2(root);
  96.                 if(Math.abs(update-root)<E)
  97.                 {
  98.                     break;
  99.                 }
  100.                 System.out.println("Iteration : "+iteration+" X = "+update);
  101.                 iteration++;
  102.                 root=update;
  103.             }
  104.         }
  105.         else if(choice==3)
  106.         {
  107.             while(true)
  108.             {
  109.                 update = G3(root);
  110.                 if(Math.abs(update-root)<E)
  111.                 {
  112.                     break;
  113.                 }
  114.                 System.out.println("Iteration : "+iteration+" X = "+update);
  115.                 iteration++;
  116.                 root=update;
  117.             }
  118.         }
  119.         else
  120.         {
  121.             System.out.println("");
  122.         }
  123.         System.out.println("The root is "+root+" after "+iteration+" iterations");
  124.     }
  125.    
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement