Hosted by CoCalc
Download
import numpy import matplotlib
%python def f(x): return numpy.array([x[0]**2 + x[1]**2 - 1, x[0] - x[1]]) def J(x): return numpy.matrix([[2*x[0], 2*x[1]],[1,-1]]) x1 = numpy.linspace(-2,2,100) x2 = numpy.linspace(-2,2,100) [xx1,xx2] = numpy.meshgrid(x1,x2) fig = matplotlib.pyplot.figure() matplotlib.pyplot.axis('equal') ax = fig.gca() ax.contour(xx1,xx2,xx1**2 + xx2**2 - 1,colors='k',levels=[0]) ax.contour(xx1,xx2,xx1 - xx2,colors='b',levels=[0]) x = numpy.array([1,0]) converged = False iter = 0 tol = 1e-6 maxit = 2 while not converged: ax.plot(x[0],x[1],marker='o',color='red') p = numpy.linalg.solve(J(x),f(x)) x = x - p iter = iter + 1 converged = (numpy.linalg.norm(p)<tol) or (iter > maxit) print(iter,numpy.linalg.norm(p)) matplotlib.pyplot.show()
(-0.059999999999999998, 0.059999999999999998, -0.059999999999999998, 0.059999999999999998) <matplotlib.contour.QuadContourSet object at 0x7fd1b38a3810> <matplotlib.contour.QuadContourSet object at 0x7fd1b37ce650> [<matplotlib.lines.Line2D object at 0x7fd1b37ebcd0>] (1, 1.0) [<matplotlib.lines.Line2D object at 0x7fd1b37ce650>] (2, 0.35355339059327379) [<matplotlib.lines.Line2D object at 0x7fd1b37ce710>] (3, 0.05892556509887896)
%python def f(x): return numpy.array([x[0]**2 + x[1]**2 - 2, x[0] - x[1], x[0] + x[1] - 2]) def J(x): return numpy.matrix([[2*x[0], 2*x[1]],[1,-1],[1,1]]) x1 = linspace(-2,2,100) x2 = linspace(-2,2,100) s = linspace(-.1,.1,10) [xx1,xx2] = numpy.meshgrid(x1,x2) fig = matplotlib.pyplot.figure() matplotlib.pyplot.axis('equal') ax = fig.gca() ax.contour(xx1,xx2,xx1**2 + xx2**2 - 2,colors='k',levels=[0]) ax.contour(xx1,xx2,xx1 - xx2,colors='b',levels=[0]) ax.contour(xx1,xx2,xx1 + xx2 - 2,colors='g',levels=[0]) x = numpy.array([1,0]) converged = False iter = 0 tol = 1e-10 maxit = 10 while not converged: a = ax.plot(x[0],x[1],marker='o',color='red') p = linalg.lstsq(J(x),f(x))[0] x = x - p iter = iter + 1 converged = (linalg.norm(p)<tol) or (iter > maxit) print(iter,linalg.norm(p)) matplotlib.pyplot.show()
(-0.059999999999999998, 0.059999999999999998, -0.059999999999999998, 0.059999999999999998) <matplotlib.contour.QuadContourSet object at 0x7f9eb7c9b8d0> <matplotlib.contour.QuadContourSet object at 0x7f9eb4a82ad0> <matplotlib.contour.QuadContourSet object at 0x7f9eb777f250> (1, 1.0540925533894594) (2, 0.31119636958012253) (3, 0.028027720048995688) (4, 0.000223025653887907) (5, 1.406916519038287e-08) (6, 0.0)
%python def f(x): return numpy.array([x[0]**2 + x[1]**2 - 2]) def J(x): return numpy.matrix([[2*x[0], 2*x[1]]]) x1 = linspace(-2,2,100) x2 = linspace(-2,2,100) [xx1,xx2] = numpy.meshgrid(x1,x2) fig = matplotlib.pyplot.figure() ax = fig.gca() matplotlib.pyplot.axis('equal') ax.contour(xx1,xx2,xx1**2 + xx2**2 - 2,colors='k',levels=[0]) x = numpy.array([1,0.5]) converged = False iter = 0 tol = 1e-10 maxit = 10 while not converged: a = ax.plot(x[0],x[1],marker='o',color='red') p = linalg.lstsq(J(x),f(x))[0] x = x - p iter = iter + 1 converged = (linalg.norm(p)<tol) or (iter > maxit) print(iter,linalg.norm(p)) matplotlib.pyplot.show()
(-0.059999999999999998, 0.059999999999999998, -0.059999999999999998, 0.059999999999999998) <matplotlib.contour.QuadContourSet object at 0x7f9eb4a82ad0> (1, 0.33541019662496846) (2, 0.038701176533650287) (3, 0.00052934739939640679) (4, 9.9068718271003633e-08) (5, 3.4542034091042956e-15)
%python def f(x): return numpy.array([x[0]**2 + x[1]**2 - 2, x[0] - x[1], x[0] - 1.2]) def J(x): return numpy.matrix([[2*x[0], 2*x[1]],[1,-1],[1,0]]) x1 = linspace(-2,2,100) x2 = linspace(-2,2,100) s = linspace(-.1,.1,10) [xx1,xx2] = numpy.meshgrid(x1,x2) fig = matplotlib.pyplot.figure() matplotlib.pyplot.axis('equal') ax = fig.gca() ax.contour(xx1,xx2,xx1**2 + xx2**2 - 2,colors='k',levels=[0]) ax.contour(xx1,xx2,xx1 - xx2,colors='b',levels=[0]) ax.contour(xx1,xx2,xx1 - 1.2,colors='g',levels=[0]) x = numpy.array([1,0]) converged = False iter = 0 tol = 1e-10 maxit = 10 while not converged: a = ax.plot(x[0],x[1],marker='o',color='red') p = linalg.lstsq(J(x),f(x))[0] x = x - p iter = iter + 1 converged = (linalg.norm(p)<tol) or (iter > maxit) print(iter,linalg.norm(p)) matplotlib.pyplot.show()
(-0.059999999999999998, 0.059999999999999998, -0.059999999999999998, 0.059999999999999998) <matplotlib.contour.QuadContourSet object at 0x7f9eb6144c50> <matplotlib.contour.QuadContourSet object at 0x7f9eb758fed0> <matplotlib.contour.QuadContourSet object at 0x7f9eb70eb790> (1, 1.5057224179774977) (2, 0.52405083388102425) (3, 0.086759087166500354) (4, 0.001773477548524111) (5, 1.4448587341850866e-05) (6, 1.2640024582108905e-07) (7, 1.1207583193209142e-09) (8, 1.1556340073250288e-11)