You are on page 1of 2

Exercises with Octave

Particle in a box problem:

1. Plotting the wave function:

x=linspace(0,1,50);

plot(x,sqrt(2)*sin(2*pi*x))

2. Plotting wavefunction and the probability distribution together on the same graph:

x=linspace(0,1,50);
y1=sqrt(2)*sin(2*pi*x);
y2=y1.^2;
plot(x,y1)
hold on
plot(x,y2,’r’)

3. Defining function for energy and plotting wavefunction for different energy levels:

#defining energy function (E_n/E_1 is computed) . Here n is quantum number


function ee =energy(n);
ee=n.^2 ;
end

#defining wavefunction . Here n is quantum number, m is mass, and L is length of the box
function pp = psi(n,L,x) ;
pp=sqrt(2/L) * sin(n*pi*x/L) ;
end

#defining the x-grids


x=linspace(0,1,50)
#defining psi_1(x) and prob. density (prob_psi1) aligning to the energy level by adding
energy
psi1=psi(1,1,x)+energy(1) ;
prob_psi1=psi(1,1,x).^2 + energy(1);

#defining psi_2(x) and prob. density (prob_psi2) aligning to the energy level by adding
energy
psi2=psi(2,1,x)+energy(2) ;
prob_psi2=psi(2,1,x).^2 + energy(2);

#defining psi_3(x) and prob. density (prob_psi3) aligning to the energy level by adding
energy
psi3=psi(3,1,x)+energy(3) ;
prob_psi3=psi(3,1,x).^2 + energy(3);

# plotting functions
plot (x,psi1)
hold on
plot (x, prob_psi1)
hold on
plot (x,psi2)
hold on
plot (x, prob_psi2)
hold on
plot (x,psi3)
hold on
plot (x, prob_psi3)

You might also like