Finding minimum/maximum of function using Lagrange multipliers

179 views (last 30 days)
As mentioned in the title, I want to find the minimum / maximum of the following function with symbolic computation using the lagrange multipliers.
f(x,y) = x*y under the constraint x^3 + y^4 = 1.
syms x y lambda
f = x * y;
g = x^3 + y^4 - 1 == 0; % constraint
L = f + lambda * lhs(g); % Lagrange function
dL_dx = diff(L,x) == 0; % derivative of L with respect to x
dL_dy = diff(L,y) == 0; % derivative of L with respect to y
dL_dlambda = diff(L,lambda) == 0; % derivative of L with respect to lambda
system = [dL_dx; dL_dy; dL_dlambda]; % build the system of equations
[x_val, y_val,lambda_val] = solve(system, [x y lambda], 'Real', true) % solve the system of equations and display the results
results_numeric = double([x_val, y_val, lambda_val]) % show results in a vector of data type double
>>results_numeric =
0.8298 0.8091 -0.3917
0.8298 -0.8091 0.3917
I tried approaching this with the matlab documentation on lagrange but only got so far. This does not represent my minimum/maximum, does it? Also is there a way to now differentiate between max and min? I know there is another aproach using fmincon, which is way easier, but I want to solve it this way.
  2 Comments
Borjan Trajanoski
Borjan Trajanoski on 23 May 2020
So I checked this using wolframalpha and the first vector appears to be the maximum, the second one the minimum of this function. How can I let Matlab check this instead of "Hard coding" it by looking at the results. Also what does the last vector represent? A saddle point?
Sen XU
Sen XU on 9 Apr 2021
Sorry, I tried the code and use Matlab version of 2014a, however, I got empty matrix as follows:
x_val =[ empty sym ]
results_numeric = Empty matrix: 1-by-0
Could you help me solve this problem.

Sign in to comment.

Accepted Answer

Raunak Gupta
Raunak Gupta on 28 May 2020
Hi,
I find some differences while understanding the question. First solve or dsolve is used to find exact solution of system of normal or differential equations as you used in this case. These functions cannot be used to find the maximum or minimum value of your objective function L. Also, since you have coded the problem such that the derivative of the respective function f,g and L are zero you may get maxima or minima based on solution you get after solving.
Also, the solution you are getting are two pair of solution (not three columns that are mentioned in comment) in which first solution is first row for value of x,y,Lambda and similarly the second row. You can evaluate the solution by replacing the values in L and figure out about maxima or minima.
% results_numeric =
0.8298 0.8091 -0.3917
0.8298 -0.8091 0.3917
% Here two solutions are returned
x1 = 0.8298 , y1 = 0.8091 , lamdba1 = -0.3917
x2 = 0.8298 , y2 = -0.8091 , lambda2 = 0.3917
L1 = 0.6714 --> Maxima
L2 = -0.6714 --> Minima
The ideal way to maximize or minimize would be fmincon as you also suggested because there the optimization can be done in different ways apart from Lagrange multiplier. But for the method you have used I think evaluating the result is necessary because solve can only solve the equation and cannot tell about the properties of solution be it maxima or minima.
Hope it clarifies some doubts.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!