Constraint Minimization using Python
The function `f(x,y) = 5 - (x - 2)^2 - 2*(y - 1)^2` is to be minimised subject to contraint `g(x,y) = x + 4y - 3`
Using Lagrange Multipliers to find maximum/minimum of `f(x,y)` subject to constraint `g(x,y)`
`L = f - lambdag`
`(delL)/(delx) = -2(x-2) - lambda = 0`
`(delL)/(dely) = -4(y-1) - 4lambda = 0`
`(delL)/(dellambda) = -x-4y+3 = 0`
Solving above 3 equations for 3 unknows `x, y and lambda`
`x = 5/3`
`y = 1/3`
`lambda = 2/3`
At `x = 5/3` and `y = 1/3`, `f(x,y) = 4`. This represents the maximum of the function under constraint `g(x,y)`.
Therefore this function does not have a minimum for the said constraint or the minimum of this function is at infinity or undefined.
Below is a Python program to plot the function on z-axis for corresponding range of values for `x` and `y` with constraint `x+4y -3 = 0`
""" To plot a given function under specified constraint"""
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
a = 1000
def z_value(l,c):
t1 = pow((l - 2),2)
t2 = pow((c - 1),2)
z_i = 5 - t1 - 2*t2
return z_i
x_i = []
y_i = []
for i in range(a):
b = -i
x_i.append(b)
for j in range(a):
x_i.append(j)
x = []
y = []
z = []
for l in x_i:
x.append(l)
c = (3 - l)/4
y.append(c)
z.append(z_value(l,c))
fig = plt.figure()
ax = Axes3D(fig)
ax.plot(x, y, z)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
Plots for the above program are as follows