Azzera filtri
Azzera filtri

fminsearch with 2 variables - help please!

58 visualizzazioni (ultimi 30 giorni)
darksideofthemoon101
darksideofthemoon101 il 22 Feb 2011
Hi,
I want to use fminsearch to find the minimum of 2 variables, but I don't understand how the fminsearch function works in this case. The basic outline of my code is as follows.
function y=myfunction(x,y)
constants not including x & y
m=1
for z=start:spacing:stop
some calculations involving x & y
m=m+1
end
y= figure of merit for minimisation
How do I use fminsearch to account for the variables x & y? If I try to use
fminsearch(myfunction(x,y),[x0 y0])
I get an error message
??? Error using ==> fcnchk at 109
FUN must be a function, a valid string expression,
or an inline function object.
Error in ==> fminsearch at 178
funfcn = fcnchk(funfcn,length(varargin));
Please help!

Risposte (1)

Baileycat
Baileycat il 1 Mar 2011
Instead of having them as two separate variables, set the problem up with each variable as part of one overall variable.
So, x -> x(1) and y -> x(2)
Then, y=myfunction(x)
[x, fval, exitflag] = fminsearch(@myfunction, [x0;y0])

Categorie

Scopri di più su Get Started with Optimization Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by