# Incorrect dimensions for matrix multiplication

12 views (last 30 days)
Mahsa Babaee on 22 Jul 2021
Commented: Mahsa Babaee on 30 Jul 2021
Hello friends!
I am trying to plot a fourier serie. But I faced a problem as below:
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise
multiplication, use '.*'.
Error in SingleLayer (line 100)
S2= S2+(4*XI*YI*ZI)*((T_ini-T_ts)*exp(-alpha*landa2(n,m,l)*t)-OMEGA.*(exp(-alpha*landa2(n,m,l)*t)-1)/landa2(n,m,l))/(Lx*Ly*ZI2);
I would be appreciated if you could guide me about my carelessness. My code is:
Lx= 0.040; Ly= 0.040; Lz= 0.00008; tmax= 5;
k= 0.25; rho= 1200; Cp=3600 ; alpha= k/(rho*Cp);
omega_b= 0; rho_b= 1060; C_b= 3770;
omega= sqrt(rho_b*C_b*omega_b/k);
w= omega^2;
T_b=310 ; T_ini=309 ; T_ts=310 ;
nmax=10 ; mmax=10 ; lmax=10 ;
mu= 80e+03;
I0=1 ; q_met= 368.1;
OMEGA= w*(T_b- T_ts)*+q_met/k;
%%%%%%%%%%%%
beta= zeros(1,nmax);
for n=1:nmax
beta(n)= n*pi/Lx;
end
gamma= zeros(1,mmax);
for m=1:mmax
gamma(m)= m*pi/Ly;
end
sigma= zeros(1,lmax);
for l=1:lmax
sigma(l)= (2*l-1)*pi/(2*Lz);
end
landa2= zeros(nmax,mmax,lmax);
for n=1:nmax
for m=1:mmax
for l=1:lmax
landa2(n,m,l)= (beta(n))^2+(gamma(m))^2+(sigma(l))^2;
end
end
end
Zeim= zeros(1,lmax);
for l=1:lmax
Zeim(l)= (mu*sigma(l)*exp(-mu*Lz)*sin(sigma(l)*Lz)-mu^2*exp(-mu*Lz)*cos(sigma(l)*Lz)+mu^2)/(mu^2+sigma(l)^2);
end
XI= zeros(1,nmax);
for n=1:nmax
XI(n)= 1/beta(n)*(1-cos(beta(n)*Lx));
end
YI= zeros(1,mmax);
for m=1:mmax
YI(m)= 1/gamma(m)*(1-cos(gamma(m)*Ly));
end
ZI= zeros(1,lmax);
for l=1:lmax
ZI(l)= 1/sigma(l)*sin(sigma(l)*Lz);
end
ZI2= Lz/2;
ql= zeros(nmax,mmax);
for n=1:nmax
for m=1:mmax
ql(n,m)= 1/(beta(n)*gamma(m))*(cos(beta(n)*Lx)-1)*(cos(gamma(m)*Ly)-1); %ql= int(f(x,y)*X(n,x)*Y(m,y),0,Lx,0,Ly) % Assumption f(x,y)=1
end
end
a= zeros(nmax,mmax,lmax); d= zeros(nmax,mmax,lmax);
S1= zeros(nmax,mmax,lmax); S2= zeros(nmax,mmax,lmax);
for n=1:nmax
for m=1:mmax
for l=1:lmax
S1=0; S2=0; T= zeros(nmax,mmax,lmax);
for x=0:Lx/10:Lx
for y= 0:Ly/10:Ly
for t=0:0.01:tmax
S1= S1+(4*I0*ql(n,m)*Zeim(l)*(1-exp(-alpha*landa2(n,m,l)*t)))/(Lx*Ly*k*ZI2*landa2(n,m,l));
S2= S2+(4*XI*YI*ZI)*((T_ini-T_ts)*exp(-alpha*landa2(n,m,l)*t)-OMEGA*(exp(-alpha*landa2(n,m,l)*t)-1)/landa2(n,m,l))/(Lx*Ly*ZI2);
T(n,m,l)=T(n,m,l)+(S1+S2)*sin(beta(n)*x)*sin(gamma(m)*y);
end
end
end
plot (t,T)
end
end
end

Walter Roberson on 22 Jul 2021
XI= zeros(1,nmax);
YI= zeros(1,mmax);
XI and YI are the same size.
S2= S2+(4*XI*YI*ZI)*((T_ini-T_ts)*exp(-alpha*landa2(n,m,l)*t)-OMEGA*(exp(-alpha*landa2(n,m,l)*t)-1)/landa2(n,m,l))/(Lx*Ly*ZI2);
You use XI*YI . In order for that to work, size(XI,2) == size(YI,1) must be true. size(YI,1) is 1 (because it is 1 x mmax), so size(XI,2) would have to be 1, so nmax would have to be 1.
nmax=10 ; mmax=10 ; lmax=10 ;
But it is not.
Remember that * is algebraic matrix multiplication ("inner product"). If you want to multiply element-by-element you need to use .* instead of *
Mahsa Babaee on 30 Jul 2021
Thank you so much.

### Categories

Find more on Annotations 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!