Passing parameters into .m function while trying to use fminsearch

3 vues (au cours des 30 derniers jours)
I have a "complex" function that is saved in a .m file that is then optimized with the fminsearch. The function requires some additional values that the only way I seem to be able to reference is to create global variables( which I prefer not to). I had tried to pass a struct in but it didn't seem to work. How can I pass parameters for a function being called on my fminsearch?
Here is the sample code Xstart = [286 340 5]; [finalValues,f,exitFlag] = fminsearch(@FcnGauss,Xstart);
%%
function f = FcnGauss(V)
global X1D Z1DROI
f = 0; X = X1D; Z = Z1DROI; h = V(1); mu = V(2); s = V(3);
idx = find(Z>0); for i = 1:length(idx) x = X(idx(i)); xbar = (x - mu)/s; del = Z(idx(i)) - h*exp(-xbar^2); f = f + del^2; end
end

Réponse acceptée

Alan Weiss
Alan Weiss le 6 Août 2018
You can pass extra parameters using nested functions or anonymous functions.
Alan Weiss
MATLAB mathematical toolbox documentation

Plus de réponses (1)

Andreia Smith-Moritz
Andreia Smith-Moritz le 6 Août 2018
Perfect! Thank you.I ended up using a nested function.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by