MATLAB: Problem regarding n no quantum well

matrix manipulation

I have written a program to calculate the energy for n no of quantum well. The code is given below
clc
X = input('Molefraction of Al in system:');
n=input('input:');
width1=input('length of inf barrier:');
for j=1:n
width(2*j)=input('input of well:');
width((2*j)+1)=input('length of barrier:');
end
del_eg=1.247*X;
V1 = 0.6*del_eg;
for j=1:n
V(1) = 0.6*del_eg;
V(2*j)=0;
V((2*j)+1)=V(1);
end
y=1;
hbar = 1.055e-34; %hbar in ev*m
m0 = 9.11e-31; %Rest mass of the electron in eV
q = 1.602e-19;
X=0;
for j=1:2*n-1
L(j) =X+width(j+1);
X=L(j);
end
meff1 = 0.08*m0;
for j=1:n
meff(1) = 0.08*m0;
meff(2*j)=0.067*m0;
meff((2*j)+1)=0.08*m0;
end
for E = 0.0001:0.0001:(V1-0.0001);
for j=1:n
k(1) = sqrt((2*meff(1)*(E - V(1))*q)/(hbar^2));
k(2*j)=sqrt((2*meff(2*j)*(E - V(2*j))*q)/(hbar^2));
k((2*j)+1)=sqrt((2*meff((2*j)+1)*(E - V((2*j)+1))*q)/(hbar^2));
end
for j=1:n
x(1)=0;
x(j+1)=L(j);
end
for j=1:(2*n)
m(j,1) = ((1/2)*((1 + ((k(j)*meff(j+1))/(k(j+1)*meff(j))))*exp(1i*(x(j)*(k(j) - k(j+1))))));
m(j,2) = ((1/2)*((1 - ((k(j)*meff(j+1))/(k(j+1)*meff(j))))*exp(-(1i*(x(j)*(k(j) + k(j+1)))))));
m(j,3) = ((1/2)*((1 - ((k(j)*meff(j+1))/(k(j+1)*meff(j))))*exp(1i*(x(j)*(k(j) + k(j+1))))));
m(j,4) = ((1/2)*((1 + ((k(j)*meff(j+1))/(k(j+1)*meff(j))))*exp(-(1i*(x(j)*(k(j) - k(j+1)))))));
end
for j=1:(2*n)
M(j) = [m(j,1),m(j,2);m(j,3),m(j,4)];
end
d=1;
for j=1:n
Mf(j)=M((2*j)-1)*M(2*j)*d;
d=Mf(j);
end
J22=d(2,2);
z = y;
y = real(J22);
if z*y < 0
disp(E)
end
end
now there is an error –
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Error in ==> newn at 65
M(j) = [m(j,1),m(j,2);m(j,3),m(j,4)];
I need help on this. Please help me.

Best Answer

You are trying to store a 2 by 2 array of m values in to a single location in the numeric array M. That is not going to fit.
If, for some reason, you need the 2 x 2 block to be accessible through a single index, then change the line to
M{j} = [m(j,1),m(j,2);m(j,3),m(j,4)];
and change the lines
d=1;
for j=1:n
Mf(j)=M((2*j)-1)*M(2*j)*d;
d=Mf(j);
end
to
d=ones(2,2);
for j=1:n
Mf{j}=M{(2*j)-1}*M{2*j}*d;
d=Mf{j};
end
However, keep in mind that the "*" operation between the two "M" accesses, and the "*" between the second "M" and the "d", are going to be matrix multiplications. If you want element-by-element multiplication, use ".*" instead of "*" .