Lattice of harmonic oscillators - Matlab

Click For Summary
SUMMARY

The discussion focuses on generating a lattice of harmonic oscillators using MATLAB. The user presents an algorithm that iteratively calculates the positions and times of oscillators but encounters unexpected collective oscillation behavior. Key issues identified include the incorrect use of indices in the x matrix before they are initialized and the need for proper handling of dynamical equations to ensure individual oscillation rather than collective motion. The user is advised to review the initialization of the x matrix and the logic within the nested loops.

PREREQUISITES
  • Understanding of MATLAB programming and syntax
  • Familiarity with dynamical systems and harmonic oscillators
  • Knowledge of numerical methods for solving differential equations
  • Experience with matrix manipulation in MATLAB
NEXT STEPS
  • Review MATLAB documentation on matrix initialization and indexing
  • Study the principles of dynamical systems to refine the oscillator model
  • Learn about numerical integration techniques for differential equations in MATLAB
  • Explore examples of individual harmonic oscillators in MATLAB to compare implementations
USEFUL FOR

Researchers, physicists, and engineers working with dynamical systems, particularly those interested in simulating harmonic oscillators using MATLAB.

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?
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K