No damping but the solution to simple harmonic oscillator damps?

Click For Summary
SUMMARY

The discussion centers on the unexpected damping observed in simulations of an undamped simple harmonic oscillator using MATLAB's ode45 solver. The user implemented the Duffing oscillator and noted that while the analytical solution is ##\cos (5\sqrt{2} t)##, the simulation results appeared damped. Key factors identified include the global parameters set for damping, stiffness, and driving force, as well as the potential introduction of error in the numerical method. Recommendations include trying different ODE solvers and refining the simulation step size to mitigate damping effects.

PREREQUISITES
  • Understanding of MATLAB programming and syntax
  • Familiarity with ordinary differential equations (ODEs)
  • Knowledge of the Duffing oscillator dynamics
  • Experience with numerical methods for solving ODEs, specifically ode45
NEXT STEPS
  • Explore alternative ODE solvers in MATLAB, such as RKF45
  • Research error analysis in numerical simulations to understand energy conservation
  • Learn about the characteristics of stiff systems and appropriate solvers
  • Investigate algorithms designed to conserve energy in simulations
USEFUL FOR

Researchers, engineers, and students working with dynamic systems, particularly those simulating nonlinear oscillators and seeking to improve numerical accuracy in MATLAB.

member 428835
I posted yesterday but figured it out; however, a different issue I just detected with the same code arose: namely, why does the solution damp here for an undamped simple harmonic oscillator? I know the exact solution is ##\cos (5\sqrt 2 t)##.

Code:
global delta alpha beta gamma OMEG
delta      = 0;     % DAMPING
alpha     = 50;   % STIFFNESS
beta       = 0;     % RESTORATION NONLINEARITY
OMEG    = 1.2;  % DRIVING FORCE ANGULAR FREQ
gamma  = 0;     % FORCING

Fs = 100;       % Sampling frequency                  
T  = 1/Fs;      % Sampling period     
L  = 10000;     % Length of signal

[t, x]  = ode45(@duffing,(0:L-1)*T,[0 1]);

plot(t,x(:,2))

and the function ode45 calls is

Code:
function xdot = duffing(t,x)
global delta alpha beta gamma OMEG
xdot(1) = -delta*x(1) - alpha*x(2) - beta*x(2)^3 + gamma*cos(OMEG*t);
xdot(2) = x(1);
xdot    = xdot';
end
 
Physics news on Phys.org
Damping can occur when error is introduced somehow. The error in this case would be subtracting from the system energy causing it to appear as damping.

You could try another ODE solver beside ODE45 to see if that fixes the problem.

When simulating stuff, error can introduce more energy or take away energy depending on the algorithm chosen. As an example, Euler methods are good for systems that are non-periodic such as tracking a particle trajectory and ODE45 is good for periodic systems. In general, though ODE45 always seemed to be the best even though it could be slower in some cases.

You could also try a finer step.

Here's how to choose an ode solver:

https://www.mathworks.com/help/matlab/math/choose-an-ode-solver.html
Here's the MATLAB description of the ode45 solver:

https://www.mathworks.com/help/matlab/ref/ode45.html
which mentions it might not be good for very stiff systems.

As an aside, I found some Matlab notes on using ode45 that might be of interest:

https://www.12000.org/my_notes/matlab_ODE/
 
Last edited:
  • Like
Likes   Reactions: member 428835
When we ran simulations of planetary orbits in java code using one of the Euler variants, we would get either a planet crashing into the Sun or zipping off on a journey of adventure and off our screen due to the algorithm introducing small error that manifested as energy loss or energy gain in a closed system.

The trick was to find an algorithm that alternated adding some error and then taking it away and between those two limits produced an effective and accurate simulation over longer periods of time.
 
jedishrfu said:
The trick was to find an algorithm that alternated adding some error and then taking it away and between those two limits produced an effective and accurate simulation over longer periods of time.
Another way would be to find an algorithm that respects the important symmetries of the system, ie, one that is explicitly designed such that energy is conserved.
 
  • Like
Likes   Reactions: jedishrfu
jedishrfu said:
Damping can occur when error is introduced somehow. The error in this case would be subtracting from the system energy causing it to appear as damping.
Can you elaborate on this please?
 
Orodruin said:
Another way would be to find an algorithm that respects the important symmetries of the system, ie, one that is explicitly designed such that energy is conserved.
Any ideas for such an algorithm? See, I'm using my developed code to solve the Duffing oscillator, so there are nonlinear components. I just thought I'd benchmark the code with an equation that admits an analytic solution first.
 
What are the initial conditions?

For "fun," I implemented the Duffing oscillator in C using RKF45 from GSL, which should be the same integrator as the one used by Matlab, and I get the correct solution for the undamped case.
 
  • Like
Likes   Reactions: jedishrfu
DrClaude said:
What are the initial conditions?

For "fun," I implemented the Duffing oscillator in C using RKF45 from GSL, which should be the same integrator as the one used by Matlab, and I get the correct solution for the undamped case.
Hmmmm this is odd. I use ##x(0) = 1## and ##\dot x (0) = 0##.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
7
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K