help with inner dimention mismatch

1 回表示 (過去 30 日間)
dav
dav 2013 年 11 月 17 日
コメント済み: dav 2013 年 11 月 17 日
Hi,
I am trying to run the following code and get an error(given). Could someone please help me figure it out?
Thanks.
clc;
clear;
p=1;
T = 1000;
h=10;
B=1000;
R=1000;
toyo=[];
simulations = 1000;
ftyPI = zeros(h,simulations);
fsigPI = zeros(h,simulations);
Lbyth = zeros(h,simulations);
Lbsigh = zeros(h,simulations);
mccounty = zeros(h,simulations);
mccountsig = zeros(h,simulations);
lb = eps*[1 0]';
a0 = 0.1; a1 = 0.4;
ra = zeros(T+2000,1);
seed = 54321;
rng(seed);
for mc= 1:simulations
mc
%ra = randn(T+2000,1);
ra = trnd(5,T+2000,1);
ra = ra./(sqrt(5/3));
ytn = [];
yt1=zeros(T/5,1);
yt2=zeros(T/5,1);
yt3=zeros(T/5,1);
yt4=zeros(T/5,1);
yt5=zeros(T/5,1);
epsi=zeros(T+2000,1);
simsig=zeros(T+2000,1);
unvar = a0/(1-a1);
for i = 1:T+2000
if (i==1)
simsig(i) = a0+a1*((a0)/(1-a1));
s=(simsig(i))^0.5;
epsi(i) = ra(i) * s;
else
simsig(i) = a0+ a1*(epsi(i-1))^2;
s=(simsig(i))^0.5;
epsi(i) = ra(i)* s;
end
end
yinteta=epsi(2001:T+2000);
epsi2 = epsi.^2;
yt = epsi2(2001:T+2000);
bytlast = yt(T);
tytlast = yt(T);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ESTIMATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
yint = epsi(2001:T+2000);
ytop = yte(yint,T);
theta0 = 0.5*ones(7,1)';
Av=[0 1 0 0 0 0 0 ];
bv=0.99999;
opts = optimset('Display','off','Algorithm','interior-point');
[theta, opt] = fmincon(@(theta)...
lhnew(theta,ytop),theta0,Av,bv,[],[],lb,[],[],opts);
alpha0 = theta(1);
ar1= theta(2);
cc1 = theta(3);
cc2 = theta(4);
cc3 = theta(5);
cc4 = theta(6);
cc5 = theta(7);
mali = [alpha0 ar1 cc1 cc2 cc3 cc4 cc5];
toyo=[toyo;mali];
end
mean(toyo)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function ytop = yte(yint,T)
yin = yint;
for i=1:T
m = mod(i,5);
if m==0
m=5;
end
if m==1
yin(i)= yin(i)*(2^0.5);
elseif m==5
yin(i)= yin(i)*1;
else
yin(i)= yin(i)*(0.5^0.5);
end
end
ytop=yin;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function L = lhnew(theta,y)
w = theta(1);
alpha = theta(2);
c1 = theta(3);
c2 = theta(4);
c3 = theta(5);
c4 = theta(6);
c5 = theta(7);
y2 = y.^2;
[T1,K] = size(y2);
ht = zeros(T1,1);
ht(1) = sum(y2);
for i=2:T1
m = mod(i-1,5);
if m==0
m=5;
end
if m==1
ht(i)= w + alpha*c1*y2(i-1);
elseif m==2
ht(i)= w + alpha*c2*y2(i-1);
elseif m==3
ht(i)= w + alpha*c3*y2(i-1);
elseif m==4
ht(i)= w + alpha*c4*y2(i-1);
elseif m==5
ht(i)= w + alpha*c5*y2(i-1);
end
end
l=zeros(T1,1);
for i=1:T1
m = mod(i,5);
if m==0
m=5;
end
if m==1
l(i) = -0.5*log(2*pi) - log(sqrt(ht(i)) - 0.5*((c1*y(i)/sqrt(i))^2));
elseif m==2
l(i) = -0.5*log(2*pi) - log(sqrt(ht(i)) - 0.5*((c2*y(i)/sqrt(i))^2));
elseif m==3
l(i) = -0.5*log(2*pi) - log(sqrt(ht(i)) - 0.5*((c3*y(i)/sqrt(i))^2));
elseif m==4
l(i) = -0.5*log(2*pi) - log(sqrt(ht(i)) - 0.5*((c4*y(i)/sqrt(i))^2));
elseif m==5
l(i) = -0.5*log(2*pi) - log(sqrt(ht(i)) - 0.5*((c5*y(i)/sqrt(i))^2));
end
end
l=-l;
L = sum(l);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
error:
Error using *
Inner matrix dimensions must agree.
Error in C:\Program Files\Matlab\R2012a\toolbox\optim\optim\private\backsolveSys.p>backsolveSys (line 18)
Error in C:\Program Files\Matlab\R2012a\toolbox\optim\optim\private\solveAugSystem.p>solveAugSystem (line 23)
Error in C:\Program Files\Matlab\R2012a\toolbox\optim\optim\private\leastSquaresLagrangeMults.p>leastSquaresLagrangeMults (line 28)
Error in C:\Program Files\Matlab\R2012a\toolbox\optim\optim\barrier.p>barrier (line 626)
Error in fmincon (line 841)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...
Error in test (line 203)
[theta, opt] = fmincon(@(theta)...

回答 (1 件)

Image Analyst
Image Analyst 2013 年 11 月 17 日
I didn't run the code but in general what it means is that if you're doing a matrix multiplication of a r1 by c1 matrix by a r2 by c2 matrix (that's rows by columns), then c1 must equal r2:
(r1 x c1) * (r2 x c2) must = (r1 x c1) * (c1 x c2) to give (r1 x c2). For some reason, you're not using matrices with those dimensions.
  3 件のコメント
dav
dav 2013 年 11 月 17 日
I checked it but I couldnt find where I was doing it wrong!
dav
dav 2013 年 11 月 17 日
The only place I could think of changing was
theta0 = [1; 1; 1; 1; 1; 1; 1];
but it didnt help

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by