Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. import math
  2.  
  3.  
  4. #Newton Raphson metode
  5. def derivertavX(f, x):
  6.     return abs(f(x))
  7.  
  8. def NewtonRaphson (f ,derivertavF, start, e):
  9.  
  10.  
  11.     stigning = derivertavX(f, start)
  12.  
  13.     while stigning > e:
  14.  
  15.         start = start - ((f(start))/(derivertavF(start)))
  16.         stigning = derivertavX(f, start)
  17.        
  18.     return start
  19.  
  20.  
  21. #oppgave1
  22. def f(x):
  23.     return x**3-3
  24.  
  25. def derivertavF(x):
  26.     return 3*x**2
  27.  
  28. starts = [2.5]
  29. for start in starts:
  30.     xvariable = NewtonRaphson(f, derivertavF, 2.5, 2e-1)
  31.     print (xvariable)
  32.  
  33. #oppgave 2
  34.  
  35. def g(x):
  36.     return (5*x + math.log(x)-10000)
  37.  
  38. def derivertavG(x):
  39.     return (5+ 1/x)
  40.  
  41. starts = [1,2000]
  42. for start in starts:
  43.     x2variable = NewtonRaphson(g, derivertavG, start, 1e-6)
  44.  
  45. print (x2variable)
  46.  
  47. #oppgave 3
  48.  
  49.  
  50. def y(x):
  51.     return 2 - x**2 - math.sin(x)
  52.  
  53.  
  54. def derivertavY(x):
  55.     return -2*x - math.cos(x)
  56.  
  57.  
  58. def NewtonRaphson2(f, derivertavF, start):
  59.  
  60.     start = start - ((f(start)) / (derivertavF(start)))
  61.  
  62.     return start
  63.  
  64. start = 0
  65.  
  66. for val in range(5):
  67.     newX = NewtonRaphson2(y, derivertavY, start)
  68.     start = newX
  69.  
  70. print(start)
  71.  
  72.  
  73.  
  74. start = -2
  75. for val2 in range(5):
  76.     newX = NewtonRaphson2(y, derivertavY, start)
  77.     start = newX
  78.  
  79. print(start)
  80.  
  81.  
  82.  
  83.  
  84.  
  85. #oppgave 4
  86.  
  87. def z(x):
  88.     return x**2 - 10
  89.  
  90. def derivertavZ(x):
  91.     return 2*x
  92.  
  93. for val in range(3):
  94.     x4variable = NewtonRaphson(z, derivertavZ, start, 1.E-08)
  95.     start = x4variable
  96.  
  97. x4variable = abs(x4variable)
  98. print (x4variable)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement