Max Kim
Max Kim

Reputation: 1124

Passing arguments to objective function fmincon Matlab

I am trying to minimize an objective function which consists of variables other than the constraint variable. Is there a way to pass arguments to such a function, for example:

data = xlsread('Returns.xlsx', 'Sheet2','A2:F324');

for i = 1:10
    returns = data(i:i+59,1:5);
    fund = data(i:i+59,6:6);
    lb = [0;0;0;0;0];
    ub = [1; 1; 1; 1; 1];
    [betas, fval] = fmincon(@obj_function, [0 .2 .2 .2 .2 .2], [], [], [], [], lb, ub, @constraints);
end

And the objective function is defined as:

function [ value ] = obj_function(betas)
    value = returns*betas(2:6) + betas(1);
    value = sum((value - fund)^2);
end

Since my objective function needs to extra variables returns and fund, what is the best way I can keep passing it from the main function? Below statement is invalid, what else can I do?

[betas, fval] = fmincon(@obj_function(returns, fund), [.2 .2 .2 .2 .2], [], [], [], [], lb, ub, @constraints);

EXTRA, function constraint is defined as follows:

function [ c, ceq ] = constraints( betas )
    c = [];
    ceq = betas(2)*1 + betas(3)*1 + betas(4)*1 + betas(5)*1 + betas(6)*1 - 1;
end

Upvotes: 2

Views: 3361

Answers (2)

Amro
Amro

Reputation: 124563

Use anonymous functions to create closures:

a = 1; b = 2;
[...] = fmincon(@(x) myObjFcn(x, a, b), ...)

Here is a documentation page explaining this in more details:

Passing Extra Parameters

Upvotes: 5

Max Kim
Max Kim

Reputation: 1124

An easy solution is to use global returns fund

Upvotes: 0

Related Questions