How to set up a function handle for many variables for an unusual function?
Afficher commentaires plus anciens
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!
Réponses (1)
Walter Roberson
le 17 Juin 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 commentaires
Daniel
le 20 Juin 2016
Walter Roberson
le 22 Juin 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);
Catégories
En savoir plus sur Choose a Solver dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!