Could anyone help me with an anonymous function trouble please?!

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)

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
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.

Asked:

JO
on 23 Mar 2015

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!