Moortiii

Tingeling

Feb 21st, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. # Bruk import math heller enn astropy og masse piss, it has all you need
  2. import math
  3.  
  4. def f(x):
  5.     return x**3 - 3.0
  6.  
  7. def df(x):
  8.     return 3*x**2
  9.  
  10.  
  11. def newtonRaphson(f, df, x, nmax, tol):
  12.     #x0 er startverdi, nmax er max antall iterasjoner
  13.     #x = x0 - (f(x0)/df(x0))
  14.    
  15.     for i in range(nmax):
  16.        
  17.         x = x - f(x)/(df(x))
  18.        
  19.         if (f(x)==0):
  20.             return x
  21.         elif (df(x)==0):
  22.             break
  23.        
  24.     return x
  25.    
  26. x_start = 2.5
  27. max_iter = 3
  28. eps = 1.E-1
  29. eps2 = 9.E-6
  30.  
  31. root = newtonRaphson(f, df, x_start, max_iter, eps)
  32.  
  33.  
  34. def g(x):
  35.     # Igjen -> math.log(variable, base), se dokumentasjonen
  36.     return 5*x + math.log(x, math.e) - 10000
  37.  
  38. def dg(x):
  39.     return 5 + (1/x)
  40.  
  41.  
  42. print(root)
  43. print(newtonRaphson(g, dg, 1500 , 10, eps2))
  44.  
  45. # Wehuu it works
Advertisement
Add Comment
Please, Sign In to add comment