How can i create an objective function with n-variables for optimization?
1 view (last 30 days)
Show older comments
I have a program that optimizes a scalar function using "fmincon". First, i ask to user a number of variables and save some coefficients on a matrix. Then, with this matrix and the number of variables i generate a symbolic function. When i have the symbolic function i change from symbolic to character using "char", and finally i used this function in "fmincon" as the objective function. The problem is that i would like to generate the objective function but with gradient to get a better solution, is there any way to do this?, or another method to generate the objective function for n-variables with gradient?. Thanks in advance.
2 Comments
Matt J
on 12 May 2013
Edited: Matt J
on 12 May 2013
Why generate a symbolic function? Why not just express the objective function and gradient non-symbolically. This is often more efficient, since in many cases, the objective and gradient reuse common intermediate quantities.
Even if you insist on using a symbolic function, why convert it to char? Why not use the symbolic form to evaluate the objective? And regardless of that, why can't you implement your gradient using the same steps as for your objective? Why is it more complicated simply because you are now computing the gradient in addition to the objective?
Answers (2)
Matt J
on 12 May 2013
Edited: Matt J
on 12 May 2013
If i analyze a problem with 3 variables, i should write the scalar function and the gradient, but what about if i want to analize a problem with 10 variables? i should rewrite the function and the gradient. And if i want to analize the same 10 variables problem but with another coefficientes, again i should rewrite the function.
No, you should be expressing your function in terms of vectors instead of scalars, so that the actual lengths of those vectors do not matter. For example, in a 3 variable problem, instead of having
f(x)=x(1)+x(2)+x(3)
you should have
f(x) = sum(x)
Now, it does not matter whether you change x from length 3 to length 10. The expression for f(x) is generic independently of the number of variables.
The same goes for the gradient calculation.
2 Comments
Matt J
on 13 May 2013
Edited: Matt J
on 13 May 2013
First, my objective function is nonlinear. with the coefficients of the matrix i generate the function: a*x(i)^2+b*x(i)+c*x(j)^2+d*x(j)+e*x(i)*x(j)+f
Does the above get summed over i and j or are i,j fixed? Assuming the latter, you you can write this in terms of a length-n vector x of unknown variables as follows. This is not optimized to minimize computation, but it shows the general idea.
function [fval,gradient]=objective(x,a,b,c,d,e,f,i,j)
E=speye(n);
Eij=E([i,j],:);
A=Eij.'*[a e/2;e/2 c]*Eij;
B=[b,d]*Eij;
fval= x.'*A*x + B*x +f
gradient = 2*A*x+B.';
Alan Weiss
on 13 May 2013
You might have some luck with matlabFunction. See the following examples:
Alan Weiss
MATLAB mathematical toolbox documentation
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!