ERROR:NOT ENOUGH INPUT ARGUMENT IN LINE n = length(y);

3 visualizaciones (últimos 30 días)
NUR BATRISYIA HANNANI ZAIN AZMI
NUR BATRISYIA HANNANI ZAIN AZMI el 20 de Feb. de 2021
Comentada: Cris LaPierre el 16 de Mzo. de 2021
% parse arguments
params = {2, 0.7, 10, 8e-4, 1e-2, 'quadprog'};
i = ~cellfun(@isempty, varargin);
params(i) = varargin(i);
[tau0, tau1, delta_knot, alpha, gamma, solver] = deal(params{:});
n = length(y);
y = y(:);
% bateman ARMA model
a1 = 1/min(tau1, tau0); % a1 > a0
a0 = 1/max(tau1, tau0);
ar = [(a1*delta + 2) * (a0*delta + 2), 2*a1*a0*delta^2 - 8, ...
(a1*delta - 2) * (a0*delta - 2)] / ((a1 - a0) * delta^2);
ma = [1 2 1];
% matrices for ARMA model
i = 3:n;
A = sparse([i i i], [i i-1 i-2], repmat(ar, n-2, 1), n, n);
M = sparse([i i i], [i i-1 i-2], repmat(ma, n-2, 1), n, n);
% spline
delta_knot_s = round(delta_knot / delta);
spl = [1:delta_knot_s delta_knot_s-1:-1:1]'; % order 1
spl = conv(spl, spl, 'full');
spl = spl / max(spl);
% matrix of spline regressors
i = bsxfun(@plus, (0:length(spl)-1)'-floor(length(spl)/2), 1:delta_knot_s:n);
nB = size(i, 2);
j = repmat(1:nB, length(spl), 1);
p = repmat(spl(:), 1, nB);
valid = i >= 1 & i <= n;
B = sparse(i(valid), j(valid), p(valid));
% trend
C = [ones(n,1) (1:n)'/n];
nC = size(C, 2);
% Solve the problem:
% .5*(M*q + B*l + C*d - y)^2 + alpha*sum(A,1)*p + .5*gamma*l'*l
% s.t. A*q >= 0
if strcmpi(solver, 'quadprog')
% Use Matlab's quadprog
H = [M'*M, M'*C, M'*B; C'*M, C'*C, C'*B; B'*M, B'*C, B'*B+gamma*speye(nB)];
f = [alpha*sum(A,1)'-M'*y; -(C'*y); -(B'*y)];
[z, obj] = quadprog(H, f, [-A zeros(n,length(f)-n)], zeros(n, 1), ...
[], [], [], [], [], optimset('Algorithm', 'interior-point-convex', ...
'TolFun', 1e-13));
%z = qp([], H, f, [], [], [], [], zeros(n,1), [A zeros(n,length(f)-n)], []);
obj = obj + .5 * (y' * y);
elseif strcmpi(solver, 'sedumi')
% Use SeDuMi
U = [A, sparse(n,nC), -speye(n), sparse(n,n+nB+4); ...
M, C, sparse(n,n+2), -speye(n), sparse(n,2), B; ...
sparse(1,2*n+nC), 1, sparse(1,n+nB+3); ...
sparse(1,3*n+nC+2), 1, sparse(1,nB+1)];
b = [sparse(n,1); y; 1; 1];
c = sparse([n+nC+(1:n), 2*n+nC+2, 3*n+nC+4], ...
1, [alpha*ones(1,n), 1, gamma], 3*n+nC+nB+4, 1);
K = struct('f', n+nC, 'l', n, 'r', [2+n 2+nB]);
pars.eps = 1e-6;
pars.chol.maxuden = 1e2;
z = sedumi(U, b, c, K, pars);
obj = c' * z;
%objd = b' * s;
end
l = z(end-nB+1:end);
d = z(n+1:n+nC);
t = B*l + C*d;
q = z(1:n);
p = A * q;
r = M * q;
e = y - r - t;
end

Respuestas (1)

Cris LaPierre
Cris LaPierre el 20 de Feb. de 2021
Editada: Cris LaPierre el 20 de Feb. de 2021
Based on what you've shared, you are trying to take the length of a variable that doesn't yet exist (y). That would give a different error, though, so perhaps you've left something out?
I suggest also sharing the entire error message (all the red text).
  8 comentarios
NUR BATRISYIA HANNANI ZAIN AZMI
NUR BATRISYIA HANNANI ZAIN AZMI el 16 de Mzo. de 2021
thankyou for your response but how to write the code to pass in values of y for my samples?
Cris LaPierre
Cris LaPierre el 16 de Mzo. de 2021
Walter already showed you how to do this.
You should really read though the page I linked you to. The examples show you how to write a function, and then how to use that function.

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by