Advertisement
kdaud

Falsi Position Method

Mar 8th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. package NumericalComputing.FalsePositionMethod;
  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 FalsiMethod {
  14.     /** function to find root for **/
  15.     public double f(double x)
  16.     {
  17.         /** make your own function here but accordingly change (s, t) **/
  18.  
  19.         return Math.cos(x) - x * x * x;
  20.  
  21.         /**return x * x * x - 3 * x + 4;
  22.         // return Math.cos(x) - 3 * x + 1;
  23.         // return 2 * x - Math.log(x)/Math.log(10) - 7;
  24.         // return x * x - Math.log(x) - 12;
  25.          **/
  26.     }
  27.     /** function to find root **/
  28.     public double findRoot(double s, double t, double e, int m)
  29.     {
  30.         double r = 0.0,fr;
  31.         int n, side = 0;
  32.  
  33.         /** starting values at endpoints of interval **/
  34.         double fs = f(s);
  35.         double ft = f(t);
  36.  
  37.         for (n = 0; n < m; n++)
  38.         {
  39.  
  40.             r = (fs * t - ft * s) / (fs - ft);
  41.             if (Math.abs(t - s) < e * Math.abs(t + s))
  42.                 break;
  43.             fr = f(r);
  44.  
  45.             if (fr * ft > 0)
  46.             {
  47.                 /** fr and ft have same sign, copy r to t **/
  48.                 t = r;
  49.                 ft = fr;
  50.                 if (side == -1)
  51.                     fs /= 2;
  52.                 side = -1;
  53.             }
  54.             else if (fs * fr > 0)
  55.             {
  56.                 /** fr and fs have same sign, copy r to s **/
  57.                 s = r;
  58.                 fs = fr;
  59.                 if (side == +1)
  60.                     ft /= 2;
  61.                 side = +1;
  62.             }
  63.             else
  64.             {
  65.                 /** fr * f_ very small (looks like zero) **/
  66.                 break;
  67.             }
  68.         }
  69.         return r;
  70.     }
  71.     /** Main function **/
  72.     public static void main(String[] args)
  73.     {
  74.         System.out.println("Implementation of  a Falsi Position Method  for");
  75.         System.out.println();
  76.         Scanner sc = new Scanner(System.in);
  77.         int highest_degree;
  78.         System.out.println("What is the highest degree of your polynomial? ");
  79.         Scanner input = new Scanner(System.in);
  80.         highest_degree = input.nextInt();
  81.         for (int x = highest_degree; x >= 0; x--) {
  82.             int coeff_deg_x;
  83.             coeff_deg_x = poly_input(x);
  84.             /**System.out.println(coeff_deg_i); */
  85.         }
  86.  
  87.         FalsiMethod rf = new FalsiMethod();
  88.         /** lower limit **/
  89.         Scanner scs = new Scanner(System.in);
  90.         System.out.println("Please enter the lower limit of the interval: ");
  91.         double s = scs.nextInt();
  92.  
  93.         /** upper limit **/
  94.         System.out.println("Please enter the upper limit of the interval: ");
  95.         double t = sc.nextInt();
  96.         /** half of upper bound for relative error **/
  97.         double e = 5E-15;
  98.         /** number of iterations **/
  99.         int iterations = 100;
  100.  
  101.         System.out.println("\nTherefore the Root is ---> "+ rf.findRoot(s, t, e, iterations));
  102.     }
  103.     public static int poly_input(int degree) {
  104.  
  105.         System.out.println("Please enter coefficient for degree " + degree);
  106.  
  107.         Scanner input = new Scanner(System.in);
  108.         int coefficient;
  109.         coefficient = input.nextInt();
  110.         return coefficient;
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement