lundi 22 décembre 2014

Newton's method in Python.


def square(x): return x * x


def cube(x): return x * square(x)


def f1(x): return cube(x) - 2 * x - 5


def dfdx1(x): return 3 * square(x) - 2


def f2(x): return cube(x) * cube(x) -2


def dfdx2(x): return 5 * cube(x) * square(x)


return the approximation of f(x) = 0 when 2 consecutive guesses give


same value.



def newton_raphson(x,f,dfdx):
x = float(x)
print x
x1 = x - f(x)/dfdx(x)
while ????:
print x1
x2 = x1 - f(x1)/dfdx(x1)
x1 = x2


I am quite new to programming and quite unsure how to make this one work :( i have followed a tutorial on defining functions, but the body, i just cant seen to get it to give me the desired result, i know there are other people who have posted on here about newtons method, but i don't want to copy their code.





Aucun commentaire:

Enregistrer un commentaire