Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- #Newton Raphson metode
- def derivertavX(f, x):
- return abs(f(x))
- def NewtonRaphson (f ,derivertavF, start, e):
- stigning = derivertavX(f, start)
- while stigning > e:
- start = start - ((f(start))/(derivertavF(start)))
- stigning = derivertavX(f, start)
- return start
- #oppgave1
- def f(x):
- return x**3-3
- def derivertavF(x):
- return 3*x**2
- starts = [2.5]
- for start in starts:
- xvariable = NewtonRaphson(f, derivertavF, 2.5, 2e-1)
- print (xvariable)
- #oppgave 2
- def g(x):
- return (5*x + math.log(x)-10000)
- def derivertavG(x):
- return (5+ 1/x)
- starts = [1,2000]
- for start in starts:
- x2variable = NewtonRaphson(g, derivertavG, start, 1e-6)
- print (x2variable)
- #oppgave 3
- def y(x):
- return 2 - x**2 - math.sin(x)
- def derivertavY(x):
- return -2*x - math.cos(x)
- def NewtonRaphson2(f, derivertavF, start):
- start = start - ((f(start)) / (derivertavF(start)))
- return start
- start = 0
- for val in range(5):
- newX = NewtonRaphson2(y, derivertavY, start)
- start = newX
- print(start)
- start = -2
- for val2 in range(5):
- newX = NewtonRaphson2(y, derivertavY, start)
- start = newX
- print(start)
- #oppgave 4
- def z(x):
- return x**2 - 10
- def derivertavZ(x):
- return 2*x
- for val in range(3):
- x4variable = NewtonRaphson(z, derivertavZ, start, 1.E-08)
- start = x4variable
- x4variable = abs(x4variable)
- print (x4variable)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement