Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fixedpoint;
- import java.util.Scanner;
- public class FIXEDPOINT
- {
- static double E = 0.001;
- public static double F(double x)
- {
- double result = Math.pow(x, 3) - x - 1 ; // x3-x-1=0
- return result;
- }
- public static double G1(double x)
- {
- double result = Math.pow(x,3)-1 ; // x3-1
- return result;
- }
- public static double G2(double x)
- {
- double result = Math.pow((x+1),1/3) ; // (x+1)^(1/3)
- return result;
- }
- public static double G3(double x)
- {
- double result = 1/(Math.pow(x,2)-1) ; // 1/(x2-1)
- return result;
- }
- public static double dG1(double x)
- {
- double result = 3*Math.pow(x,2); //3x2
- return result;
- }
- public static double dG2(double x)
- {
- double result = 1/(3*Math.pow((x+1),2/3)); // 1/(3*(x+1)^(2/3))
- return result;
- }
- public static double dG3(double x)
- {
- double result = (-2*x)/(Math.pow(Math.pow(x,2)-1,2)); // -2x/(x2-1)^2
- return result;
- }
- public static int convergence(double x)
- {
- if(dG1(x)<1)
- {
- return 1;
- }
- else if(dG2(x)<1)
- {
- return 2;
- }
- else if(dG3(x)<1)
- {
- return 3;
- }
- return 0;
- }
- public static void main(String[] args)
- {
- System.out.println("Enter initial guess : ");
- Scanner scanner = new Scanner(System.in);
- double root = scanner.nextDouble();
- double update ;
- int iteration = 1;
- //determine which conversion will go
- int choice = convergence(root);
- if(choice==1)
- {
- while(true)
- {
- update = G1(root);
- if(Math.abs(update-root)<E)
- {
- break;
- }
- System.out.println("Iteration : "+iteration+" X = "+update);
- iteration++;
- root=update;
- }
- }
- else if(choice==2)
- {
- while(true)
- {
- update = G2(root);
- if(Math.abs(update-root)<E)
- {
- break;
- }
- System.out.println("Iteration : "+iteration+" X = "+update);
- iteration++;
- root=update;
- }
- }
- else if(choice==3)
- {
- while(true)
- {
- update = G3(root);
- if(Math.abs(update-root)<E)
- {
- break;
- }
- System.out.println("Iteration : "+iteration+" X = "+update);
- iteration++;
- root=update;
- }
- }
- else
- {
- System.out.println("");
- }
- System.out.println("The root is "+root+" after "+iteration+" iterations");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement