my fminsearch function doesn't get the optimum values
2 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
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])
function y = tut2(x)
y = 2*x(1)*x(2)+5*x(1)-2*x(1)^2-3*x(2)^2+10;
end
.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!