0% found this document useful (0 votes)
10 views

Multivariable Optimization

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Multivariable Optimization

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Multivariable_Optimization

April 7, 2024

Example: Find the extreme points of the function


𝑓(𝑥, 𝑦) = 𝑥3 + 𝑦3 + 2𝑥2 + 3𝑦2 + 𝑥𝑦 + 𝑥 + 𝑦 + 5
The necessary conditions for the existence of an extreme point are:
𝜕𝑓
= 3𝑥2 + 4𝑥 + 𝑦 + 1 = 0
𝜕𝑥
𝜕𝑓
= 3𝑦2 + 6𝑦 + 𝑥 + 1 = 0
𝜕𝑦
As we can see, finding the roots of the two functions is very difficult. Therefore, we have to use
numerical methods for the solution. As the case of the single variable problem case, we can use
the multivariable version of the Newton-Raphson method. For this particular case, the Newton-
Raphson equation will look like this,
−1
𝜕2𝑓 𝜕2𝑓 𝜕𝑓
𝑥 𝑥 𝜕𝑥2 𝜕𝑥𝜕𝑦
[ 1 ]=[ 0 ]−[ 𝜕2𝑓 𝜕2𝑓 ] [ 𝜕𝑥
𝜕𝑓 ]
𝑦1 𝑦0 𝜕𝑦
𝜕𝑦𝜕𝑥 𝜕𝑦2 (𝑥0 ,𝑦0 ) (𝑥0 ,𝑦0 )

We may rewrite this in compact form as,


𝑋1 = 𝑋0 − 𝑑𝑋

The second order partial derivatives of 𝑓 are given by:


𝜕 2𝑓
= 6𝑥 + 4
𝜕𝑥2
𝜕 2𝑓
=1
𝜕𝑥𝜕𝑦

𝜕 2𝑓
=1
𝜕𝑦𝜕𝑥
𝜕 2𝑓
= 6𝑦 + 6
𝜕𝑦2
Then the Hessian matrix of 𝑓 will be:
6𝑥 + 4 1
𝐽 =[ ]
1 6𝑦 + 6
Let’s write a code to solve the problem following the fundamental principles.
Let’s define the function and its derivatives

1
[2]: #Import important libraries.

import matplotlib.pyplot as plt


from matplotlib import cm
from matplotlib.ticker import LinearLocator
from mpl_toolkits import mplot3d
import numpy as np

#Define the function, its derivatives and the Hessian Matrix.

def f(X):
return X[0]**3+X[1]**3+2*X[0]**2+3*X[1]**2+X[0]*X[1]+X[0]+X[1]+5

def dfdx(X):
return 3*X[0]**2+4*X[0]+X[1]+1
def dfdy(X):
return 3*X[1]**2+6*X[1]+X[0]+1
def J(X):
return np.array([[6*X[0]+4,1],[1,6*X[1]+6]])
def df(X):
return np.array([dfdx(X),dfdy(X)])

#Apply the Newton-Raphson method to find the stationary points.

X0=np.array([10,10]) #Initial guess point


tol=1e-5
dX=np.array([10,20])
err=np.linalg.norm(dX)

while err>tol:
dX=np.linalg.solve(J(X0),df(X0))
X0=X0-dX
x0=X0[0];y0=X0[1]
err=np.linalg.norm(dX)

#Display the results.

print('The stationary point is:', X0)


print('The stationary value is',f(X0))
print('J=',np.array(J(X0)))
print('J_1=',J(X0)[0,0])
print('J_2=',np.linalg.det(J(X0)))

if J(X0)[0,0]<0 and np.linalg.det(J(X0))<0:


print('The stationary value is local maxima.')
elif J(X0)[0,0]>0 and np.linalg.det(J(X0))>0:
print('The stationary value is local minima.')

2
else:
print('The stationary value is saddle point.')

#Plot the function and the stationary point.

fig, ax = plt.subplots(subplot_kw={"projection": "3d"})


# Make data.
x = np.linspace(-10, 10, 50)
y = np.linspace(-10, 10, 50)
X = np.meshgrid(x, y)
Z = f(X)

# Plot the surface.

ax = plt.axes(projection ="3d")
plt.rcParams['figure.figsize'] = [20, 15]
surf = ax.plot_surface(X[0], X[1], Z, cmap='viridis', alpha=0.7, linewidth=0,␣
↪antialiased=True, shade=True)

ax.contour(X[0], X[1], Z, zdir='z', offset=-1000, cmap='coolwarm')


ax.contour(X[0], X[1], Z, zdir='x', offset=-10, cmap='coolwarm')
ax.contour(X[0], X[1], Z, zdir='y', offset=10, cmap='coolwarm')

ax.set(xlim=(-10, 10), ylim=(-10, 10), zlim=(-1000, 2500),


xlabel='X', ylabel='Y', zlabel='Z');

# Plot the stationary point.


ax.scatter(X0[0], X0[1], f(X0), color = "green")
ax.plot(X0[0], f(X0), 'ro', zdir='y', zs=10)
ax.plot(X0[1], f(X0), 'go', zdir='x', zs=-10)
ax.plot(X0[0], X0[1], 'bo', zdir='z', zs=-1000)

plt.show()

The stationary point is: [-0.27393531 -0.12938043]


The stationary value is 4.809703128618927
J= [[2.35638816 1. ]
[1. 5.22371742]]
J_1= 2.3563881600415124
J_2= 11.309105878967097
The stationary value is local minima.

3
[ ]:

You might also like