2

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

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

Comments

0

An easy solution is to use global returns fund

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.