How to create a multivariable function for used in fminsearch
7 views (last 30 days)
Show older comments
Hi,
I have a function with 200 variables. Now these need to be in the form x(1), x(2) ...... x(200) and to be used as. And some time my number of variables change based on my input generally varying in between 100-200. How can I place such symbolic expression like x(n) in a matrix form to be used for generating functions to bu used in fminsearch.
1 Comment
Alexandra Harkai
on 3 Mar 2017
The number of design variables (in this case 200) is not meant to change in an optimisation like fmincon. It sounds like your minimisation problem may not be well defined.
It is easier to help if you provide more details on what the optimisation details are, for example, what is the objective function.
Answers (2)
John D'Errico
on 3 Mar 2017
Edited: John D'Errico
on 3 Mar 2017
NO. You will NEVER be able to use fminsearch on a 200 variable problem. It will essentially never be able to search the space to your satisfaction.
Use an optimizer that is designed to solve larger problems. Fminsearch works reasonably well on small problems, with up to around 6 variables as my usual upper limit, although sometimes you might go a bit higher than that, perhaps to as many as 10 variables. 200 (even 100) is wildly beyond the capabilities of fminsearch. You will be wasting your time if you try.
2 Comments
John D'Errico
on 4 Mar 2017
This is a problem that may grow exponentially (or worse!) with dimension. A simple way to appreciate why is to see that a basic dissection of the n-hypercube into simplexes will result in factorial(n) different simplexes. Worse, see that factorial(n) actually grows faster than exp(n). (Look at Stirling's approximation for the factorial to see how it grows.)
So to fully explore an n-dimensional space, a simplex does very poorly in high dimensions.
Walter Roberson
on 3 Mar 2017
Example:
nvar = randi([100 200]);
y = randn(1, nvar);
x = sym('x', [1, nvar]);
F = @(x) sin(x).*cos(2*x); %function being minimized
funsym = sum((F(x)-y).^2); %least squared minimization
f = matlabFunction(funsym, 'Vars', {x}); %numeric function with vector input
x0 = rand(1, nvar) * 100 - 50; %starting point
bestx = fminsearch(f, x0, struct('MaxFunEvals', 100000, 'MaxIter', 100000) );
(Realistically you won't get anywhere useful on this particular function.)
Remember that systematically evaluating even just two different positions for each variable requires 2^nvar function evaluations, which is probably not realistic for nvar > 34 or so.
0 Comments
See Also
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!