Finding minimum/maximum of function using Lagrange multipliers

조회 수: 21 (최근 30일)
Borjan Trajanoski
Borjan Trajanoski 2020년 5월 23일
댓글: zean 2024년 11월 8일
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
Borjan Trajanoski
Borjan Trajanoski 2020년 5월 23일
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 2021년 4월 9일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Raunak Gupta
Raunak Gupta 2020년 5월 28일
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.
  댓글 수: 2
Borjan Trajanoski
Borjan Trajanoski 2020년 6월 1일
Thank you very much for the precise answer.
zean
zean 2024년 11월 8일
syms x y lam real
f=x^2+2*y^2; %Input ('Ente f(x,y) to beextremized');
g=x^2+y^2-1;%input('Enter the constraint function g(x,y);
F=f-lam*g;
Fx=diff(F,x);
Fy=diff(F,y);
[ax,ay,alam]=solve([Fx,Fy,g],x,y,lam);
ax=double(ax);
ay=double(ay);
r=1;
for k=1:1:size(ax)
if((imag(ax(k))==0)&&(imag(ay(k))==0))
ptx(r)=ax(k);
pty(r)=ay(k);
r=r+1;
end
end
ax=ptx;
ay=pty;
T=subs(f,{x,y},{ax,ay});
T=double(T);
epx=3;
epy=3;
figure(1)
for i=1:length(T)
D=[ax(i)-epx ax(i)+epx ay(i)-epy ay(i)+epy];
fprintf('The critical point (x,y) is (%1.3f,%1.3f).',ax(i),ay(i))
fprintf('The value of the function is %1.3f\n',T(i))
ezcontour(f,D)
hold on
h=ezplot(g,D);
set(h,'Color',[1,0.7,0.9])
plot(ax(i),ay(i),'k.','markersize',15+2*i)
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by