Advertisement
kdaud

Newton Raphson Method

Mar 8th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. package NumericalComputing.NewtonRaphsonMethod;
  2.  
  3. import java.util.Scanner;
  4.  
  5. @interface DaudKakumirizi
  6. {
  7.     String name() default " ";
  8.     String ID() default " ";
  9. }
  10. @DaudKakumirizi(name = "Kakumirizi Daud", ID = "17/BSS/BU/R/0010")
  11.  
  12. public class NewtonRaphson {
  13.     public static void main(String[] args) {
  14.         double x0,root,givenError,error;
  15.         boolean flag=true;
  16.         int i=1;
  17.         Scanner sc = new Scanner(System.in);
  18.         int highest_degree;
  19.         System.out.println("What is the highest degree of your polynomial? ");
  20.         Scanner input = new Scanner(System.in);
  21.         highest_degree = input.nextInt();
  22.         for (int j = highest_degree; j >= 0; j--) {
  23.             int coeff_deg_j;
  24.             coeff_deg_j = poly_input(j);
  25.             /**System.out.println(coeff_deg_i); */
  26.         }
  27.  
  28.         System.out.print("Enter value for x0 : ");
  29.         x0 = sc.nextDouble();
  30.  
  31.         givenError=x0;
  32.  
  33.         while(flag){
  34.             root = x0 - (F(x0)/Fprime_X(x0));
  35.             error = Math.abs((root-x0)/root)*100;
  36.  
  37.             System.out.println("Iteration : "+ i++);
  38.             System.out.println("Root:"+root);
  39.             System.out.println("Error:"+error);
  40.             x0 = root;
  41.  
  42.             if(givenError>error)
  43.             {
  44.                 flag=false;
  45.             }
  46.         }
  47.     }
  48.  
  49.     public static double F(double x){
  50.         return Math.pow(x,3)-(0.165*Math.pow(x,2))+(3.993*Math.pow(10,-4));
  51.     }
  52.  
  53.  
  54.     public static double Fprime_X(double x){
  55.         return 3*Math.pow(x,2)-0.33*x;
  56.     }
  57.     public static int poly_input(int degree) {
  58.  
  59.         System.out.println("Please enter coefficient for degree " + degree);
  60.  
  61.         Scanner input = new Scanner(System.in);
  62.         int coefficient;
  63.         coefficient = input.nextInt();
  64.         return coefficient;
  65.     }
  66.     /**
  67.      * This program has been written by KAKUMIRIZI DAUD
  68.      */
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement