Homepage › Solution manuals › Gilbert Strang › Linear Algebra and Learning from Data › Exercise 6.1.14
Exercise 6.1.14
Answers
For a system of equations, e.g.
If we want to find the roots for the two functions, assuming is our initial guess, approximate the equation with Taylor expansion we have
Written in matrix format we have
As we are seeking the roots of functions , if is the point we are looking for, set , we have
Apply the formula to the equations in problem 14, we compute the Jacobian matrix first:
Thus
import numpy as np import math def newton_m(f, J, x0, maxit=500): x = x0 xs, ys = [x0], [f(x0)] try: for it in np.arange(maxit): J1 = np.linalg.inv(J(x)) x = x - np.matmul(J1, f(x)) if np.linalg.norm(f(x)) <= 1.0e-12: #print(’Find the root:’, x, f(x), ’ at iteration: ’, it) break xs.append(x) ys.append(f(x)) except Exception as e: return x, np.array([math.inf, math.inf]), None, None, it if it >= maxit: print("Exhausted␣all␣", it, "␣iterations.") return x, f(x), xs, ys, it def f14(x): u = x[0] v = x[1] res = np.array([u**3 -v, v**3 - u]) return res def J14(x): u = x[0] v = x[1] res = np.array([[3*u**2, -1], [-1, 3*v**2]]) return res print("####␣Problem␣VI.1.14") solutions = [np.array([0,0]), np.array([1,1]), np.array([-1, -1]), np.array([math.inf, math.inf])] data_types = {0:[], 1:[], 2:[], 3:[]} xa = np.arange(-1, 1, 0.01) ya = np.arange(-1, 1, 0.01) #xv, yv = np.meshgrid(xa, ya) for i,x in enumerate(xa): for j, y in enumerate(ya): x0 = (x,y) #print("With initial values: ", x0) v, fv, xs, ys, it = newton_m(f14, J14, x0) for idx, sol in enumerate(solutions): if np.isinf(fv[0]) and np.isinf(fv[1]): data_types[3].append(x0) elif np.allclose(v, sol): data_types[idx].append(x0) #data_types
\#\#\#\# Problem VI.1.14
\ipykernel_launcher.py:26: RuntimeWarning: overflow encountered in double_scalars \ipykernel_launcher.py:11: RuntimeWarning: overflow encountered in matmul # This is added back by InteractiveShellApp.init_path() \ipykernel_launcher.py:11: RuntimeWarning: invalid value encountered in matmul # This is added back by InteractiveShellApp.init_path() \ipykernel_launcher.py:32: RuntimeWarning: overflow encountered in double_scalars \ipykernel_launcher.py:26: RuntimeWarning: invalid value encountered in double_scalars
import matplotlib.pyplot as plt colors = [’y’,’g’,’b’, ’r’] # (0,0), (1,1), (-1,-1), (infty, infty) for k, x0s in data_types.items(): #print(x0s) xs = [x for x,y in x0s] ys = [y for x,y in x0s] ys = np.array(ys) plt.scatter(xs, ys, c = colors[k], marker=’.’, s = 0.6)