Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) {
- Secant s = new Secant();
- Function x2 = new X2();//Creating a reference of the interface Function
- //(x2) that is pointing to an object of X2.
- System.out.println("Testing the secant-method for x^2-6");
- //According to Wolfram Alpha the root for "x^2-6" should be 0.774597.
- //Since we have eps= 10e-6, we should have 5 correctly decimals
- //(10^(1)*10^(-6)=10^(-5)).
- double rot = s.secant(x2, 0, 4, 10e-6);
- System.out.println("Roten för " + x2 + " är " + rot);
- double realRoot = 0.77459;
- if(realRoot <= rot + 10e-6 && realRoot >= rot - 10e-6){//Testing that
- //the realRoot is within the interval of our approximated root +/-
- //eps (10e-6)
- System.out.println("Test successful!\n");
- } else {
- System.out.println("Test failed\n");
- }
- //We can do the same with these functions to test that the root is
- //calculated correctly
- Function arctan = new Arctan();
- double rot2 = s.secant(arctan, -1, 1, 10e-6);
- System.out.println("Roten för " + arctan + " är " + rot2);
- Function sin = new Sin();
- double rot3 = s.secant(sin, -1, 1, 10e-6);
- System.out.println("Roten för " + sin + " är " + rot3);
- Function ln = new Ln();
- double rot4 = s.secant(ln, 0.3, 1, 10e-6);
- System.out.println("Roten för " + ln + " är " + rot4);
- }
- }
Add Comment
Please, Sign In to add comment