Advertisement
alvinfnaldi

Newton-Raphson

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