my fminsearch function doesn't get the optimum values

8 visualizaciones (últimos 30 días)
Jeremy Choo
Jeremy Choo el 16 de Oct. de 2021
Comentada: Star Strider el 16 de Oct. de 2021
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?

Respuesta aceptada

Star Strider
Star Strider el 16 de Oct. de 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
.

Más respuestas (0)

Categorías

Más información sobre Optimization en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by