All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Problem: Minimize: 5-(x-2)2-2(y-1)2; subject to the following constraint: x+4y=3 1) Lagrange Multipliers Lagrange multipliers technique is a fundamental technique to solve problems involving constrained problems. This method is utilised to find the local minima and maxima subjected to (at least one) equality…
Adnan Zaib Bhat
updated on 22 Dec 2018
Problem:
Minimize: 5-(x-2)2-2(y-1)2;
subject to the following constraint: x+4y=3
Lagrange multipliers technique is a fundamental technique to solve problems involving constrained problems. This method is utilised to find the local minima and maxima subjected to (at least one) equality constraints.
The fundamental concept in a constrained optimisation problem is to maximise or minimise a certain equation but keeping some other constraint equation in consideration as well. The equation which is to be optimised is called the objective function. For example, if a company has to maximise its revenue, it may say that they have a model (some mathematical equation) for the revenue f(x,y), where, say, x and y are the material cost and labour hours respectively. The company can input more and more x and y to increase the revenue. But the company has a certain constraint for its budget. And say that the model for their budget is given by the function g(x,y). In this situation, the company wants to maximise its revenue given the budget. When optimised, we get the values for x and y that will maximise the revenue within the given budget.
In order to solve the problem, we use a Lagrangian form:
∇L(x,y,λ):∇(f(x,y)-λ(c(x,y)-k)
where f(x,y) is the objective function, c(x,y)=k is the constrained equality and λ is known as the Lagrange multiplier(s). We solve for ∇L(x,y,λ)=0 and find the values of x and y.
Considering our problem, we have:
Objective function, f(x,y):5-(x-2)2-2(y-1)2
Constraint, c(x,y):x+4y=3
Now looking at the objective function, what 'f(x,y)' actually is that in the x-y plane, 5-(x-2)2-2(y-1)2= constant is the collection of all points (x,y) where it equates to the 'constant' value. Now, this constant can have different values. If we equate f(x,y) to z, the values of x and y in the objective function can be determined by fixing all the possible values for z and calculating the (x,y) values which may be real only, or complex. A 3D plot of the objective function can be obtained in MatLab as shown in the code below:
%Plotting the objective function in 3D using MATLAB
%Symbolic
syms x y z %syms command allows the creation of symbolic variables.
z = 5-(x-2)^2 -2*(y-1)^2; %the objective function.
fsurf(x,y,z) % plots a 3D plot (surface)
title('Plot of Objective Function')
xlabel('X')
ylabel('Y')
zlabel('Z')
Clealy, the curve has only global maxima and no minima i.e., the curve keeps on going in the negative z-direction, but halts somewhere (has a maximum) in the positive z-direction. To calculate it, we can simply differentiate the function with respect to x and y and then equate each differential to zero.
[∂∂xf(x,y)∂∂xf(x,y)]=[00] {scalar matrix}
[∂∂x(5-(x-2)2-2⋅(y-1)2)∂∂y(5-(x-2)2-2⋅(y-1)2)]=[00]
[-2(x-2)-4(y-1)]=[00]
Solving, -2(x-2)=0 and -4(y-1)=0, we have, (x,y)=(2,1). Putting this value of (x,y) in the objective function, we get: f(2,1)=5-(2-2)2 -2(y-1)2=5. Thus, the function has a global maxima, 5 at (2,1) and no global minima.
1.1.a MatLab Code For Maxima:
%Finding the Maximum of the Objective function
syms x y z
z = 5-(x-2)^2 -2*(y-1)^2;
deriv = [diff(z,x),diff(z,y)]; %diff(z,x) differentiates z w.r.t x.
x_y = [solve(deriv(1) == sym(0),x),solve(deriv(2) == sym(0),y)];
%x_y contains the symbolic values of x and y at the extrema
x1 = double(x_y(1)); %converts the first element, x value to a number
y1 = double(x_y(2));
z_max = 5-(x1-2)^2 -2*(y1-1)^2; %calculates the value of the function at x_y
D = sprintf('The function has a maximum of %d at (x,y) = (%d,%d)',z_max,x1,y1);
disp(D)
>>>
The function has a maximum of 5 at (x,y) = (2,1)
1.1.b Plotting Objective Function and Constraint
Now, to optimise the above function, we have to look for the constraint. Here, our constraint is a line in the x-y plane. If we draw a line with the objective function, we get a plot like the one shown in Fig. 2.
%Plotting Objective Function and Constaint Function in MATLAB
syms x,y,z
z = 5-(x-2)^2 -2*(y-1)^2;
fsurf(x,y,z,'EdgeColor','None') %creates a smooth interpolated coloured surface
title('Plot of Objective and Constrain Functions')
xlabel('X')
ylabel('Y')
zlabel('Z')
hold on %keeps the previous plot on the graph
y = (3- x)/4 %constraint
fplot(y,'linewidth',2) %plots the symbolic thicker planar curve
1.1.c Contours
Our solution lies where the constraint line and the objective function meet. In other words, the minimum of the objective function which is on the graph of the constraint, is the solution. To have a better visualisation of the two curves meeting, we must see both the plots in a single plane. As the constraint equation is solely in the X-Y plane (z=0), we need to project the objective function onto the X-Y plane. The curves obtained are called contours. A contour is a set of lines (curves) of a function of two variables along which the function has a constant value (z in our case). The contour of the objective function can be obtained in MatLab with the help of the following code:
%Plotting Contours of the Objective Function and the Constraint Line
syms x y z
z = 5-(x-2)^2 -2*(y-1)^2; %objective funstion
fc = fcontour(z,'r'); %fcontour() is the contour function and fc stores its handle
fc.LevelList = [-40 -30 -20 -10 5 0 1 3 4 4.5 4.9 10 20 30 40];
%LevelList is the method of fcontour which specifies the values of the function and generate curves. Eg., in our case, it will assign z values of -40,-30,-20....40 and draw the contour lines at these values of the function.
axis([-4 4 -4 4]) %fixes the axis
hold on
y = (3-x)/4; %constraint
fplot(y,'b') %plot as a blue line
grid on
title('Contour of the Objective Function and the Constraint Line')
xlabel('X')
ylabel('Y')
legend('Contour','Constraint Line')
From the fig. 3, it is quite clear now that each constant value of the f(x,y) yields different contour curves. We know that the function has a maxima at (2,1) where the contour is just a point on the x-y plane. Now, as we move away from this point in the x-y plane, the function will tend to become more and more negative up to (-)infinity. However, our constraint puts a limit to this. We only want to find the minimum of the curve where the constraint is also true. That is, (x,y) must be satisfied both by the objective function as well as the constraint. This means we have to find the contour curve which as much far away from the maximum but at the same time not more away than the constraint line. In simple terms, the constraint line is tangent to the contour curve at the optimisation level.
We know that the gradient of a function at a point gives a vector that is normal to the curve. Mathematically,
∇f(x,y)= [∂∂xf(x,y)∂∂yf(x,y)] {a vector}
1.1.d Tangency
As the constraint line is tangent to the curve, a normal at the point of tangency will be normal to both constraint line and the contour i.e., gradients of either function will be proportional to each other. Fig. 4 shows the idea visually.
Mathematically,
∇f(x,y)∝∇c(x,y)
or, ∇f(x,y)=λ ∇c(x,y)
where λ is the proportionality constant.
[∂∂xf(x,y)∂∂yf(x,y)]=λ [∂∂xc(x,y)∂∂yc(x,y)]
Solving the above equation gives us two two equations and three variables, add the constriant equation, and we have three variables and three equations. Using this we can easily calculate the value of (x,y) and ultimately f(x,y)min.
[∂∂x(5-(x-2)2-2⋅(y-1)2)∂∂y(5-(x-2)2-2⋅(y-1)2)]=λ [∂∂x(x+4y)∂∂y(x+4y)]
[-2(x-2)-4(y-1)]=λ[14]
which gives: -2(x-2)=λ and -4(y-1)=4λ
Eliminating λ, we have:
-2(x-2)=-(y-1)⇒y=2x-3
Substituting the value of y in the constraint equation, we have:
x+4(2x-3)=3⇒9x-12=3⇒x=159=53
∴y=2(53)-3=103-3=13
Thus, f(x,y)cmin=f(53,13)=5-(53-2)2-2(13-1)2=4
The value of λ (which is calculated if one want's to check the proportionality) can also be calculated.
we have, -4(y-1)=4λ⇒λ=-(13-1)=23
This implies that the gradient or normal of the objective function is 2/3 times the gradient of the constraint line at (5/3,1/3), and they are in the same direction.
To put all the equations in a beautiful matrix form, Lagrange Multipliers equation can be put in a single gradiant equation form. If the constraint function is c(x,y)=k, where k is some constant, putting g(x,y)=c(x,y)-k=0, we have:
∇f(x,y)=λ ∇c(x,y)=λ∇g(x,y)
or, ∇f(x,y)-λ ∇g(x,y)=0 ⇒ ∇(f(x,y)-λg(x,y)=0
∴∇L(x,y,λ):∇(f(x,y)-λ(g(x,y))=0
Which gives all the required equations in a single matrix form.
Inserting the equations of the problems we have,
∇L(x,y,λ)=∇(5-(x-2)2-2⋅(y-1)2-λ(x+4y-3))
[∂∂x(5-(x-2)2-2⋅(y-1)2-λ(x+4y-3))∂∂y(5-(x-2)2-2⋅(y-1)2-λ(x+4y-3))∂∂λ(5-(x-2)2-2⋅(y-1)2-λ(x+4y-3))]=[000]
Which gives back the three equations we required to solve for the constrained minimisation. Note that the third row in the below matrix is the constraint equation in g(x,y)=0 form.
[-2(x-2)-λ-4(y-1)-4λ-x-4y+3]=[000]
[-2(x-2)-λ-4(y-1)-4λx+4y-3]=[000]
Note: The LaGrange multiplier equation can also be written in the form:
∴∇L(x,y,λ):∇(f(x,y)+λ(g(x,y))=0
In this case, the sign of λ is opposite to that of the one obtained from the previous equation. For example, if we calculate the Lagrange multiplier for our problem using this formula, we get λ=-23. However, x and y remain unchanged. Also, the gradient of the objective function will still be 23 times that of the constraint at (53,13).
Using, Matlab's 'diff()' command to differentiate a function, 'solve()' command to solve an equation or set of linear equations, we can write a code to solve the steps involved in manual calculation to solve for f(x,y)cmin and λ.
Code:
%Optimsing the Objective Function Using the Lagrange multipliers
%1) Define symbolic functions
syms x y z t l
clc
z = 5 - (x-2)^2 - 2*(y-1)^2; %objective function
t = x + 4*y - 3; %constraint
%2) Obtain gradients
%2.1) gradiant of the objective function
deriv_O = [diff(z,x),diff(z,y)]; %diff(z,x) differentiates z w.r.t x.
%2.2) gradiant of the constraint
deriv_C = [diff(t,x),diff(t,y)]; %diff(z,x) differentiates z w.r.t x.
%3) Putting the gradients in the LaGrange form
M = deriv_O + l*deriv_C; %deriv_O - deriv_C would result in opp. sign of lambda
%Here, the matrix addition is done element-wise.
%4) Solving the equations
sol = solve([M(1),M(2),t],[x,y,l]); %three equations, variables
%sol is a struct containing the values of symbolic x,y and l.
values = [string(sol.x),string(sol.y),string(sol.l)]; %convertind struct to strings
%5) Calculating the value of the function at optimimum values
f_opt = 5 - (double(sol.x)-2)^2 - 2*(double(sol.y)-1)^2; %converting symbolic valus to double precision numbers
%)6 Displaying the functions and results
E = sprintf(['For the constrained Optimisation problem: \n '...
'objective function: \t%s \n Subjected to the cosntraint: \t%s'...
'\n\n x = %s, y = %s and lambda = %s \n Where the value of the '...
'objective function is = %d'],z,t,values,f_opt);
disp(E)
%if M = derivO - derivC is used, lamda would be 2/3
>>>
For the constrained Optimisation problem:
objective function: 5 - 2*(y - 1)^2 - (x - 2)^2
Subjected to the cosntraint: x + 4*y - 3
x = 5/3, y = 1/3 and lambda = -2/3
Where the value of the objective function is = 4
NOTE: Constraint optimisation can be solved in both Python as well as Matlab. In Python, the 'scipy.optimize' library has the essential functions to solve constrained optimisation problems. In Matlab, constrained optimisation problems can be solved by various functions like 'fmincon', 'fseminf' and 'fminbnd'. One can also utilise the Optimisation Toolbox for an interactive optimisation. However, most of these programs require initial guesswork. The initial value must be as close to the solution as possible, which can often be challenging. Another problem with these functions is that they may get stuck in local minima instead of the global minima. One needs to set proper inputs to these functions in order to get the best results. Optimisation techniques like the Genetic Algorithm become essential in such situations. For the working of Genetic Algorithm optimisation in Matlab, check out Genetic Algorithm. Finding The Global Maxima of The Stalagmite Function in MatLab. [https://bit.ly/2CbQmox]
***END***
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
File Parsing and Data Analysis in Python Part I (Interactive Parsing and Data Visualisation)
1) File Parsing Definition: Parse essentially means to ''resolve (a sentence) into its component parts and describe their syntactic roles''. In computing, parsing is 'an act of parsing a string or a text'. [Google Dictionary]File parsing in computer language means to give a meaning to the characters of a text file as per…
15 Jan 2019 02:28 PM IST
File Parsing and Data Analysis in Python Part I (Interactive Parsing and Data Visualisation)
1) File Parsing Definition: Parse essentially means to ''resolve (a sentence) into its component parts and describe their syntactic roles''. In computing, parsing is 'an act of parsing a string or a text'. [Google Dictionary]File parsing in computer language means to give a meaning to the characters of a text file as per…
09 Jan 2019 02:59 AM IST
File Parsing and Data Analysis in Python Part II (Area Under Curve and Engine Performance)
1) Integration/Area Under Curve 1.1 PV Diagram In thermodynamics, a PV diagram is a plot which shows the relationship between the pressure and volume for a particular process. We know that dw=p.dv is the small work done by the process at a particular instance. Hence, total work done by a process from…
08 Jan 2019 06:07 AM IST
Constrained Optimisation Using Lagrange Multipliers
Problem: Minimize: 5-(x-2)2-2(y-1)2; subject to the following constraint: x+4y=3 1) Lagrange Multipliers Lagrange multipliers technique is a fundamental technique to solve problems involving constrained problems. This method is utilised to find the local minima and maxima subjected to (at least one) equality…
22 Dec 2018 06:32 PM IST
Related Courses
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.