return fval, fjac

problem_as_zero = NLP(f, xinit)

''' Compute fixed-point using Newton method '''
t0 = tic()
x1 = problem_as_zero.newton()
t1 = 100 * toc(t0)
n1 = problem_as_zero.fnorm

''' Compute fixed-point using Broyden method '''
t0 = tic()
x2 = problem_as_zero.broyden()
t2 = 100 * toc(t0)
n2 = problem_as_zero.fnorm

''' Compute fixed-point using function iteration '''
t0 = tic()
x3 = problem_as_fixpoint.fixpoint()
t3 = 100 * toc(t0)
n3 = np.linalg.norm(problem_as_fixpoint.fx - x3)


print('Hundredths of seconds required to compute fixed-point of g(x)=sqrt(x)')
print('using Newton, Broyden, and function iteration methods, starting at')
print('x = %4.2f\n' % xinit)
print('Method       Time   Norm of f         x\n', '-' * 40)
ff = '%9s %8.2f    %8.0e     %5.2f'
print(ff % ('Newton', t1, n1, x1))
print(ff % ('Broyden', t2, n2, x2))
print(ff % ('Function', t3, n3, x3))
示例#2
0
def f(x):
    fval = x - np.sqrt(x)
    fjac = 1 - 0.5 / np.sqrt(x)
    return fval, fjac


problem_as_zero = NLP(f, xinit)
''' Compute fixed-point using Newton method '''
t0 = tic()
x1 = problem_as_zero.newton()
t1 = 100 * toc(t0)
n1 = problem_as_zero.fnorm
''' Compute fixed-point using Broyden method '''
t0 = tic()
x2 = problem_as_zero.broyden()
t2 = 100 * toc(t0)
n2 = problem_as_zero.fnorm
''' Compute fixed-point using function iteration '''
t0 = tic()
x3 = problem_as_fixpoint.fixpoint()
t3 = 100 * toc(t0)
n3 = np.linalg.norm(problem_as_fixpoint.fx - x3)

print('Hundredths of seconds required to compute fixed-point of g(x)=sqrt(x)')
print('using Newton, Broyden, and function iteration methods, starting at')
print('x = %4.2f\n' % xinit)
print('Method       Time   Norm of f         x\n', '-' * 40)
ff = '%9s %8.2f    %8.0e     %5.2f'
print(ff % ('Newton', t1, n1, x1))
print(ff % ('Broyden', t2, n2, x2))
print(ff % ('Function', t3, n3, x3))
示例#3
0
__author__ = 'Randall'

import numpy as np
from numpy import log, exp, sqrt
from scipy.stats import norm as Normal_distribution
from compecon import NLP, MCP, LCP
from compecon.tools import example, exercise
''' Example page 32 '''
example(32)
f = NLP(lambda x: x**3 - 2)
x = f.bisect(1, 2)
print('x = ', x)
''' Example page 33 '''
example(33)
g = NLP(lambda x: x**0.5)
x = g.fixpoint(0.4)
print('x = ', x)
''' Example page 35 '''
example(35)


def cournot(q):
    c = np.array([0.6, 0.8])
    eta = 1.6
    e = -1 / eta
    s = q.sum()
    fval = s**e + e * s**(e - 1) * q - c * q
    fjac = e*s**(e-1) * np.ones([2,2]) + e * s ** (e-1) * np.identity(2) +\
           (e-1)*e*s **(e-2)* np.outer(q, [1,1]) - np.diag(c)
    return fval, fjac
problem_as_zero = NLP(f, maxit=1500)

'''% Randomly generate starting point'''
xinit = np.random.randn(2)

''' Compute fixed-point using Newton method '''
t0 = tic()
z1 = problem_as_zero.newton(xinit)
t1 = 100 * toc(t0)
n1 = problem_as_zero.fnorm

''' Compute fixed-point using Broyden method '''
t0 = tic()
z2 = problem_as_zero.broyden(xinit)
t2 = 100 * toc(t0)
n2 = problem_as_zero.fnorm

''' Compute fixed-point using function iteration '''
t0 = tic()
z3 = problem_as_fixpoint.fixpoint(xinit)
t3 = 100 * toc(t0)
n3 = np.linalg.norm(problem_as_fixpoint.fx - z3)

''' Print table header '''
print('Hundredths of seconds required to compute fixed-point of g(x1,x2)=[x1^2+x2^3;x1*x2-0.5]')
print('using Newton, Broyden, and function iteration methods, starting at')
print('x1 = {:4.2f}  x2 = {:4.2f}\n\n'.format(*xinit))
print('Method       Time   Norm of f         x1       x2\n', '-' * 45)
print('Newton   {:8.2f}    {:8.0e}     {:5.2f}     {:5.2f}'.format(t1, n1, *z1))
print('Broyden  {:8.2f}    {:8.0e}     {:5.2f}     {:5.2f}'.format(t2, n2, *z2))
print('Function {:8.2f}    {:8.0e}     {:5.2f}     {:5.2f}'.format(t3, n3, *z3))