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!

Respuestas (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])

La pregunta está cerrada.

Preguntada:

JO
el 23 de Mzo. de 2015

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by