Advertisement
kdaud

SecantMethod

Mar 8th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. package NumericalComputing.SecantMethod;
  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. @SuppressWarnings("ALL")
  13. public class Secant {
  14.  
  15.     /**
  16.      *
  17.      * @param function takes value of x and returns f(x)
  18.      * @return
  19.      */
  20.  
  21.     static float f(float x) {
  22.         /**
  23.          * we are taking equation
  24.          *  as x^3+x-1
  25.          */
  26.  
  27.  
  28.         float f = (float)Math.pow(x, 3)
  29.                 + x - 1;
  30.  
  31.         return f;
  32.     }
  33.  
  34.     static void secant(float x1, float x2,
  35.             float E) {
  36.  
  37.         float n = 0, xm, x0, c;
  38.         if (f(x1) * f(x2) < 0)
  39.         {
  40.             do {
  41.  
  42.                 /**
  43.                  * calculate the intermediate
  44.                  * value
  45.                  */
  46.  
  47.  
  48.                 x0 = (x1 * f(x2) - x2 * f(x1))
  49.                         / (f(x2) - f(x1));
  50.  
  51.                 /**
  52.                  * check if x0 is root of equation or not
  53.                  */
  54.  
  55.  
  56.                 c = f(x1) * f(x0);
  57.  
  58.                 /** update the value of interval */
  59.                 x1 = x2;
  60.                 x2 = x0;
  61.  
  62.                 /** update number of iteration */
  63.                 n++;
  64.                 /** if x0 is the root of equation then break the loop */
  65.                 if (c == 0)
  66.                     break;
  67.                 xm = (x1 * f(x2) - x2 * f(x1))
  68.                         / (f(x2) - f(x1));
  69.  
  70.                 /** repeat the loop until the convergence*/
  71.  
  72.             } while (Math.abs(xm - x0) >= E);
  73.  
  74.             System.out.println("Therefore the Root is ---> " + x0);
  75.             System.out.println();
  76.  
  77.             System.out.println("No. of "
  78.                     + "iterations = " + n);
  79.         }
  80.  
  81.         else
  82.             System.out.print("Can not find a"+ " root in the given inteval");
  83.     }
  84.  
  85.     /** Driver code */
  86.     public static void main(String[] args) {
  87.  
  88.         System.out.println("Implementation of Secant Method in Java");
  89.         System.out.println();
  90.  
  91.         Scanner sc = new Scanner(System.in);
  92.         int highest_degree;
  93.         System.out.println("What is the highest degree of your polynomial? ");
  94.         Scanner input = new Scanner(System.in);
  95.         highest_degree = input.nextInt();
  96.         for (int x = highest_degree; x >= 0; x--) {
  97.             int coeff_deg_x;
  98.             coeff_deg_x = poly_input(x);
  99.             /**System.out.println(coeff_deg_i); */
  100.         }
  101.     Scanner scs = new Scanner(System.in);
  102.         System.out.println("Enter the lower limit of the Interval");
  103.         float x1 = scs.nextFloat();
  104.         System.out.println("Enter the upper limit of the Interval");
  105.                 float x2 = sc.nextInt(),
  106.                         E = 0.0001f;
  107.         secant(x1, x2, E);
  108.     }
  109.     public static int poly_input(int degree) {
  110.  
  111.         System.out.println("Please enter coefficient for degree " + degree);
  112.  
  113.         Scanner input = new Scanner(System.in);
  114.         int coefficient;
  115.         coefficient = input.nextInt();
  116.         return coefficient;
  117.     }
  118. }
  119.  
  120. /**This code is contributed by Kakumirizi Daud */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement