Problem regarding Matlab code for n no of quantum well

Click For Summary
SUMMARY

The forum discussion addresses a MATLAB coding issue related to quantum well calculations. The user encounters a "Subscripted assignment dimension mismatch" error when attempting to assign a 2x2 matrix to a 1x2 row of matrix M. The root cause is the mismatch between the dimensions of the matrices being assigned and the defined size of M. To resolve this, the user must ensure that M is defined with appropriate dimensions to accommodate the assignments being made in the loop.

PREREQUISITES
  • Understanding of MATLAB programming and syntax
  • Familiarity with matrix operations in MATLAB
  • Knowledge of quantum mechanics principles related to quantum wells
  • Experience with debugging MATLAB code and interpreting error messages
NEXT STEPS
  • Review MATLAB matrix dimension rules and best practices
  • Learn about MATLAB's 'size' and 'length' functions for debugging
  • Explore MATLAB's error handling techniques to manage runtime errors
  • Investigate quantum mechanics simulations using MATLAB for practical applications
USEFUL FOR

This discussion is beneficial for MATLAB programmers, quantum physicists, and researchers working on quantum well simulations who need to troubleshoot coding errors and improve their understanding of matrix operations in MATLAB.

Rajat Karmaka
Messages
1
Reaction score
0
I have written a code which is something like that

clc
X = input('Molefraction of Al in system:');
n=input('no of well:');

width=zeros(1,50);
for j=1:(2*n+1)
width(j,:)=input('length of areas:');
end

del_eg=1.247*X;
V1=0.6*del_eg;
V=zeros(1,100);
for j=1:n
V(2*j,:)=0;
V(((2*j)+1),:)=0.6*del_eg;
V(1)=0.6*del_eg;
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;

Xi=0;
L=zeros(1,100);
for j=1:(2*n-1)
L(j,:) =Xi+width(j+1);
Xi=L(j);
end

Y=0;
x=zeros(1,1000);
for j=1:n
x(((2*j)-1),:)=Y;
x(2*j,:)=L((2*j)-1);
Y=L(2*j);
end

meff=zeros(1,1000);
for j=1:n
meff(2*j,:)=0.067*m0;
meff(((2*j)+1),:)=0.08*m0;
meff(1)=0.08*m0;
end

for E = 0.0001:0.0001:(V1-0.0001);

k=zeros(1,1000);
for j=1:n
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));
k(1)=sqrt((2*meff(1)*(E - V(1))*q)/(hbar^2));
end

m1=zeros(1,4);
m2=zeros(1,4);
m3=zeros(1,4);
m4=zeros(1,4);
M=repmat(0,2,2);

for j=1:(2*n)
m1(j,:) = ((1/2)*((1 + ((k(j)*meff(j+1))/(k(j+1)*meff(j))))*exp(1i*(x(j)*(k(j) - k(j+1))))));
m2(j,:) = ((1/2)*((1 - ((k(j)*meff(j+1))/(k(j+1)*meff(j))))*exp(-(1i*(x(j)*(k(j) + k(j+1)))))));
m3(j,:) = ((1/2)*((1 - ((k(j)*meff(j+1))/(k(j+1)*meff(j))))*exp(1i*(x(j)*(k(j) + k(j+1))))));
m4(j,:) = ((1/2)*((1 + ((k(j)*meff(j+1))/(k(j+1)*meff(j))))*exp(-(1i*(x(j)*(k(j) - k(j+1)))))));

M(j,:) = [m1(j),m2(j);m3(j),m4(j)];

end

d=repmat(1,2,2);
Mf=repmat(0,2,2);
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-

?? Subscripted assignment dimension mismatch.

Error in ==> kihobekejane at 68
M(j,:) = [m1(j),m2(j);m3(j),m4(j)];

how can i remove this problem? please help me.
 
Physics news on Phys.org
Your matrix dimensions do not match.
You defined:
Rajat Karmaka said:
m1=zeros(1,4);
m2=zeros(1,4);
m3=zeros(1,4);
m4=zeros(1,4);
M=repmat(0,2,2);

So m1 to m4 are matrices that look like
0 0 0 0
and M looks like
0 0
0 0

Then you try to assign
Rajat Karmaka said:
M(j,:) = [m1(j),m2(j);m3(j),m4(j)];

M(j,:) is the jth row of matrix M and looks like
0 0.
Now you try to assign a matrix of 2x2 values to a 1x2 matrix which cannot work.
Also, your loop over j goes from 1 to 2n while M is statically defined to be a 2x2 matrix. This will also cause problems.
 

Similar threads

  • · Replies 41 ·
2
Replies
41
Views
10K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K