MATLAB: Subscript indices must either be real positive integers or logicals.

schrodinger eig

So I was descritizing schordinger equation in 1D and I wanted to plot the potential on the wells. I used GaAs and InAs as my layers. Yet I get this error when I run the program.
Subscript indices must either be real positive integers or logicals.
Error in Schrodinger3 (line 57)
A(i,j)=y*(1./(h(i)-h(i-1)))*(-((m(i+1)+m(i))./(h(i)*m(i+1)*m(i)))-((m(i)+m(i-1))./(h(i-1)*m(i)*m(i-1)))-U(i));
Can anyone tell me why I get it?
clear
clc
tic;
format short
hplank=6.626e-34;%%---unit m^2.kg/s---%%
hb= hplank/(2*pi);
ec=1.6e-19; %1 electron volts = 1.6x10^-19
m_0=9.1e-31; %unit kg
%mass for InAs layers
m1=0.026; %m_effect=m_r*m_0
%mass for GaAs
m2=0.067;
y=-hb.^2/(2*m_0); %in J.m^2
c1=y/ec; %ev.m^2
V=2;
%--------------------
%thickness
p_1=0.1; p_2=0.1; p_3=0.1; p_4=0.1; p_5=0.1;
%number of points and units in meters
L_1=10;
L_2=4;
L_3=2;
L_4=4;
L_5=10;
N_1=L_1/p_1;
N_2=N_1+L_2/p_2;
N_3=N_2+L_3/p_3;
N_4=N_3+L_4/p_4;
N_5=(N_4-1)+L_5/p_5;
%defining the length
z_1=0:p_1:L_1;
z_2=L_1+p_2:p_2:L_2+L_1;
z_3=L_2+L_1+p_3:p_3:L_3+L_2+L_1;
z_4=L_3+L_2+L_1+p_4:p_4:L_4+L_3+L_2+L_1;
z_5=L_4+L_3+L_2+L_1+p_5:p_5:L_5+L_4+L_3+L_2+L_1-p_5;
z=[z_1 z_2 z_3 z_4 z_5];
for i=1:N_5
if (i>N_1) && (i<=N_2)
m(i)=m2;
U(i)=V;
elseif (i>N_3)&&(i<=N_4)
m(i)=m2;
U(i)=V;
else
m(i)=m1;
U(i)=0;
end
end
for i=1:N_5-1
h(i)=z(i+1)-z(i);
end
for i=1:N_5
for j=1:N_5
if j==i
A(i,j)=y*(1./(h(i)-h(i-1)))*(-((m(i+1)+m(i))./(h(i)*m(i+1)*m(i)))-((m(i)+m(i-1))./(h(i-1)*m(i)*m(i-1)))-U(i));
elseif j==i-1
A(i,j)=y*(1./(h(i)-h(i-1)))*((m(i)+m(i-1))./(h(i-1)*m(i)*m(i-1)));
else
A(i,j)=y*(1./(h(i)-h(i-1)))*(1./(2*h(i)))*((m(i+1)+m(i))./(m(i+1)*m(i)));
end
end
end
[phi,E]=eig(A);
for i=1:N_5
E_0(i)=E(i,i);
end

Best Answer

Your for i loop starts with i=1. You try to use h(i-1). When i is 1 that would be h(1-1) which is h(0) which is not permitted.
Related Question