The previous examples show that depending on the initial guess, we may arrive at different roots! These are the so-called basins of attraction for each root. Newton’s method jumps around the parameter space, and the increase in residual corresponds to a jump across a basin. This can lead to tempermental (but beautiful) behaviour.
The Newton-Raphson method naturally can handle complex numbers of the form .
import numpy as np
import plotly.graph_objects as go
from scipy.optimize import newton
try:
import executable_engineering as exe
except ImportError:
%pip install -q executable_engineering
import executable_engineering as exeExample: ¶
def f(z):
return z**3 - z
def df(z):
return 3 * z**2 - 1
exe.plot_basins_of_attraction(f, df, known_roots=[1, -1, 0], title='Basins of Attraction for z^3 - z', real_range=(-2, 2), imag_range=(-2, 2), th=0.5)
Example: ¶
Beautiful, but like most beautiful things... often problematic... It implies small changes in initial guesses can find dramatically different roots, and numerical methods are prone to ‘small changes’ due to roundoff error...
def f(z):
return z**3 - 1
def df(z):
return 3 * z**2
rs = np.roots([1, 0, 0, -1])
exe.plot_basins_of_attraction(f, df, known_roots=rs, title='Basins of Attraction for z^3 - 1', real_range=(-1.5, 1.5), imag_range=(-1.5, 1.5), th=1e-3)