Using Mathematica to solve oscillator differential equations
Unforced, damped oscillator
General solution to forced harmonic oscillator equation (which fails when b^2=4k, i.e. at perfect damp-
ing). C[1] and C[2} are integration constants.
In[8]:= DSolve@x''@tD + b x'@tD + k x@tD � 0, x@tD, tD
Out[8]= ::x@tD ® ã
1
2-b- b
2-4 k t
C@1D + ã
1
2-b+ b
2-4 k t
C@2D>>
Now let's solve with boundary conditions: x=1 at t=0 and it is at rest. Then get a fully defined solution.
In[9]:= res1 = DSolve@8x''@tD + b x'@tD + k x@tD � 0, x@0D � 1, x'@0D � 0<, x@tD, tD
Out[9]= ::x@tD ®1
2 b2 - 4 k
-b ã
1
2-b- b
2-4 k t
+
b ã
1
2-b+ b
2-4 k t
+ ã
1
2-b- b
2-4 k t
b2
- 4 k + ã
1
2-b+ b
2-4 k t
b2
- 4 k >>
Set up a function to plot the result, here setting k=1 so the undamped frequency is 1,
and keeping the damping constant as a variable. Note the construction with two "/." in turn ("use the
assignment res1, and, within that, make the assignments to b and k").
In[10]:= res1plot@b0_D := Plot@x@tD �. res1 �. 8b ® b0, k ® 1<, 8t, 0, 20<,PlotRange ® 880, 20<, 8-1, 1<<, PlotStyle ® Thick, PlotLabel ® b0D
Here's what the oscillations look like with no damping:
In[11]:= res1plot@0D
Out[11]=
5 10 15 20
-1.0
-0.5
0.0
0.5
1.0
0
Underdamping (b^2 < 4 k). First, very weak damping (b=0.1),
which is the value used below for the driven oscillator.
In[12]:= [email protected]
Out[12]=
5 10 15 20
-1.0
-0.5
0.0
0.5
1.0
0.1
Next, a much more damped case (b=1).
In[13]:= res1plot@1D
Out[13]=
5 10 15 20
-1.0
-0.5
0.0
0.5
1.0
1
Essentially perfect damping (b^2=4k). The function is singular at b exactly
equal to 2 because the solution is not correct there.
In[14]:= [email protected]
Out[14]=
5 10 15 20
-1.0
-0.5
0.0
0.5
1.0
2.01
2 Oscillator.nb
Overdamping:
In[15]:= res1plot@3D
Out[15]=
5 10 15 20
-1.0
-0.5
0.0
0.5
1.0
3
Using animate to show how the behavior varies with damping:
In[16]:= Animate@res1plot@b0D, 8b0, 0, 4<D
Out[16]=
b0
5 10 15 20
-1.0
-0.5
0.0
0.5
1.0
1.41511
Perfect damping
Mathematica gets the perfectly damped case correct if it is asked for specifically:
In[17]:= res2 = DSolve@8x''@tD + 2 x'@tD + x@tD � 0, x@0D � 1, x'@0D � 0<, x@tD, tDOut[17]= 99x@tD ® ã
-t H1 + tL==
Oscillator.nb 3
In[18]:= res2plot = Plot@x@tD �. res2, 8t, 0, 20<,PlotRange ® 880, 20<, 8-1, 1<<, PlotStyle ® 8Thick, Red<D
Out[18]=
5 10 15 20
-1.0
-0.5
0.0
0.5
1.0
Comparing to almost perfect damping case shown above---identical to the eye.
In[19]:= Show@res2plot, [email protected]
Out[19]=
5 10 15 20
-1.0
-0.5
0.0
0.5
1.0
Mathematica can correctly find the perfect damping solution from the general solution by taking a limit.
In[20]:= LimitBx@tD �. First@res1D, b ® 2 k F
Out[20]= ã- k t J1 + k tN
Which agrees with “res2” for k=1
Forced, damped oscillator
General solution in forced case (except forcing amplitude is set to unity). Note the presence of the two
constants in the first two terms as above. Here there is also the long-time "particular solution".
4 Oscillator.nb
In[21]:= DSolve@8x''@tD + b x'@tD + k x@tD � Sin@om0 tD<, x@tD, tD
Out[21]= ::x@tD ® ã
1
2-b- b
2-4 k t
C@1D + ã
1
2-b+ b
2-4 k t
C@2D +
I4 Ib om0 Cos@om0 tD - k Sin@om0 tD + om02Sin@om0 tDMM �
-b2
+ b b2
- 4 k + 2 k - 2 om02
b2
+ b b2
- 4 k - 2 k + 2 om02 >>
Now include boundary conditions to completely fix the solution:
In[22]:= res3 = DSolve@8x''@tD + b x'@tD + k x@tD � Sin@om0 tD, x@0D � 1, x'@0D � 0<, x@tD, tD;
Now specify that b=0.1, k=1 (very underdamped) for definiteness, and make plots of the time depen-
dence as the driving frequency is varied.
In[23]:= res3f@t0_, omin_D = x@tD �. res3 �. 8b ® 0.1, k ® 1, om0 ® omin< �. t ® t0;
In[24]:= res3plot@omega_D := Plot@res3f@t, omegaD, 8t, 0, 200<,PlotRange ® 880, 200<, 8-5, 5<<, PlotStyle ® Thick, PlotLabel ® omegaD
The following plots show a quick scan as the driving frequency varies from smaller than the natural
frequency (which is unity) to above the natural frequency. We see the expected resonant behavior:
The smaller "wiggles" are at a frequency very close to the natural frequency, and correspond to the
damped solution to the homogeneous equation. Eventually, the particular solution takes over.
In[25]:= [email protected]
Out[25]=
50 100 150 200
-4
-2
0
2
4
0.2
Just below the resonant frequency: notice the increase in amplitude.
Oscillator.nb 5
In[26]:= [email protected]
Out[26]=
50 100 150 200
-4
-2
0
2
4
0.9
Just above the resonant frequency:
In[27]:= [email protected]
Out[27]=
50 100 150 200
-4
-2
0
2
4
1.1
Well above the resonant frequency: the amplitude has greatly decreased.
In[28]:= res3plot@2D
Out[28]=
50 100 150 200
-4
-2
0
2
4
2
Scanning through the frequencies:
6 Oscillator.nb
In[29]:= Animate@res3plot@omegaD, 8omega, 0, 2<D
Out[29]=
omega
50 100 150 200
-4
-2
0
2
4
1.72104
Oscillator.nb 7