How can i create an objective function with n-variables for optimization?

1 view (last 30 days)
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
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?
Pablo Tagle
Pablo Tagle on 12 May 2013
Edited: Pablo Tagle on 12 May 2013
Because i don't analyze a problem with a fixed number of variables. 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. In the progrma i create i don't have to do this, i only change the matrix and the number of variables, an it generates the gradient and the scalar function automatically, but as symbolic. The function "fmincon" cannot read this kind of objective functions, but if i change only the scalar to char fmincon reads it, but whitout the gradient. If you have another idea to do this problem i will be gratefull.

Sign in to comment.

Answers (2)

Matt J
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
Pablo Tagle
Pablo Tagle 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 where a to f are saved on the matrix and x(i),x(j) are generated from the number of variables. i can create the objective by usign symbolic, and even use it on fmincon, but i'd like that fmincon "reads" the gradient, so how can i do that? i can generate the gradient as strings or symbolic, but i can't make fmincon read it correctly. do you understand? in few words, i can generate the objective function (without gradient) and i can make fmincon read the objective function, but when i try to include the gradient it doesn't work.
Matt J
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.';

Sign in to comment.


Alan Weiss
Alan Weiss on 13 May 2013
You might have some luck with matlabFunction. See the following examples:
Alan Weiss
MATLAB mathematical toolbox documentation

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!