I'm having trouble implementing Horner's method to decrease the computational cost of polynomial calculations, and doing so use 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 you apply Newton's method (x = x- (b / c)), you acknowledge 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: as I understand it, the list stores the coefficients backwards, that is, 3 is the independent term and 1 is the multiplying term x³