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
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:
一个简单的解决方案是使用global returns fund
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.