简体   繁体   中英

Formulating a summation objective function to minimize for fmincon in MatLab

I have a summation objective function (non-linear portfolio optimization) which looks like:

minimize w(i)*w(j)*cv(i,j) for i = 1 to 10 and j = 1 to 10
  • w is the decision vector
  • cv is a known 10 by 10 matrix

I have the formulation done for the constraints (a separate .m file for the project constraints) and for the execution of the fmincon (a separate .m file for the lower/upper bounds, initial value, and calling fmincon with the arguments).

I just can't figure out how to do the objective function. I'm used to linear programming in GLPK rather than matlab so I'm not doing so good.

I'm currently got:

ObjectiveFunction.m

function f = obj(w)

cv = [all the constants are in here]

i = 1;    
j = 1;    
n = 10;    
var = 0;    

while i <= n    
       while j<=n    
           var = var + abs(w(i)*w(j)*cv(i, j));    
           j = j + 1;    
       end    
       i = i + 1;    
end

f = var

...but this isn't working.

Any help would be appreciated! Thanks in advance :)

So this is from a class I took a few years ago, but it addresses a very similar problem to your own with respect to use of fminsearch to optimize some values. The problem is essentially that you have at, y, and you want a continuous exponential function to represent t, y in terms of c1*t.*exp(c2*t). The textbook I lifted the values from is called Numerical Analysis by Timothy Sauer . Unfortunately, I don't remember the exact problem or chapter, but it's in there somewhere.

c1 and c2 are found recursively by fminsearch minimizing a residual y - ((c1) * t .* exp((c2) * t)). Try copying and running my code below to get a feel for things:

    %% Main code
    clear all; 
    t = [1,2,3,4,5,6,7,8]; 
    y = [8,12.3,15.5,16.8,17.1,15.8,15.2,14]; 
    lambda0=[1 -.5]; 
    lambda=fminunc(@expdecayfun,lambda0, ...
    optimset('LargeScale','off','Display','iter','TolX',1.e-6),t,y); 
    c1=lambda(1);
    c2=lambda(2); 
    fprintf('Using the BFGS method through fminunc, c1 = %e\n',c1); 
    fprintf('and c2 = %e. Since these values match textbook values for\n', c2); 
    fprintf('c1 and c2, I will stop here.\n');        

    %% Index of functions:
    % expdecayfun
    function res=expdecayfun(lambda,t,y) c1=lambda(1); 
    c2=lambda(2); 
    r=y-((c1)*t.*exp((c2)*t)); 
    res=norm(r);

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM