Lattice of harmonic oscillators - Matlab

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 3K views
barnflakes
Messages
156
Reaction score
4
Can someone help me with my code? I am trying to generate a lattice of harmonic oscillators, and I have written the following algorithm:

for n = 2:((N-1)/h)

a(1,n) = -(x0-(x(2,n)-x(1,n)));
x(1,n+1) = 2*x(1,n) -x(1,n-1) + (h^2)*a(1,n);
t(1,n+1) = t(1,n) + (h);

for i = 1:(osc_num-2);
a(i+1,n) = -(2*x(i+1,n) - x(i,n) -x(i+2,n));
x(i+1,n+1) = 2*x(i+1,n) -x(i+1,n-1) + (h^2)*a(i+1,n);
t(i+1,n+1) = t(i+1,n) + (h);
end

end

To generate this, are the dynamical equations correct? There should just be small vibrations of each oscillator around its starting position, but instead they oscillate collectively which is very bizarre. Any help much appreciated.
 
Physics news on Phys.org
All the calculations inside the second for loop are the same for each i value. That's why they oscillate collectively. Another strange thing is that you use x(i+1, n) and x(i+2, n) before they are even calculated. Is the x matrix preloaded with values?