Newton Raphson Method in Python - Numerical Methods

Описание к видео Newton Raphson Method in Python - Numerical Methods

Please don't forget to include the greater than/smaller than symbols in the while loop (as shown in the video) as Youtube doesn't allow me to put any 'angled brackets'.


Code:


#Problem ... find the roots of the function x2 + 2x - 15 using the Newton Raphson Method


def f(x):
return x**2 + 2*x -15
def df(x):
return 2*x + 2


#Parameters
TOL = 1.0e-10
MAX_ITER=10
itr = 0
x=-2 # initial guess
result = f(x)


#Main program


while abs(result) TOL and itr MAX_ITER:
itr+=1
result = f(x)
print('iteration',itr,'result',result)
x = x- result/df(x) #NR Formula

if abs(result) TOL:
print('Converged to',x)

else:
print('Did not converge')

Комментарии

Информация по комментариям в разработке