Advertisement
yloplopy

Newton's Method 2.166

Apr 23rd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.52 KB | None | 0 0
  1.     public static void main(String[] args) {
  2.         NewtonsMethod();
  3.     }
  4.    
  5.     private static void NewtonsMethod() {
  6.         double g = 2.0;
  7.         double tol = Math.pow(10F, -12);
  8.        
  9.         double old = g+1F;
  10.         int counter = 0;
  11.         while(Math.abs(g-old) > tol) {
  12.             old = g;
  13.             g = old - (f(old)/df(old));
  14.             counter++;
  15.             System.out.println("Iteration " + counter + ": " + g);
  16.         }
  17.     }
  18.    
  19.     private static double f(double x) {
  20.         return Math.pow(Math.E, x)-3F*x-1;
  21.     }
  22.    
  23.     private static double df(double x) {
  24.         return Math.pow(Math.E, x)-3F;
  25.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement