Chapter-14 : Multidimensional Unconstrained Optimization

Ex:14.1 Pg: 368

In [7]:
from numpy.random import rand
def f(x,y):
    z=y-x-(2*(x**2))-(2*x*y)-(y**2)
    return z
x1=-2#
x2=2#
y1=1#
y2=3#
fmax=-1*10**(-15)#
n=10000#
for j in range(0,n):
    r=rand(1,2)
    x=x1+(x2-x1)*r[0,0]
    y=y1+(y2-y1)*r[0,1]
    fn=f(x,y)#
    if fn>fmax:
        fmax=fn#
        xmax=x#
        ymax=y#
    
    if j%1000==0:
    
        print "Iteration:",(j+1)
        print "x:",x
        print "y:",y
        print "function value:",fn
        print "------------------------------------------"
    
Iteration: 1
x: 1.53606983054
y: 1.86168445446
function value: -13.5786300816
------------------------------------------
Iteration: 1001
x: 1.76453990766
y: 1.86004762993
function value: -16.155728181
------------------------------------------
Iteration: 2001
x: -0.735800459375
y: 1.48176151402
function value: 1.11970176233
------------------------------------------
Iteration: 3001
x: -0.493690866305
y: 2.74840585243
function value: -2.08537362139
------------------------------------------
Iteration: 4001
x: 0.191618544017
y: 2.12536130424
function value: -3.47137052343
------------------------------------------
Iteration: 5001
x: 0.556097851317
y: 2.75235702552
function value: -9.05885931809
------------------------------------------
Iteration: 6001
x: -1.3315420382
y: 1.98026194153
function value: 1.11796226726
------------------------------------------
Iteration: 7001
x: -1.30334156994
y: 2.2803362443
function value: 0.930459972575
------------------------------------------
Iteration: 8001
x: -1.42505981694
y: 1.24322307994
function value: 0.604422816099
------------------------------------------
Iteration: 9001
x: -1.04901290775
y: 1.83044616216
function value: 1.16839305819
------------------------------------------

Ex:14.2 Pg: 374

In [9]:
from math import atan
def f(x,y):
    z=x*y*y
    return z
p1=[2, 2]
elevation=f(p1[0],p1[1])
dfx=p1[0]*p1[0]
dfy=2*p1[0]*p1[1]
theta=atan(dfy/dfx)
slope=(dfx**2 + dfy**2)**0.5#
print "Elevation:",elevation
print "Theta: %0.3f"%theta
print "slope: %0.2f"%slope
Elevation: 8
Theta: 1.107
slope: 8.94

Ex:14.3 Pg: 380

In [11]:
def f(x,y):
    z=2*x*y + 2*x - x**2 - 2*y**2
    return z
x=-1#
y=1#
dfx=2*y+2-2*x#
dfy=2*x-4*y#
#the function can thus be expressed along h axis as
#f((x+dfx*h),(y+dfy*h))
print "The final equation is=","180*h**2 + 72*h - 7"
The final equation is= 180*h**2 + 72*h - 7

Ex:14.4 Pg: 381

In [14]:
def f(x,y):
    z=2*x*y + 2*x - x**2 - 2*y**2
    return z
x=-1#
y=1#
d2fx=-2#
d2fy=-4#
d2fxy=2#

modH=d2fx*d2fy-(d2fxy)**2#

for i in range(0,25):
    dfx=2*y+2-2*x#
    dfy=2*x - 4*y#
    #the function can thus be expressed along h axis as
    #f((x+dfx*h),(y+dfy*h))
    def g(h):
        d=2*(x+dfx*h)*(y+dfy*h) + 2*(x+dfx*h) - (x+dfx*h)**2 - 2*(y+dfy*h)**2
        return d
    #2*dfx*(y+dfy*h)+2*dfy*(x+dfx*h)+2*dfx-2*(x+dfx*h)*dfx-4*(y+dfy*h)*dfy=g'(h)=0
    #2*dfx*y + 2*dfx*dfy*h + 2*dfy*x + 2*dfy*dfx*h + 2*dfx - 2*x*dfx - 2*dfx*dfx*h - 4*y*dfy - 4*dfy*dfy*h=0
    #h(2*dfx*dfy+2*dfy*dfx-2*dfx*dfx-4*dfy*dfy)=-(2*dfx*y+2*dfy*x-2*x*dfx-4*y*dfy)
    h=(2*dfx*y+2*dfy*x-2*x*dfx-4*y*dfy+2*dfx)/(-1*(2*dfx*dfy+2*dfy*dfx-2*dfx*dfx-4*dfy*dfy))#
    x=x+dfx*h#
    y=y+dfy*h#
print "The final values are:",[x, y]
The final values are: [-1, 1]