How to set up a function handle for many variables for an unusual function?
Show older comments
Hi,
I have an optimization problem. However it is not a regular equation like y=x^2. I have the following function where I want to optimize Performance:
Performance = optimizeOscillators(matrix_close, Oscillator, OptimalSellShort, OptimalSellLong, OptimalBuyShort, OptimalBuyLong)
The variables matrix_close and Oscillator are given and need to be transferred into the function. The goal is to find the optimal values for OptimalSellShort, OptimalSellLong, OptimalBuyShort, OptimalBuyLong each lying within the interval [0,100]. I want to use different approaches like fminsearch or ga. I don’t know how to set up a function handle. I was thinking about this:
FitnessFcn = @(OptimalBuyLong, OptimalBuyShort,OptimalSellLong,OptimalSellShort ) optimizeOscillators(matrix_close, Oscillator, OptimalSellShort, OptimalSellLong, OptimalBuyShort, OptimalBuyLong)
With the following call function for the first example:
a=fminsearch(FitnessFcn, matrix_close, Oscillator, OptimalSellShort, OptimalSellLong, OptimalBuyShort, OptimalBuyLong)
However I get the following error:
Error using @(matrix_close,Oscillator,OptimalBuyLong,OptimalBuyShort,OptimalSellLong,OptimalSellShort)optimizeOscillators(matrix_close,Oscillator,OptimalSellShort,OptimalSellLong,OptimalBuyShort,OptimalBuyLong) Not enough input arguments.
I just don’t know how to declare the independent variables giving an interval. Do I have to put a vector for each with e.g. integers 1 till 100? How do I have to put it? At the moment I only have the start value of 50 for each. Please help me! Thanks in advance!
Answers (1)
Walter Roberson
on 17 Jun 2016
0 votes
All values to be found must be bundled together into a single vector. Then inside your objective function you can pull that vector apart, assign parts of it to variables with names meaningful to you, reshaping if that is appropriate.
2 Comments
Daniel
on 20 Jun 2016
Walter Roberson
on 22 Jun 2016
FitnessFcn = @(vars) optimizeOscillators(matrix_close, Oscillator, vars(1), vars(2), vars(3), vars(4));
x0 = [50, 50, 50, 50];
[point, fval] = fminsearch(FitnessFcn, x0);
However, fminsearch does not have any possibility for constraints of any kind, other than that you can get it to give up when it encounters nan or inf. You should be considering fmincon:
A = [];
b = [];
Aeq = [];
beq = [];
lb = [0, 0, 0, 0];
ub = [100, 100, 100, 100];
[point, fval] = fmincon(FitnessFcn, x0, A, b, Aeq, beq, lb, ub);
Categories
Find more on Choose a Solver in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!