悬赏 5 个论坛币 未解决
function [re1,re2]=minvar(x,y)
% calculate the optimal coefficient by minimize the variance between x and
% c*y. That is var(x-c*y).
% Input parameters:
% x: the raw data
% y: the benchmark for optimization
% Output parameters:
% re1: the R^2 (first column)
% the parameters c (rest of every row)
% re2: the ESS
% x should be a array or a matrix
% y should be a matrix,the form is either(1,b1,b2...),or(b1,b2...)
% x and y should have the same dimension
[m,n]=size(y);
[a,b]=size(x);
lb = zeros(1,n); % Set lower bounds
if y(:,1) == ones(m,1) % if b is (1,b1,b2...), the corresponding lower bound is empty
lb(1,1)=-inf;
end
ub = [ ]; % No upper bounds
Aeq= ones(1,n); % Set equality constrain
if y(:,1) == ones(m,1) % if b is (1,b1,b2...), the Aeq is (0,1,1...)
Aeq(1,1)=0;
end
beq=1;
options = optimset('LargeScale','off');
mm1 = zeros(b,n+1);
mm2 = zeros(a,b);
for i =1:b
k = x(:,i);
[v,fval] = lsqlin(y,k,[ ],[ ],Aeq,beq,lb,ub);
mm1(i,2:n+1) = v'; % calculate the optimal coefficient c
tssk=sum((k-mean(k)).^2);
essk=sum((k-y*v).^2);
mm1(i,1)=1-essk/tssk; % calculate the R^2 value
mm2(:,i)=(k-y*v).^2; % calculate the ESS
end
re1 = mm1;
re2 = mm2;
为什么运行后出现: Error: Function definitions are not permitted at the prompt or in scripts.