SHARE
TWEET

Untitled

a guest May 19th, 2017 41 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import javax.swing.JOptionPane;
  2.  
  3. public class Newton {
  4.     public static  double eps, xo;
  5.     public static final int MAXIT = 200;
  6.     public static  int counter;
  7.     public Newton() {
  8.     }
  9.    
  10.    
  11.     public static void main(String[] args) {
  12.         eps = Double.parseDouble(JOptionPane.showInputDialog("enter eps please:"));
  13.         xo = Double.parseDouble(JOptionPane.showInputDialog("enter first try please:"));
  14.         counter = 0;
  15.         System.out.println(newton(xo));
  16.          System.out.print("found in  " + counter + " eterations");
  17.     }
  18.     public static double newton(double x) {
  19.         double newX = x - (f(x) / df(x));
  20.         if (Math.abs(newX - x)< eps) {
  21.             return newX;
  22.         }
  23.         counter++;
  24.         if (counter == MAXIT) {
  25.             System.out.print("function has no roots");
  26.             return 0.0;
  27.         }
  28.         return newton(newX);
  29.     }
  30.    
  31.     public static double f(double x) {
  32.         return x*x*x - x*x - 2*x +1;
  33.     }
  34.     public static double df(double x) {
  35.         return 3*x*x - 2*x - 2;
  36.     }
  37.    
  38. }
RAW Paste Data
Challenge yourself this year...
Learn something new in 2017
Top