Effacer les filtres
Effacer les filtres

??? Error using ==> times Matrix dimensions must agree

3 vues (au cours des 30 derniers jours)
Guillermo Lopez
Guillermo Lopez le 24 Jan 2012
I have been trying to do my first program with matlab in order to plot some data on a graph. My function is as follows:
%refractive indices
clear all
nair=1;
nSi=3.5+0.005*1i;
nSiO2=1.5;
nMoS2=5.6;
%Materials thickness
dSiO2=285e-10;
dMoS2=input('insert MoS2 thickness (m): ');
donda=linspace(450e-9,700e-9,1000);
for M=1:length(dMoS2)
%Total matrix bare SiO2
m_bare=(Sij(nair,nSiO2)).*(Pj(nSiO2,dSiO2,donda)).* (Sij(nSiO2,nSi));
%Total matrix with MoS2
m=(Sij(nair,nMoS2)).*(Pj(nMoS2,dMoS2(M),donda)).*(Sij(nMoS2,nSiO2)).*(Pj(nSiO2,dSiO2,donda)).*(Sij(nSiO2,nSi));
%Reflection coefficients
R_bare=abs(m_bare(2,1)./m_bare(1,1)).^2;
R=abs(m(2,1)/m(1,1)).^2;
%Contrast
C(M)=(R-R_bare)/(R+R_bare);
end
%Output:
plot(donda,C)
xlabel('wavelength (nm)')
ylabel('Contrast')
Pj function:
%Propagation matrix
function p=Pj(nj,dj,lambda0)
phi=1i*2*pi*nj*dj./lambda0;
p=[exp(-phi) 0; 0 exp(+phi)];
end
And my Sij function:
%scattering matrix
function s=Sij(ni,nj)
nsum=ni+nj;
nn=(ni-nj)./nsum;
s=[1 nn; nn 1]./(2*ni/nsum);
end
When I try to run the program I used to get an error mldivide but after adding a dot to define variables everything was solved on that issue. NOW, im getting an error on line 13 which is as follows:
??? Error using ==> times
Matrix dimensions must agree.
Error in ==> Wavelength_vs_contrast at 13
m_bare=(Sij(nair,nSiO2)).*(Pj(nSiO2,dSiO2,donda)).*(Sij(nSiO2,nSi));
I don't know what im doing wrong, so if you could please help me out I would greatly appreciate it. Thanks in advance: Guillermo

Réponse acceptée

Andrew Newell
Andrew Newell le 24 Jan 2012
In the line
m_bare=(Sij(nair,nSiO2)).*(Pj(nSiO2,dSiO2,donda)).* (Sij(nSiO2,nSi));
you are doing element-by-element multiplication between matrices that are 2 x 2, 2 x 1001 and 2 x 2. Let's call them A, B and C. A multiplication like
A*B
(no dot) makes dimensional sense, as does
(A*B)'*C
I don't know if that is what you are trying to calculate, though.
I recommend you read Debugging.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by