Find argmin of function with two input variables

Hello, I want to find the arguments of a function La(x1, x2) that minimize this function by calculating the diff of the function in order of x1 and x2, equal to 0 and solving them. MATLAB keeps returning Empty sym: 0-by-1 and I don't understand why. Any help?
Also, if the derivative of the function in order of one of the variables was equal to 1, and then MATLAB tried to calculate 1 == 0, what would it return?
syms x1 x2
assume(x1 > 0.1)
assumeAlso(x1 < 2.0)
assumeAlso(x2 > 0.1)
assumeAlso(x2 < 2.5)
La(x1, x2) = x1^2 + x1 + x2^2 + 5*x2;
eqn1 = [diff(La, x1)==0];
solx1 = solve(eqn1, x1);
eqn2 = [diff(La, x2)==0];
solx2 = solve(eqn2, x2);

 채택된 답변

Matt J
Matt J 2021년 11월 29일
편집: Matt J 2021년 11월 29일
MATLAB keeps returning Empty sym: 0-by-1 and I don't understand why. Any help?
The solution to diff(La,x1)==0 is x1 = -0.5, which does not satisfy your assumption x1>0.1
and then MATLAB tried to calculate 1 == 0,
It would return no solution, as it should.
syms x
solve(diff(x)==0,x)
ans = Empty sym: 0-by-1

댓글 수: 5

It makes total sense, I just experimented with diferent boundaries for the input variables and it worked, thank you for your time.
ABCDEFG HIJKLMN
ABCDEFG HIJKLMN 2021년 11월 29일
편집: ABCDEFG HIJKLMN 2021년 11월 29일
However, I would like the code to return to me the minimum possible for the limits imposed. For instance, in the original limits of x1 and x2, what would be the pair that lead to the minimum value of La? Is there a way to obtain one of the solutions when there are multiple? @Matt J
It makes total sense, I just experimented with diferent boundaries for the input variables and it worked, thank you for your time.
You're welcome, but please accept-click the answer to indicate that your question is resolved.
For instance, in the original limits of x1 and x2, what would be the pair that lead to the minimum value of La? Is there a way to obtain one of the solutions when there are multiple?
You can use fminbnd.
fminbnd(@(x1) x1^2+x1, 0.1,2)
ans = 0.1000
ABCDEFG HIJKLMN
ABCDEFG HIJKLMN 2021년 11월 29일
편집: ABCDEFG HIJKLMN 2021년 11월 29일
But fminbnd only works if the function only had one variable, right? I would need the coordinates (x1, x2) instead! @Matt J
Your function is additively separable in this example, so the terms dependent on x2 can be minimized independently of x1.
fminbnd(@(x2) x2^2+5*x2, 0.1,2.5)
ans = 0.1001
If you have to do deal with non-separable functions, you can use fmincon().

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2021년 11월 29일

댓글:

2021년 11월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by