my fminsearch function doesn't get the optimum values

조회 수: 8 (최근 30일)
Jeremy Choo
Jeremy Choo 2021년 10월 16일
댓글: Star Strider 2021년 10월 16일
I've tried to do a very simple problem by finding out the optimal x and y with x0 = 1, y0 = 1 for the function f (x, y) = 2xy +5x − 2x^2 −3y^2 +10.
I have no idea what I'm doing so I assumed x = x(1) and y = x(2) and did the function as follows:
function y = tut2(x)
y = 2*x(1)*x(2)+5*x(1)-2*x(1)^2-3*x(2)^2+10;
I then called the thing in the command window:
>> [x,fval] = fminsearch(@tut2,[1,1]), which should have given me x and y values of 3/2 and 1/2 respectively, which are the correct answers to the solution. However, they don't. May I know what went wrong in my coding? The fminsearch website told me to use x(1) and x(2) as the initial points, so why is the function not working?

채택된 답변

Star Strider
Star Strider 2021년 10월 16일
The fminsearch function searches for a minimum. To get a maximum, negate the function —
[x,fval] = fminsearch(@(x)-tut2(x),[1,1])
x = 1×2
1.5000 0.5000
fval = -13.7500
function y = tut2(x)
y = 2*x(1)*x(2)+5*x(1)-2*x(1)^2-3*x(2)^2+10;
end
.

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by