Exercise 6.1.14

Answers

For a system of equations, e.g. 

f1(x,y) = 0 f2(x,y) = 0

If we want to find the roots (x,y) for the two functions, assuming (x0,y0) is our initial guess, approximate the equation with Taylor expansion we have

f1(x,y) = f1(x0,y0) + f1 ∂x |(x0,y0)(x x0) + f1 ∂y |(x0,y0)(y y0) f2(x,y) = f2(x0,y0) + f2 ∂x |(x0,y0)(x x0) + f2 ∂y |(x0,y0)(y y0)

Written in matrix format we have

[f1(x,y) f2(x,y) ] = [f1(x0,y0) f2(x0,y0) ] + [f1 ∂x f1 ∂y f2 ∂x f2 ∂y ] ( [ x y ] [x0 y0 ] )

As we are seeking the roots of functions f1,f2, if (x,y) is the point we are looking for, set f1(x,y) = 0,f2(x,y) = 0, we have

[f1(x0,y0) f2(x0,y0) ] + [f1 ∂x f1 ∂y f2 ∂x f2 ∂y ] ( [ x y ] [x0 y0 ] ) = 0 [x y ] = [x0 y0 ] [f1 ∂x f1 ∂y f2 ∂x f2 ∂y ] 1 [ f1(x0,y0) f2(x0,y0) ] [x y ] = [x0 y0 ] J1 [ f1(x0,y0) f2(x0,y0) ]

Apply the formula to the equations in problem 14, we compute the Jacobian matrix first:

J = [f1 ∂u f1 ∂v f2 ∂u f2 ∂v ] = [3u21 13v2 ]

Thus J1 = 1 9u2v21 [ 3v2 1 1 3u2 ]

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("Exhaustedall", 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("####ProblemVI.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)

PIC

User profile picture
2020-03-20 00:00
Comments