PDA

View Full Version : Lattice of harmonic oscillators - Matlab


barnflakes
Feb18-11, 05:36 PM
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.

caffenta
Feb21-11, 03:53 AM
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?