Moortiii icon

Tingeling

Moortiii | PRO | 02/21/18 10:37:55 PM UTC | 0 ⭐ | 408 👁️ | Never ⏰ | []
Python |

820 B

|

None

|

0 👍

/

0 👎

# Bruk import math heller enn astropy og masse piss, it has all you need
import math
 
def f(x):
    return x**3 - 3.0
 
def df(x):
    return 3*x**2 
 
 
def newtonRaphson(f, df, x, nmax, tol):
    #x0 er startverdi, nmax er max antall iterasjoner
    #x = x0 - (f(x0)/df(x0))
    
    for i in range(nmax):
        
        x = x - f(x)/(df(x))
        
        if (f(x)==0):
            return x
        elif (df(x)==0):
            break
        
    return x
    
x_start = 2.5
max_iter = 3
eps = 1.E-1
eps2 = 9.E-6
 
root = newtonRaphson(f, df, x_start, max_iter, eps)
 
 
def g(x):
    # Igjen -> math.log(variable, base), se dokumentasjonen
    return 5*x + math.log(x, math.e) - 10000
 
def dg(x):
    return 5 + (1/x)
 
 
print(root)
print(newtonRaphson(g, dg, 1500 , 10, eps2))
 
# Wehuu it works

Comments