2016-11-21 155 views
2

我需要尽量减少以下功能MATLAB - 写入目标函数fmincon()

nDOF,nprocs和T从该表中

enter image description here

的前两列取

所以我在MATLAB中有以下向量:

nDOF = 
     3993 
     3993 
     3993 
     3993 
     3993 
     3993 
     3993 
     3993 
     7623 
     7623 
     7623 
     7623 
     7623 
     7623 
     7623 
     7623 

nprocs = 
    1 
    2 
    3 
    4 
    6 
    8 
    12 
    24 
    1 
    2 
    3 
    4 
    6 
    8 
    12 
    24 

vals = 
    0.6564 
    0.2569 
    0.2719 
    0.1743 
    0.1305 
    0.1230 
    0.1739 
    0.1147 
    1.1998 
    0.5088 
    0.6419 
    0.2899 
    0.2192 
    0.2033 
    0.2126 
    0.1821 

,我想尽量减少与功能fmincon函数$ F $是这样的:

fmincon(@(theta) objFunctionMatAssemble(theta(1), theta(2), theta(3), theta(4), ndofIN, nprocsIN, vals), [0 0 0 0]', [], [], [], [], [0 0 0 0], [+inf +inf +inf +inf]) 

代码objFunctionMatAssemble的:

function [t] = objFunctionMatAssemble(alpha, beta, gamma, delta, ndofIN, nprocsIN, vals) 
    t = 0; 
    for i=1:length(nprocsIN) 
     t = t + alpha*ndofIN^beta + gamma*((ndofIN(i)^delta)/nprocsIN(i) - vals(i))^2; 
    end 
end 

的问题是,我收到以下错误:

>> fmincon(@(theta) objFunctionMatAssemble(theta(1), theta(2), theta(3), theta(4), ndofIN, nprocsIN, vals), [0 0 0 0]', [], [], [], [], [0 0 0 0], [+inf +inf +inf +inf]) 
Error using^
Inputs must be a scalar and a square matrix. 
To compute elementwise POWER, use POWER (.^) instead. 

Error in objFunctionMatAssemble (line 4) 
     t = t + alpha*ndofIN^beta + gamma*((ndofIN(i)^delta)/nprocsIN(i) - vals(i))^2; 

Error in @(theta)objFunctionMatAssemble(theta(1),theta(2),theta(3),theta(4),ndofIN,nprocsIN,vals) 

Error in fmincon (line 535) 
     initVals.f = feval(funfcn{3},X,varargin{:}); 

Caused by: 
    Failure in initial objective function evaluation. FMINCON cannot continue. 

这个问题显然是在我的目标函数中,但尽管尝试了几次,但我仍然无法正确写入它。我在这里看到了一些解决方案,但即使我的“闭包函数”只接受一个参数并且调用另一个参数,它的格式仍然不正确。

请问您能帮我解决吗?

回答

1

你忘了在循环中编制ndofIN(你写ndofIN^beta,应该是ndofIN(i)^beta)。

现在,MATLAB中的运算符^意味着matrix power。对于标量/标量输入,此操作与正常的求幂相同,但对于矩阵/标量输入,它不完全相同。在你的情况下,MATLAB试图向向量ndofINbeta取幂 - 这是不允许的,因为矩阵幂只能为方阵定义。这就是你得到这个错误的原因。

很明显,这不是你打算做的 - 你想要明智的指数(.^)。用这种方法,可以简化并大大加快你的目标函数 - 只写目标函数是这样的:在你的原有功能

function t = objFunctionMatAssemble(alpha, ... 
            beta, ... 
            gamma, ... 
            delta, ... 
            ndofIN, ... 
            nprocsIN, ... 
            vals) 

    t = alpha * ndofIN.^beta + ... 
     gamma * (ndofIN.^delta)./nprocsIN - vals; 

    t = t.' * t; 

end 

注意也有一个括号不一致。也就是说,你的括号让你的目标函数别的东西比你在问题中所显示的内容:

% your version 
t = t + alpha*ndofIN^beta + gamma*((ndofIN(i)^delta)/nprocsIN(i) - vals(i))^2; 

% what's in the image 
t = t + (alpha*ndofIN^beta + gamma* (ndofIN(i)^delta)/nprocsIN(i) - vals(i))^2; 

NB2:你也可以写你这样的目标函数:

function t = objFunctionMatAssemble2(theta, ... 
            ndofIN, ... 
            nprocsIN, ... 
            TIN)          

    N = bsxfun(@power, ndofIN, theta([2 4]).');   
    t = [N(:,1) N(:,2)./nprocsIN] * theta([1 3]) - TIN;   
    t = t.' * t; 

end 

所以你的目标函数不需要太多的论据。但是,这取决于你的口味。