""" from demos.setup import np, plt, tic, toc from numpy.linalg import norm from compecon import NLP ''' Set up the problem ''' def f(x): fval = np.exp(-x) - 1 fjac = -np.exp(-x) return fval, fjac problem = NLP(f, all_x=True) ''' Randomly generate starting point ''' problem.x0 = 10 * np.random.randn(1) ''' Compute root using Newton method ''' t0 = tic() x1 = problem.newton() t1 = 100 * toc(t0) n1, x_newton = problem.fnorm, problem.x_sequence ''' Compute root using Broyden method ''' t0 = tic() x2 = problem.broyden() t2 = 100 * toc(t0) n2, x_broyden = problem.fnorm, problem.x_sequence
""" from demos.setup import np, plt, tic, toc from numpy.linalg import norm from compecon import NLP ''' Set up the problem ''' def f(x): fval = np.exp(-x) - 1 fjac = -np.exp(-x) return fval, fjac problem = NLP(f, all_x=True) ''' Randomly generate starting point ''' problem.x0 = 10 * np.random.randn(1) ''' Compute root using Newton method ''' t0 = tic() x1 = problem.newton() t1 = 100 * toc(t0) n1, x_newton = problem.fnorm, problem.x_sequence ''' Compute root using Broyden method ''' t0 = tic() x2 = problem.broyden() t2 = 100 * toc(t0) n2, x_broyden = problem.fnorm, problem.x_sequence ''' Print results ''' print('Hundredths of seconds required to compute root of exp(-x)-1,') print('via Newton and Broyden methods, starting at x = %4.2f.' % problem.x0) print('\nMethod Time Norm of f Final x') print('Newton %8.2f %8.0e %5.2f' % (t1, n1, x1))
from compecon import NLP ''' Set up the problem ''' def f(x): fval = [200 * x[0] * (x[1] - x[0] ** 2) + 1 - x[0], 100 * (x[0] ** 2 - x[1])] fjac = [[200 * (x[1] - x[0] ** 2) - 400 * x[0] ** 2 - 1, 200 * x[0]], [200 * x[0], -100]] return np.array(fval), np.array(fjac) problem = NLP(f) ''' Randomly generate starting point ''' problem.x0 = np.random.randn(2) ''' Compute root using Newton method ''' t0 = tic() x1 = problem.newton() t1 = 100 * toc(t0) n1 = problem.fnorm '''Compute root using Broyden method ''' t0 = tic() x2 = problem.broyden() t2 = 100 * toc(t0) n2 = problem.fnorm ''' Print results '''