I have written a python code for a newton method in 1D and want to use it to calculate the newton fractal for the function
The basic python code I am using is this:
error = 1e-10
resolution = 100
x_range = np.linspace(-2,2,resolution)
y_range = np.linspace(-1,1,resolution)
fraktal = np.zeros(shape=(resolution,resolution))
for i in range(resolution):
for j in range(resolution):
x = x_range[i]
y = y_range[j]
z = complex(x,y)
fraktal[i,j] = newton(z,error)
plt.imshow(fraktal)
plt.show()
My newton()-function returns the number of iterations it needed to find an approximation xk such that |f(xk)|<1e-10.
I tested this code with and it worked, but when I use the actual function I would like to use, namely
, I get an Overflow Error, "OverflowError: math range error".
This is my code for the function f:
import cmath as cm
def f(x):
y = pow(x,4)*cm.cos(x) - 1
return y
I don't really know how to debug this. I tried to convert to double precision, but my web research suggested that python is already using double precision. That's about the only idea I have for solving this.
Does anyone have an idea for what to do here? Thanks!
Edit:
def grad(x):
h = 1e-6
y = (f(x+h)-f(x-h))/(2*h)
return y
def newton(x0,error):
k = 1
xk = x0
while 1:
xk = xk - f(xk)/grad(xk)
err = abs(f(xk))
if err < error:
return k
break
if k > 100:
return 100
break
k = k+1