Guest User

Untitled

a guest
Apr 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. public class Main {
  2.     public static void main(String[] args) {
  3.         Secant s = new Secant();
  4.        
  5.         Function x2 = new X2();//Creating a reference of the interface Function
  6.                                 //(x2) that is pointing to an object of X2.
  7.         System.out.println("Testing the secant-method for x^2-6");
  8.         //According to Wolfram Alpha the root for "x^2-6" should be 0.774597.
  9.         //Since we have eps= 10e-6, we should have 5 correctly decimals
  10.         //(10^(1)*10^(-6)=10^(-5)).
  11.         double rot = s.secant(x2, 0, 4, 10e-6);
  12.         System.out.println("Roten för " + x2 + " är " + rot);
  13.        
  14.         double realRoot = 0.77459;
  15.         if(realRoot <= rot + 10e-6 && realRoot >= rot - 10e-6){//Testing that
  16.             //the realRoot is within the interval of our approximated root +/-
  17.             //eps (10e-6)
  18.             System.out.println("Test successful!\n");
  19.         } else {
  20.             System.out.println("Test failed\n");
  21.         }
  22.        
  23.         //We can do the same with these functions to test that the root is
  24.         //calculated correctly
  25.         Function arctan = new Arctan();
  26.         double rot2 = s.secant(arctan, -1, 1, 10e-6);
  27.         System.out.println("Roten för " + arctan + " är " + rot2);
  28.  
  29.         Function sin = new Sin();
  30.         double rot3 = s.secant(sin, -1, 1, 10e-6);
  31.         System.out.println("Roten för " + sin + " är " + rot3);
  32.        
  33.         Function ln = new Ln();
  34.         double rot4 = s.secant(ln, 0.3, 1, 10e-6);
  35.         System.out.println("Roten för " + ln + " är " + rot4);
  36.  
  37.     }
  38.  
  39. }
Add Comment
Please, Sign In to add comment