fork(1) download
  1. # your code goes here
  2. import time
  3. from pylab import*
  4.  
  5. def newton (f, fder, a, N=1000, to1=1e-10):
  6. i=0
  7. c=1
  8. while i<N and abs (f(c)) >= to1:
  9. c = a - f(a)/fder(a)
  10. a=c
  11. i+=1
  12. return c
  13.  
  14. def finntid_og_nullpunkt (f, fder, an):
  15. tid = 0
  16. for i in range (5):
  17. tid_start = time.time()
  18. newton (f, fder, an)
  19. tid_slutt = time.time()
  20. tid += tid_slutt - tid_start
  21. tid /= 5
  22.  
  23. nullpunkt = newton (f, fder, an)
  24.  
  25. print ("Newtons metode:", nullpunkt)
  26.  
  27. def f(x):
  28. return x - 3
  29.  
  30. def fder (x):
  31. return 1
  32.  
  33. an = 1
  34. finntid_og_nullpunkt(f, fder, an)
Success #stdin #stdout #stderr 0.75s 67196KB
stdin
Standard input is empty
stdout
Newtons metode: 3.0
stderr
Unable to init server: Could not connect: Connection refused
Unable to init server: Could not connect: Connection refused

(prog:19117): Gdk-CRITICAL **: 12:08:29.323: gdk_cursor_new_for_display: assertion 'GDK_IS_DISPLAY (display)' failed

(prog:19117): Gdk-CRITICAL **: 12:08:29.324: gdk_cursor_new_for_display: assertion 'GDK_IS_DISPLAY (display)' failed