Placement 2025 Scholarship: Your Future Starts Here | 6 Guaranteed Job Interviews | Limited to 100 seats. Apply Now

05D 22H 51M 00S

Menu

Executive Programs

Workshops

Projects

Blogs

Careers

Placements

Student Reviews


For Business


More

Academic Training

Informative Articles

Find Jobs

We are Hiring!


All Courses

Choose a category

Mechanical

Electrical

Civil

Computer Science

Electronics

Offline Program

All Courses

All Courses

logo

CHOOSE A CATEGORY

Mechanical

Electrical

Civil

Computer Science

Electronics

Offline Program

Top Job Leading Courses

Automotive

CFD

FEA

Design

MBD

Med Tech

Courses by Software

Design

Solver

Automation

Vehicle Dynamics

CFD Solver

Preprocessor

Courses by Semester

First Year

Second Year

Third Year

Fourth Year

Courses by Domain

Automotive

CFD

Design

FEA

Tool-focused Courses

Design

Solver

Automation

Preprocessor

CFD Solver

Vehicle Dynamics

Machine learning

Machine Learning and AI

POPULAR COURSES

coursePost Graduate Program in Hybrid Electric Vehicle Design and Analysis
coursePost Graduate Program in Computational Fluid Dynamics
coursePost Graduate Program in CAD
coursePost Graduate Program in CAE
coursePost Graduate Program in Manufacturing Design
coursePost Graduate Program in Computational Design and Pre-processing
coursePost Graduate Program in Complete Passenger Car Design & Product Development
Executive Programs
Workshops
For Business

Success Stories

Placements

Student Reviews

More

Projects

Blogs

Academic Training

Find Jobs

Informative Articles

We're Hiring!

phone+91 9342691281Log in
  1. Home/
  2. Adnan Zaib Bhat/
  3. Constrained Optimisation Using Lagrange Multipliers

Constrained Optimisation Using Lagrange Multipliers

Problem: Minimize: 5−(x−2)2−2(y−1)25-(x-2)2-2(y-1)2; subject to the following constraint: x+4y=3x+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…

  • MATLAB
  • Adnan Zaib Bhat

    updated on 22 Dec 2018

Problem:

Minimize: 5−(x−2)2−2(y−1)25-(x-2)2-2(y-1)2;

subject to the following constraint: x+4y=3x+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 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)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)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)∇L(x,y,λ):∇(f(x,y)-λ(c(x,y)-k) 

where f(x,y)f(x,y) is the objective function, c(x,y)=kc(x,y)=k is the constrained equality and λλ is known as the Lagrange multiplier(s). We solve for ∇L(x,y,λ)=0∇L(x,y,λ)=0 and find the values of x and y. 

1.1 Geometrical Interpretation:

Considering our problem, we have:

Objective function, f(x,y):5−(x−2)2−2(y−1)2f(x,y):5-(x-2)2-2(y-1)2  

Constraint, c(x,y):x+4y=3c(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=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)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')
 3D Surface Plot of the objective function                                                                                      Fig. 1: 3D, Surface Plot of the Objective Function.

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][∂∂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][∂∂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][-2(x-2)-4(y-1)]=[00]

Solving, −2(x−2)=0-2(x-2)=0 and −4(y−1)=0-4(y-1)=0, we have, (x,y)=(2,1)(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=5f(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

     

 

Objective and Constraint Function in 3D plot

                                                                                         Fig. 2: Objective Function and Constraint  Plot.

 

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')
 Objective Function Contour and Constraint                                                                              Fig. 3: Contour of the Objective Function and the Constrained Line

From the fig. 3, it is quite clear now that each constant value of the f(x,y)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)⎤⎦∇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. 

Lagrange's tangency                                                                                          Fig. 4: Tangency, Gradients of the Objective and Constraint

Mathematically,

∇f(x,y)∝∇c(x,y)∇f(x,y)∝∇c(x,y)

or, ∇f(x,y)=λ ∇c(x,y)∇f(x,y)=λ ∇c(x,y)

where λλ is the proportionality constant. 

⎡⎣∂∂xf(x,y)∂∂yf(x,y)⎤⎦=λ ⎡⎣∂∂xc(x,y)∂∂yc(x,y)⎤⎦[∂∂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)minf(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)⎤⎦[∂∂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][-2(x-2)-4(y-1)]=λ[14]

which gives: −2(x−2)=λ-2(x-2)=λ and −4(y−1)=4λ-4(y-1)=4λ

Eliminating λλ, we have:

−2(x−2)=−(y−1)⇒y=2x−3-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=53x+4(2x-3)=3⇒9x-12=3⇒x=159=53

∴y=2(53)−3=103−3=13∴y=2(53)-3=103-3=13

Thus, f(x,y)cmin=f(53,13)=5−(53−2)2−2(13−1)2=4f(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-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. 

 

1.2 Lagrangian Form:

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)=kc(x,y)=k, where kk is some constant, putting g(x,y)=c(x,y)−k=0g(x,y)=c(x,y)-k=0, we have:

 

∇f(x,y)=λ ∇c(x,y)=λ∇g(x,y)∇f(x,y)=λ ∇c(x,y)=λ∇g(x,y)

or, ∇f(x,y)−λ ∇g(x,y)=0 ⇒ ∇(f(x,y)−λg(x,y)=0∇f(x,y)-λ ∇g(x,y)=0 ⇒ ∇(f(x,y)-λg(x,y)=0

∴∇L(x,y,λ):∇(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))∇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⎤⎥⎦[∂∂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)=0g(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]

⎡⎢⎣−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∴∇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λ=-23. However, xx and yy remain unchanged. Also, the gradient of the objective function will still be 2323 times that of the constraint at (53,13)(53,13). 

 

1.3 MatLab Code To Solve Lagrangian

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)cminf(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.

Please  login to add a comment

Other comments...

No comments yet!
Be the first to add a comment

Read more Projects by Adnan Zaib Bhat (17)

File Parsing and Data Analysis in Python Part I (Interactive Parsing and Data Visualisation)

Objective:

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…

calendar

15 Jan 2019 02:28 PM IST

    Read more

    File Parsing and Data Analysis in Python Part I (Interactive Parsing and Data Visualisation)

    Objective:

    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…

    calendar

    09 Jan 2019 02:59 AM IST

      Read more

      File Parsing and Data Analysis in Python Part II (Area Under Curve and Engine Performance)

      Objective:

      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.dvdw=p.dv is the small work done by the process at a particular instance. Hence, total work done by a process from…

      calendar

      08 Jan 2019 06:07 AM IST

        Read more

        Constrained Optimisation Using Lagrange Multipliers

        Objective:

        Problem: Minimize: 5−(x−2)2−2(y−1)25-(x-2)2-2(y-1)2; subject to the following constraint: x+4y=3x+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…

        calendar

        22 Dec 2018 06:32 PM IST

        • MATLAB
        Read more

        Schedule a counselling session

        Please enter your name
        Please enter a valid email
        Please enter a valid number

        Related Courses

        coursecard

        Simulation and Design of Power Converters for EV using MATLAB and Simulink

        4.9

        22 Hours of Content

        coursecard

        Introduction to Hybrid Electric Vehicle using MATLAB and Simulink

        4.8

        23 Hours of Content

        coursecardcoursetype

        Mechanical Engineering Essentials Program

        4.7

        21 Hours of Content

        coursecard

        Vehicle Dynamics using MATLAB

        4.8

        37 Hours of Content

        coursecard

        Introduction to CFD using MATLAB and OpenFOAM

        4.8

        13 Hours of Content

        Schedule a counselling session

        Please enter your name
        Please enter a valid email
        Please enter a valid number

        logo

        Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.

        https://d27yxarlh48w6q.cloudfront.net/web/v1/images/facebook.svghttps://d27yxarlh48w6q.cloudfront.net/web/v1/images/insta.svghttps://d27yxarlh48w6q.cloudfront.net/web/v1/images/twitter.svghttps://d27yxarlh48w6q.cloudfront.net/web/v1/images/youtube.svghttps://d27yxarlh48w6q.cloudfront.net/web/v1/images/linkedin.svg

        Our Company

        News & EventsBlogCareersGrievance RedressalSkill-Lync ReviewsTermsPrivacy PolicyBecome an Affiliate
        map
        EpowerX Learning Technologies Pvt Ltd.
        4th Floor, BLOCK-B, Velachery - Tambaram Main Rd, Ram Nagar South, Madipakkam, Chennai, Tamil Nadu 600042.
        mail
        info@skill-lync.com
        mail
        ITgrievance@skill-lync.com

        Top Individual Courses

        Computational Combustion Using Python and CanteraIntroduction to Physical Modeling using SimscapeIntroduction to Structural Analysis using ANSYS WorkbenchIntroduction to Structural Analysis using ANSYS Workbench

        Top PG Programs

        Post Graduate Program in Hybrid Electric Vehicle Design and AnalysisPost Graduate Program in Computational Fluid DynamicsPost Graduate Program in CADPost Graduate Program in Electric Vehicle Design & Development

        Skill-Lync Plus

        Executive Program in Electric Vehicle Embedded SoftwareExecutive Program in Electric Vehicle DesignExecutive Program in Cybersecurity

        Trending Blogs

        Heat Transfer Principles in Energy-Efficient Refrigerators and Air Conditioners Advanced Modeling and Result Visualization in Simscape Exploring Simulink and Library Browser in Simscape Advanced Simulink Tools and Libraries in SimscapeExploring Simulink Basics in Simscape

        © 2025 Skill-Lync Inc. All Rights Reserved.

                    Do You Want To Showcase Your Technical Skills?
                    Sign-Up for our projects.