Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package NumericalComputing.FalsePositionMethod;
- import java.util.Scanner;
- @interface DaudKakumirizi
- {
- String name() default " ";
- String ID() default " ";
- }
- @DaudKakumirizi(name = "Kakumirizi Daud", ID = "17/BSS/BU/R/0010")
- @SuppressWarnings("all")
- public class FalsiMethod {
- /** function to find root for **/
- public double f(double x)
- {
- /** make your own function here but accordingly change (s, t) **/
- return Math.cos(x) - x * x * x;
- /**return x * x * x - 3 * x + 4;
- // return Math.cos(x) - 3 * x + 1;
- // return 2 * x - Math.log(x)/Math.log(10) - 7;
- // return x * x - Math.log(x) - 12;
- **/
- }
- /** function to find root **/
- public double findRoot(double s, double t, double e, int m)
- {
- double r = 0.0,fr;
- int n, side = 0;
- /** starting values at endpoints of interval **/
- double fs = f(s);
- double ft = f(t);
- for (n = 0; n < m; n++)
- {
- r = (fs * t - ft * s) / (fs - ft);
- if (Math.abs(t - s) < e * Math.abs(t + s))
- break;
- fr = f(r);
- if (fr * ft > 0)
- {
- /** fr and ft have same sign, copy r to t **/
- t = r;
- ft = fr;
- if (side == -1)
- fs /= 2;
- side = -1;
- }
- else if (fs * fr > 0)
- {
- /** fr and fs have same sign, copy r to s **/
- s = r;
- fs = fr;
- if (side == +1)
- ft /= 2;
- side = +1;
- }
- else
- {
- /** fr * f_ very small (looks like zero) **/
- break;
- }
- }
- return r;
- }
- /** Main function **/
- public static void main(String[] args)
- {
- System.out.println("Implementation of a Falsi Position Method for");
- System.out.println();
- Scanner sc = new Scanner(System.in);
- int highest_degree;
- System.out.println("What is the highest degree of your polynomial? ");
- Scanner input = new Scanner(System.in);
- highest_degree = input.nextInt();
- for (int x = highest_degree; x >= 0; x--) {
- int coeff_deg_x;
- coeff_deg_x = poly_input(x);
- /**System.out.println(coeff_deg_i); */
- }
- FalsiMethod rf = new FalsiMethod();
- /** lower limit **/
- Scanner scs = new Scanner(System.in);
- System.out.println("Please enter the lower limit of the interval: ");
- double s = scs.nextInt();
- /** upper limit **/
- System.out.println("Please enter the upper limit of the interval: ");
- double t = sc.nextInt();
- /** half of upper bound for relative error **/
- double e = 5E-15;
- /** number of iterations **/
- int iterations = 100;
- System.out.println("\nTherefore the Root is ---> "+ rf.findRoot(s, t, e, iterations));
- }
- public static int poly_input(int degree) {
- System.out.println("Please enter coefficient for degree " + degree);
- Scanner input = new Scanner(System.in);
- int coefficient;
- coefficient = input.nextInt();
- return coefficient;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement