In Reference [4], Discrete damped harmonic oscillator, we applied early discretization to the damped harmonic oscillator (DHO). The discrete solution compared favorably to the standard analytical solution. Later, in Reference [5], Perspectives on energy conservation: discrete variational spherical pendulum, we discovered undesirable periodic energy error in the discrete solution, error that grows and accelerates, error that we didn’t look for first-time-around. Long-term mean energy conservation was excellent, as expected from the mathematics, but the periodic error was troubling.
Amongst the obvious new questions are “Do all discrete solutions in classical mechanics show such energy noise, or only more complex problems like the spherical pendulum?” “Can we quantify the error and characterize it qualitatively?”
Here, we launch some new notebooks to study energy noise in discrete dynamics. The DHO shows periodic energy noise of RMS that scales downward with the time step in the logarithm. When
log
10
time step is
-2
,
log
10
RMS error is around
-4
;
log
10
time step
-3
,
log
10
RMS error around
-6
; and so on.

Analytical Solution

In[]:=
ClearAll[L,F,V,T,Ld];

Lagrangian

Potential Energy

entailing spring constant κ as a free variable:
In[]:=
V[q_]:=
1
2
κ
2
q

Kinetic Energy

entailing mass
m
as a free variable:
In[]:=
T[v_]:=
1
2
m
2
v
;

Lagrangian

In[]:=
L[q_,v_,t_]:=T[v]-V[q]

Modified Euler-Lagrange Equation

The Lagrange-D'Alembert Principle adds a force term to the Euler-Lagrange Equation:

t
∂L
∂v
-
∂L
∂q
+
∂F
∂v
=0
(
1
)

First Term

We correct the first term, a total time derivative, an explicit constant-mass rule for this problem.
In[]:=
Dt[D[L[q,v,t],v],t]/.{Dt[m,t]->0}
Out[]=
mDt[v,t]

Second Term

Partial derivative of
L
with respect to generalized coordinate
q
.
In[]:=
D[L[q,v,t],q]
Out[]=
-qκ

Force Term

Choose Rayleigh's Dissipation Function,
F=
b
2
2
v
, with
b
, the dissipation constant as a third free variable:
In[]:=
F[q_,v_,t_]:=
b
2
2
v

Sum of all Terms

In[]:=
deqn=deqn=((Dt[D[L[q,v,t],v],t]-D[L[q,v,t],q]+D[F[q,v,t],v])/.{Dt[m,t]->0}/.{v->Dt[q,t]}/.{q->x[t]})==0
Out[]=
κx[t]+b
′
x
[t]+m
′′
x
[t]0

Analytical Solution

Pick the pertinent part of the expression returned by DSolve:
In[]:=
asoln=(DSolve[deqn,x,t])[[1,1,2]]
Out[]=
Function{t},
1
2
t-
b
m
-
2
b
-4mκ
m


1
+
1
2
t-
b
m
+
2
b
-4mκ
m


2


Numerical Rules

For all subsequent plots and demonstrations, choose the following values for the constants
κ,m,C[2],C[1]
.
In[]:=
ClearAll[numRules];​​numRules={κ->1,m->1,C[2]->1,C[1]->0};

Plot

Over 25 seconds’ time, plot the displacement; tLim=25 is a constant over all the plots in this notebook:
In[]:=
tLim=25;
If
2
b
is smaller than
4mκ
, we get complex exponentials. Only the real parts are physical, but we plot the imaginary parts for mathematical interest.
As
2
b
varies from 0 to
4mκ=4
, the system transitions from undamped and purely sinusoidal, to underdamped showing oscillation, to critically damped when the imaginary part goes to zero, to overdamped showing no oscillation. See this page on damping.
In[]:=
Manipulate[Plot[{​​Re[asoln[t]]/.numRules/.{b->bManip},​​Im[asoln[t]]/.numRules/.{b->bManip}},{t,0,tLim},​​GridLines->Automatic,PlotLegends->{"Real Part","Imaginary Part"},GridLines->All,Frame->True,FrameLabel->{{"Displacement",""},{"Time [s]","Displacement versus Time"}}],​​{{bManip,0.2},0,4,Appearance->"Open"},SaveDefinitions->True]
Out[]=
​
bManip
0.2
Real Part
Imaginary Part

Analytical Energy

We need generalized velocity via a new, “dummy” differentiation variable,
u
, to avoid collision with plotting variable
t
:
In[]:=
D[asoln[u],u]
Out[]=
1
2
1
2
u-
b
m
-
2
b
-4mκ
m

-
b
m
-
2
b
-4mκ
m

1
+
1
2
1
2
u-
b
m
+
2
b
-4mκ
m

-
b
m
+
2
b
-4mκ
m

2
Take only the real parts. Don’t be tempted to take the magnitude of a complex-valued energy! It mixes in the non-physical imaginary part.
In[]:=
ClearAll[analyticalEnergy];​​analyticalEnergy[t_]:=V[Re[asoln[t]]]+T[Re[D[asoln[u],u]]]/.{u->t};
In[]:=
Manipulate[​​Plot[analyticalEnergy[t]/.numRules/.{b->bManip},{t,0,tLim},GridLines->All,Frame->True,FrameLabel->{{"Energy [J]",""},{"Time [s]","Analytical Energy versus Time"}}],​​{{bManip,0.2},0,4,Appearance->"Open"},SaveDefinitions->True]
Out[]=
​
bManip
0.2
Notice that the energy is conserved only when
b=2
, at critical damping, but not otherwise. Such is expected from the physics: the DHO is a non-conservative system. Near critical damping
b=2
, no “bumpy” structure is seen.

Discrete Solution

Reference 2 gives us the forced discrete Euler-Lagrange Equations:
Guess that the discrete generalized forces are derivatives of the damping force. This guess does not introduce risky physics because we can always check the undamped case. In other cases, the results are so plausible that we are satisfied with the guess.
and write
Pick the solution out of Wolfram’s structure and apply the arithmetic rules:

Compare Solutions

Displacement

Wolfram could not solve the recurrence exactly within a few minutes. However, I left the expression in an unevaluatable cell in case you’d like to try it.
We’ll settle for a direct, brute-force evaluation of the recurrence.

Energies

We need velocities for kinetic energy.
The discrete solution tracks the analytical solution fairly well, at least visually. However, the energy difference shows periodic structure for all damping constants and time steps.
The RMS energy error shows a peak at critical damping, and interesting structure for other damping constants.

Conclusion

The damped harmonic oscillator shows periodic energy noise at all values of the damping constant and time step that we tried. The energy noise peaks at critical damping. It scales downward with time step in an approximate linear relationship in the logarithms.
This is good news and bad news. The good news is that the noise is manageable. The bad news is that we don’t fully understand it and we can’t make it go away. This is surprising given that the mathematics is purported to have the best possible energy behavior amongst all integration schemes because it follows the natural Lie-group structure of the problem. We must keep digging to understand the origin of this noise.
We also note that the run-time speed of the discrete method is too slow. We have work to do to speed it up.

References

1
.
Marsden and West, 2001, Discrete Mechanics and Variational Integrators, http://www.cds.caltech.edu/~marsden/bib/2001/09-MaWe2001/MaWe2001.pdf
2
.
Matthew West, Variational Integrators, Caltech Thesis, May 2004, https://thesis.library.caltech.edu/2492/1/west_thesis.pdf
3
.
Marin Kobilarov and Jerrold E. Marsden, Discrete Geometric Optimal Control on Lie Groups, IEEE Transactions on Robotics, Vol. 27, No. 4, August 2011.
4
.
Discrete damped harmonic oscillator​
by Brian Beckman​
Wolfram Community, STAFF PICKS, May 13, 2024
​https://community.wolfram.com/groups/-/m/t/3174169
5
.
Perspectives on energy conservation: discrete variational spherical pendulum​
by Brian Beckman​
Wolfram Community, STAFF PICKS, April 14, 2025
​https://community.wolfram.com/groups/-/m/t/3444021
6
.
Energy perspectives on the discrete harmonic oscillator, Brian Beckman, April 2025, to appear.

CITE THIS NOTEBOOK

Energy perspectives: discrete damped harmonic oscillator​
by Brian Beckman​
Wolfram Community, STAFF PICKS, April 20, 2025
​https://community.wolfram.com/groups/-/m/t/3447615