Advertisement
yloplopy

Newton's Method 3.166

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