Don't like ads? PRO users don't see any ads ;-)
Guest

Metoda Newtona

By: Mistic92 on May 8th, 2012  |  syntax: Java  |  size: 0.68 KB  |  hits: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package numeryczne;
  2.  
  3. import java.lang.System;
  4. public class styczne {
  5.    
  6.     static double f(double x)
  7.     {
  8.         return x*x-1;
  9.     }
  10.     static double fpoch(double x)
  11.     {
  12.         return 2*x;
  13.     }
  14.  
  15.     public static void main(String[]args)
  16.     {
  17.       long time1 = System.nanoTime();
  18.         double a=-0.5,b=3,eps=0.000001,x=a;
  19.         int l=0;
  20.        
  21.           while (Math.abs(f(x)) > eps || x>3 )
  22.            {
  23.             x=x -(f(x)/Math.abs(fpoch(x)));
  24.               l++;
  25.            }//while  
  26.      long time2 = System.nanoTime();
  27.      long wynik = time2-time1;
  28. System.out.println("Wykonałem "+l+" operacji a x wyniósł "+x+" w czasie: "+wynik);
  29.     }
  30. }