How to minimize a function when variables do not appear directly?
Afficher commentaires plus anciens
The code is too complicated, so I resume its structure in equivalent way: I choose a start vector x, for example x=[x1 x2 x3 x4] (for my problem it could have more than 1000 elements), and through separate functions I calculate the vectors a,b,c,d. Each of them depends on the previous one, more or less in this way:
a=f(x);
b=f(a,par1,par2,par3);
c=f(b,x,par1,par2,par3);
d=f(c,par1,par2,par3);
The object function (OF) I want to minimize is very simple, something like OF=d1+d2+d3+d4, but how can I find the vector x that minimizes OF if it does not appear in the function?
6 commentaires
James Tursa
le 3 Nov 2017
Please tell us how d1+d2+d3+d4 are related to the functions you have posted. Why can't you just chain some function handles together?
Walter Roberson
le 3 Nov 2017
Your OF would appear to be a vector output. How do you want to minimize a vector? Are you looking to minimize the sum of squares of the entries? Are you looking for a pareto front?
The calculations that are done: are they pure formulas, or do they have conditional logic? If they are formulas then they could be evaluated with symbolic inputs to come up with an overall formula that could be used for minimization.
Pasquale
le 3 Nov 2017
Walter Roberson
le 3 Nov 2017
"for" loops can still be worked symbolically, as long as there are no tests against the value of the variables.
Pasquale
le 3 Nov 2017
Stephen23
le 4 Nov 2017
You just need to pass the parameters to the function exactly as Walter Roberson and the MATLAB documentation show:
Réponse acceptée
Plus de réponses (1)
Pasquale
le 4 Nov 2017
0 votes
9 commentaires
Walter Roberson
le 4 Nov 2017
par1 = ...
par2 = ...
par3 = ...
func = @(x) minimize_this(x, par1, par2, par3)
x0 = ...
[best_x, fval, exitflag, output] = fminunc(func, x0);
function cost = minimize_this(x, par1, par2, par3);
a=f(x);
b=f(a,par1,par2,par3);
c=f(b,x,par1,par2,par3);
d=f(c,par1,par2,par3);
OF = sum(d)
cost = OF;
end
Pasquale
le 4 Nov 2017
Walter Roberson
le 4 Nov 2017
You had asked "but how can I find the vector x that minimizes OF" and this code finds that. It tries out different values for entries of x looking for the combination that minimizes the cost, however the cost is calculated. It does not matter whether the calculation is linear. As long as you can take a trial version of x and calculate through to a cost, then the above will do a local minimization.
(If the calculation has discontinuities, it probably will not do well on finding a minima.)
You pass in all the constant values through parameters such as par1 -- you can pass any number of constants that you need. The x parameter gets the current test value whose fitness is to be evaluated. Then it uses the results and possibly numeric gradient estimations to predict where a lower value might be, and it tries that, and so on.
Pasquale
le 4 Nov 2017
Walter Roberson
le 4 Nov 2017
function cost = minimize_this(x);
a = funz1(x);
b = funz2(x);
c = funz3(x);
d = funz4(a, b, c);
cost = d;
end
with
x0 = ...
[best_x, fval, exitflag, output] = fminunc(@minimize_this, x0);
"how can I find the vector x that minimizes OF if it does not appear in the function?"
This does not make sense: x must appear in the cost function for the minimization to make any sense. The problem is that you seem to think that it is required to evaluate all of those functions for some x value before calling fminunc, but this totally defeats the purpose of functions: they can be passed around without being evaluated with specific values.
Instead of this:
x=1;
A=funz1(x);
B=funz2(x);
C=funz3(x);
D=funz4(A,B,C);
you need to stop thinking that you have to evaluate those functions for some x value before using them in your cost function. All you need is to call those functions inside of your main cost function (of course adding the parameters as Walter Roberson showed):
function OF = cost(x)
A=funz1(x);
B=funz2(x);
C=funz3(x);
D=funz4(A,B,C);
OF=2*D-D;
end
"How should I write the function cost, and how write it in the main code?"
Walter Roberson has already shown you exactly how you should solve this task. Thrice. And now me too. What part of this does not work for you? What are you expecting that this solution does not give you?
I even put all of this into one Mfile and attached it to this comment. It runs. It gives exactly the same output as your "simple example" above.
Add the parameters exactly like Walter Roberson showed you already.
Pasquale
le 4 Nov 2017
Stephen23
le 4 Nov 2017
@Pasquale: you should accept Walter Roberson's answer, because it correctly resolves your question.
Pasquale
le 4 Nov 2017
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!