my fminsearch function doesn't get the optimum values

2 ビュー (過去 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
.
  2 件のコメント
Jeremy Choo
Jeremy Choo 2021 年 10 月 16 日
Thank you so much!
Star Strider
Star Strider 2021 年 10 月 16 日
As always, my pleasure!
.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by