Newton-Raphson method together with Horner

Asked

Viewed 324 times

2

I’m having trouble implementing Horner’s method to decrease the computational cost of polynomial calculations and, having done so, using Newton’s method to find the root of the polynomial

In the code, b would store the value of the polynomial and c the value of the derivative

n=len(a)-1
print("k\ta\tx\tpx")
for k in range (1, iterMax):
    b=a[n] 
    c=0
    for i in range (n-1,1):
        c=b-(x*c)
        b=(x*b)+a[i]
    b=x*b+a[0]
    if (b<epsilon and b>-epsilon):
        print ("raiz encontrada: ")
        print (x)
        return (0, x)
    x=x-(b/c)   
print("erro. Maximo de interações atingido") 
return (1, x)

When applying the Newton method (x=x-(b/c)), it accuses error saying that I am dividing by zero, but I do not understand why.

Function call:

a = [3, -9, 0, 1]
x0 = 0.5
epsilon = 0.0001
maxIter = 20
(houveErro, raiz) = newton_poli(a, x0, epsilon, maxIter)

Note: from what I understand, the list a stores the coefficients backwards, that is, 3 is the independent term and 1 is the term that multiplies x³

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.