import numpy as np
import numpy.linalg as la
def f(xvec):
x, y = xvec
return np.array([
x + 2*y -2,
x**2 + 4*y**2 - 4
])
def Jf(xvec):
x, y = xvec
return np.array([
[1, 2],
[2*x, 8*y]
])
Pick an initial guess.
x = np.array([1, 2])
Now implement Newton's method.
x = x - la.solve(Jf(x), f(x))
print(x)
Check if that's really a solution:
f(x)
Let's keep an error history and check.
xtrue = np.array([0, 1])
errors = []
x = np.array([1, 2])
x = x - la.solve(Jf(x), f(x))
errors.append(la.norm(x-xtrue))
print(x)
for e in errors:
print(e)
for i in range(len(errors)-1):
print(errors[i+1]/errors[i]**2)