my fminsearch function doesn't get the optimum values

2 views (last 30 days)
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?

Accepted Answer

Star Strider
Star Strider on 16 Oct 2021
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
.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!