Could anyone help me with an anonymous function trouble please?!
Info
This question is closed. Reopen it to edit or answer.
Show older comments
I am straggling to have this code work!
myfun = @(x1,x2) ((x1 - 2).^4 + (x1 - 2*x2).^2 + (x1.^2 - x2).^2); fminsearch(my fun, [2.0, 1.0]);
could anyone help please!
Answers (2)
James Tursa
on 23 Mar 2015
Make the argument of myfun a vector. E.g.,
myfun = @(x) ((x(1) - 2).^4 + (x(1) - 2*x(2)).^2 + (x(1).^2 - x(2)).^2);
fminsearch(myfun, [2.0, 1.0])
ans =
1.1687 0.7407
Guillaume
on 23 Mar 2015
The function that you pass to fminsearch takes a single vector/matrix as input instead of several variables. Therefore:
myfun = @(x) ((x(1) - 2).^4 + (x(1) - 2*x(2)).^2 + (x(1).^2 - x(2)).^2);
fminsearch(myfun, [2 1])
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!