help with inner dimention mismatch
Show older comments
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)...
Answers (1)
Image Analyst
on 17 Nov 2013
1 vote
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 Comments
Image Analyst
on 17 Nov 2013
Edited: Image Analyst
on 17 Nov 2013
The "inner dimensions" refer to the c1 and r2 - those must be equal, and they sort of cancel out and you're left with a matrix that has the outer dimensions of r1 x c2. At least that's how I think of it (the inner numbers vanishing or canceling out: m*k*k*n => m*n). If the two inner k's are not the same number it can't cancel out and you will have an error.
dav
on 17 Nov 2013
dav
on 17 Nov 2013
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!