Reputation:
I am trying to minimize a function that is a function of a 1x25 vector (weights_vector). In other words, I'm trying to find the values in the vector that minimize the function.
The function is defined by:
function weights_correct = Moo(weights_vector)
corr_matrix = evalin('base', 'corr_matrix');
tolerance = evalin('base', 'tolerance');
returns = evalin('base', 'returns');
weights_correct = weights_vector'*corr_matrix*weights_vector - tolerance*returns'*weights_vector;
end
On this function, I am calling:
weights_correct = fminsearch(@Moo, weights_vector);
This iterates until I see the error
"Exiting: Maximum number of function evaluations has been exceeded
- increase MaxFunEvals option."
Which leads me to believe that I'm not minimizing correctly. What's going on?
Upvotes: 2
Views: 7170
Reputation:
Use of evalin here is silly. Multiple calls to evalin will be inefficient for no reason. If you will make the effort to learn to use evalin for the wrong purpose, instead, make the effort to learn how to use function handles.
You need not even define an m-file, although you could do so. A simple function handle will suffice.
Moo = @(w_v) w_v'*corr_matrix*w_v-tolerance*returns'*w_v;
Then call a better optimizer. Use of fminsearch on a 25 variable problem is INSANE. The optimization toolbox is worth the investment if you will do optimization a lot.
weights_correct = fminunc(@Moo, weights_vector);
Or, you can do it all in one line.
weights_correct = fminunc(@(w_v) w_v'*corr_matrix*w_v-tolerance*returns'*w_v, weights_vector);
See that when you create the function handle here, MATLAB passes in the values of those arrays.
Finally, the problem with max function evals is a symptom of what you are doing. 25 variables is too much to expect convergence in any reasonable amount of time for fminsearch. You can change the limit of course, but better is to use the right tool to begin with.
Upvotes: 3
Reputation: 14498
You are exceeding the default number of function evaluations. You could change that using
weights_correct = fminsearch(@Moo, weights_vector, optimset('MaxFunEvals', num);
where num
is some number you specify. The default is 200*numberOfVariables
.
I am certainly not an expert, and please, somebody correct me, but 25 variables seems like a lot to ask for an optimization routine.
Upvotes: 2