Advertisement
alvinfnaldi

Secant

Apr 2nd, 2021
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. package metodenumerik;
  2.  
  3. /**
  4. * @author Kelompok3_RK_3IA14
  5. **/
  6. public class Secant {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. float xn2 = 2;
  11. float xn1 = 1;
  12. float x = 9999;
  13. float selisih = x - xn1;
  14. float error = 0;
  15. String prefix1 = "X";
  16. String prefix2 = "Selisih";
  17. String format = "%-14s%s%n";
  18.  
  19. System.out.printf(format, prefix1, " | " + prefix2);
  20. System.out.println("-----------------------------");
  21. System.out.printf(format, xn2, " | " + 0);
  22. System.out.printf(format, xn1, " | " + selisih);
  23.  
  24. while(Math.abs(selisih) > error){
  25.  
  26. x = xn1 - ((fungsi(xn1) * (xn1-xn2)) / (fungsi(xn1) - fungsi(xn2)));
  27.  
  28. selisih = x - xn1;
  29. System.out.printf(format, x, " | " + selisih);
  30.  
  31. xn2 = xn1;
  32. xn1 = x;
  33.  
  34. }
  35. String prefix3 = "Akar Persamaan";
  36. String prefix4 = "Error";
  37. float absolute = Math.abs(selisih);
  38. System.out.println("-----------------------------");
  39. System.out.printf(format, prefix3, " : " + x);
  40. System.out.printf(format, prefix4, " : " + absolute);
  41.  
  42. }
  43.  
  44. public static float fungsi(float x){
  45. //fungsi ini bisa diganti dengan fungsi apa pun
  46. float y = (x*x)-(7*x)-7;
  47. return y;
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement