Guest User

Untitled

a guest
Apr 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. /**
  2. * A class containing a method to calculate a root of a given function.
  3. *
  4. * @author Daniel Augurell och Viktor R�dmark
  5. * @group Labbgrupp 4
  6. * @assignement Lab 3 part 2
  7. * @date 2011-09-27
  8. *
  9. */
  10. public class Secant {
  11. /**
  12. * Calculates a root of a given function between two given values.
  13. *
  14. * @param f The function
  15. * @param low The lower limit
  16. * @param high The higher limit
  17. * @param eps The accuracy of the calculation
  18. * @return The x value of the root
  19. */
  20. public static double secant(Function f, double low, double high, double eps) {
  21. double x = (high * f.f(low) - low * f.f(high)) / (f.f(low) - f.f(high));
  22.  
  23. if (f.f(x + eps) * f.f(x - eps) > 0) {
  24. low = (f.f(x) < 0 ? x : low);
  25. high = (f.f(x) >= 0 ? x : high);
  26. x = secant(f, low, high, eps);
  27. }
  28. return x;
  29. }
  30. }
Add Comment
Please, Sign In to add comment