0% found this document useful (0 votes)
38 views13 pages

Duffing Oscillator

Notes for clarification addition

Uploaded by

aneeshraman2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views13 pages

Duffing Oscillator

Notes for clarification addition

Uploaded by

aneeshraman2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Computer Laboratory Manual - II

NUMERICAL SIMULATIONS IN PHYSICS USING PYTHON - Practical


(Core Course - XII)

M. Sc. Physics - III Semester


Course Code: 23PPH4CC15P2

Dr. A. Mohamed Saleem


Dr. A. Ishaq Ahamed

Postgraduate and Research Department of Physics


JAMAL MOHAMED COLLEGE (Autonomous)
Accredited with A++ Grade by NAAC (4th Cycle) with CGPA 3.69 out of 4.0
Affiliated to Bharathidasan University
TIRUCHIRAPPALLI -620 020

November 23, 2024


2
Contents

1 Solution of ODEs using RK4 Method: Duffing Oscillator 5


1.1 Aim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2 RK4 Algorithm for First Order ODEs . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 RK4 Method for Second Order ODEs . . . . . . . . . . . . . . . . . . . . . . . . 6
1.4 Duffing Oscillator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.5 Numerical Simulation of the Dynamics . . . . . . . . . . . . . . . . . . . . . . . 8
1.6 Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.7 Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.8 Additional Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

3
4 CONTENTS
Chapter 1

Solution of ODEs using RK4 Method:


Duffing Oscillator

1.1 Aim
1. To solve numerically the equation of motion of a Duffing Oscillator using RK4 Method.

2. To analyse the various dynamical behaviours of the Duffing oscillator such as the fixed
point, period 1-T, period 2-T oscillations, single band chaos and double band chaos
behaviour by varying the control parameter.

3. To plot the time series and phase portraits for these behaviours using Matplotlib libraray.

1.2 RK4 Algorithm for First Order ODEs


The Runge-Kutte IV order Method is a most commonly used method for solving ordinary
differential equations. If

f (x, t) = 0. (1.1)

is a first order differential equation in one independent variable, say t and one dependent
variable x, then the RK4 algorithm is given as

m1 = f (ti , xi )
!
h m1 h
m2 = f ti + , xi +
2 2
!
h m2 h
m3 = f ti + , xi +
2 2
m4 = f (ti + h, xi + m3 h)
m1 + 2m2 + 2m3 + m4
 
xi+1 = xi + h (1.2)
6

Here the coefficients mi s are the slope dx


dt
at various points defined by the interval h.

5
6 CHAPTER 1. SOLUTION OF ODES USING RK4 METHOD: DUFFING OSCILLATOR

1.3 RK4 Method for Second Order ODEs


Any second order differential equation can be converted into a set of two coupled first order
differential equations. This algorithm can be extended to such a coupled set of two first order
differential equations, say
f1 (x, y, t) = 0,
f2 (x, y, t) = 0. (1.3)
where the independent variable is t and the dependent variables are x and y. The extended
RK4 algorithm is given as
k1 = f1 (ti , xi , yi )
m1 = f1 (ti , xi , yi )
!
h k1 h m1 h
k2 = f ti + , xi + , yi +
2 2 2
!
h k1 h m1 h
m2 = f ti + , xi + , yi +
2 2 2
!
h k2 h m2 h
k3 = f ti + , xi + , yi +
2 2 2
!
h k2 h m2 h
m3 = f ti + , xi + , yi +
2 2 2

k4 = f (ti + h, xi + k3 h, yi + m3 h)
m4 = f (ti + h, xi + k3 h, yi + m3 h)

k1 + 2k2 + 2k3 + k4
!
xi+1 = xi + h
6
m1 + 2m2 + 2m3 + m4
 
yi+1 = yi + h (1.4)
6
This RK4 algorithm can be implemented using user defined functions. However the Scipy
module provides us an easy and accurate implementation of RK4 algorithm, namely the
odeint() module. Hence we will be using this in our numerical integration of the Duffing
oscillator.

Time Series Plots


Time series plots are the geometric representation of the dynamical variables of the system as
a function of time. If the time plot maintains a constant amplitude as time progresses, then
we say that the system is stable. If however the amplitude of the dynamical variable decreases
successively to zero as time progresses, the system is said to be dissipative and will cease to
exhibit any motion as time elapses. If on the other hand the amplitude of the dynamical
variable goes on increasing with time then we say that the system is a regenerative one and will
become unstable as time progresses. Further from the periodicity of the dynamical variable,
the time evolution behaviour of the system can be observed.
1.4. DUFFING OSCILLATOR 7

Phase Portraits
A phase portrait is an elegant geometric description of the dynamics of the system in a 2N
dimensional space. Here N refers to the number of independent variables or coordinates required
to describe adequately the dynamics of the system. As more than two dimensions cannot
be represented practically, it is generally visualized in an abstract manner. For the sake of
simplicity, a phase space is a position-velocity space or position-momentum space.
Phase Points
A point in the phase space denotes the physical state of the system at a given instant of
time.
Phase Trajectory
As time progresses the state of the system may change. Then naturally the point in phase
space also moves to represent the new states of the system. The path followed by the
phase point in phase space as the system dynamics evolves is called as a phase trajectory.
Attractors and Repellers
The equilibrium states of nonlinear systems can be represented by attractors and repellers.
If the system is in stable equilibrium, then all the phase trajectories approach a particular
point in phase space that correspond to the equilibrium state, as time progresses. Then
such a point is called as an attractor.
If however the system is in an inequilibrium state, then the phase trajectories will get
repelled away from the phase point representing the inequilibrium state. Such points are
called as repellers.

1.4 Duffing Oscillator


Duffing oscillator is a ubiquitous nonlinear system useful in modelling mathematically many
problems in the fields of physics, engineering and biology. It was first introduced by the Dutch
physicists von Duffing in 1918 to describe the hardening spring effect observed in mechanical
problems. It is represented as

ẍ + αẋ + ω02 x + βx3 = f sin(ωt), α > 0. (1.5)

Here α is the damping constant, ω0 is the natural angular frequency and β is the stiffness
constant, f is the amplitude and ω is the angular frequency of the external driving signal. The
above Eqn. 1.5 is a second order ordinary differential equation which is difficult to solve. It
can however be converted into a set of two coupled first order differential equations, namely
dx
= y,
dt
dy
= −αy − ω02 x − βx3 + f sin(ωt). (1.6)
dt
This set of coupled first order differential equations can be solved in Python using the odeint()
built in numerical integrator of scipy.integrate package. The general syntax of the odeint()
module as given in the Scipy manual is listed below.
1 from s c i p y . i n t e g r a t e import o d e i n t
2 s o l = o d e i n t ( func , y0 , t , a r g s =(alpha , beta , omega 0 , omega , f ) )
8 CHAPTER 1. SOLUTION OF ODES USING RK4 METHOD: DUFFING OSCILLATOR

• Here func is used to define the set of odes of the system. We name it here as Duff Osc(y,t)

• y0 is an array to contain the initial values of the variables x(t) and y(t). Here we name
this as initial state = np.array[x initial, y initial] .

• t is an array that contains the intervals at which the script is to be evaluated and is
defined as t = np.arange(0.0, 1500, h)

• args is a tuple containing the values of the parameters of the system, namely args =
(α, β, ω0 , ω, f ).

• sol is an array to collect the solution of the odes. Here we name it as y = sol

The function to define the Eqns. 1.6 is listed below


1 d e f Duff Osc ( y , t ) :
2 ”””
3 f u n c t i o n d e f i n i n g the equations f o r the Duffing O s c i l l a t o r
4 ”””
5 F1 = y [ 1 ]
6 F2 = −alpha ∗y [1] − omega 0 ∗y [0] − beta ∗y [ 0 ] ∗ ∗ 3 + f ∗np . s i n ( omega∗ t )
7

8 F = np . a r r a y ( [ F1 , F2 ] )
9 return F

1.5 Numerical Simulation of the Dynamics


The full Python script to solve numerically the Duffing oscillator equations 1.6 is listed below.
1

2 ”””
3 d u f f i n g . py
4 To p l o t t h e time p l o t s and phase p o r t r a i t s o f a D u f f i n g O s c i l l a t o r
5

6 ”””
7

8 import numpy as np
9 import m a t p l o t l i b . p y p l o t as p l t
10 from s c i p y . i n t e g r a t e import o d e i n t
11

12 N max =150000
13 t r a n s i e n t s = 70000
14

15 x i n i t i a l = −0.1
16 v i n i t i a l = −0.1
17

18 alp h a = 0 . 5
19 omega = 1 . 0
20 omega 0 = −1
1.5. NUMERICAL SIMULATION OF THE DYNAMICS 9

21 beta = 1.0
22

23 ”””
24 f = c o n t r o l parameter
25 f = 0 . 3 3 −−−> p e r i o d −1T
26 f = 0 . 3 5 −−−> p e r i o d −2T
27 f = 0.357−−−> p e r i o d −4T
28 f = 0.365−−−> s i n g l e band chaos
29 f = 0 . 4 2 −−−> double band chaos
30 ”””
31

32 f = 0.033
33

34 h = 0.01
35

36 i n i t i a l s t a t e = np . a r r a y ( [ x i n i t i a l , v i n i t i a l ] )
37 t = np . a range ( 0 . 0 , 1 5 0 0 , h )
38

39 d e f Duff Osc ( y , t ) :
40 ”””
41 f u n c t i o n d e f i n i n g the equations f o r the Duffing O s c i l l a t o r
42 ”””
43 F1 = y [ 1 ]
44 F2 = −alpha ∗y [1] − omega 0 ∗y [0] − beta ∗y [ 0 ] ∗ ∗ 3 + f ∗np . s i n ( omega∗ t )
45

46 F = np . a r r a y ( [ F1 , F2 ] )
47 return F
48

49 y = o d e i n t ( Duff Osc , i n i t i a l s t a t e , t )
50 y = y[ transients : ]
51 t = t [ transients : ]
52

53 xp = [ ]
54 yp = [ ]
55

56

57 f o r j i n r ange ( N max−t r a n s i e n t s ) :
58 xp . append ( y [ j , 0 ] )
59 yp . append ( y [ j , 1 ] )
60

61 # −−−−−−−−−Time P l o t s o f th e D u f f i n g o s c i l l a t o r −−−−−−−−−−−−
62

63 p l t . f i g u r e ( f i g s i z e =(10 , 8 ) )
64 plt . subplot (2 ,1 ,1)
65 p l t . p l o t ( t , xp , ’ r ’ )
66 p l t . xlim ( 7 0 0 , 1 0 0 0 )
67 p l t . x l a b e l ( ’ Time ’ )
10CHAPTER 1. SOLUTION OF ODES USING RK4 METHOD: DUFFING OSCILLATOR

68 plt . ylabel ( ’x( t ) ’ )


69

70 # −−−−−−−−−Phase P o r t r a i t o f th e D u f f i n g o s c i l l a t o r −−−−−−−−−−−−
71 plt . subplot (2 ,1 ,2)
72 p l t . p l o t ( xp , yp , ’ r ’ , l i n e w i d t h = 0 . 6 )
73 plt . xlabel ( ’x( t ) ’ )
74 plt . ylabel ( ’y( t ) ’ )
75

76 p l t . s a v e f i g ( ” d u f f i n g . png ” )
In this Python script, we

1. import the numpy module from the Numerical Python package and name it as np for
short for arithmetic calculations

2. import odeint module from the scipy.integrate package for numerical integration of
ode’s.

3. define a function Duffing Osc to realize the equations of the Duffing oscillator

4. set the oscillator parameters as α = 0.5, ω = 1.0, ω02 = −1.0, β = 1.0. The amplitude of
the external driving force f is considered as the control parameter.

5. take the time of integration as t = 1000 starting from the instant t = 0.0 and incrementing
it in steps of h = 0.01.

6. declare an array initial state to contain the initial values of the normalized voltage
x(t) and current y(t) variables.

7. set the maximum number of iterations N max = 150000 and step size for the RK4
algorithm as h = 0.01.

8. The transients to be cut-off is set as transients = 70000

1.6 Procedure
1. set the value of the control parameter to f = 0.33 for obtaining period-1T behaviour.

2. invoke odeint module and collect the values of the normalized voltage and current in an
array y.

3. import pyplot module from the matplotlib plotting package as plt to plot the time
series plot, phase portrait and the resonance curves and save the figure as duffing1.png
using the syntax
1 p l t . s a v e f i g ( ” d u f f i n g 1 . png ” )

4. run the script for different values of the control parameter f = 0.35 for period -2T,
f = 0.365 for single-band chaos and f = 0.42 for double-band chaos and save the figures
appropriately using the syntax listed above.
1.6. PROCEDURE 11

Period-1T Behaviour
By setting the value of the control parameter to f = 0.33, the system exhibits period-1T
behaviour. The time series of the x(t) variable and the phase-portrait in the (x − y) plane
corresponding to this period-1T behaviour are shown in the upper and the lower panes of Fig.
1.1.

Figure 1.1: The time series of the x(t) variable Figure 1.2: The time series of the x(t) variable
and the phase portrait in the (x − y) plane for and the phase portrait in the (x − y) plane for
f = 0.33 pertaining to the period-1T behaviour. f = 0.35 pertaining to the period-2T behaviour.

Period-2T Behaviour
By setting the value of the control parameter to f = 0.35, the system changes to period-2T
behaviour. The time series of the x(t) variable and the phase-portrait in the (x − y) plane
corresponding to this period-2T behaviour are shown in the upper and lower panes of Fig. 1.2.

Single Band Chaos


By setting the value of the control parameter to f = 0.365, the system exhibits single band
chaos. The time series of the x(t) variable and the phase-portrait in the (x − y) plane corre-
sponding to this single and chaotic behaviour are shown in the upper and the lower panes of
Fig. 1.3.

Double Band Chaos


By setting the value of the control parameter to f = 0.42, the system changes to double band
chaotic behaviour. The time series of the x(t) variable and the phase-portrait in the (x−y) plane
corresponding to this double band chaos are shown in the upper and lower panes of Fig. 1.4.
12CHAPTER 1. SOLUTION OF ODES USING RK4 METHOD: DUFFING OSCILLATOR

Figure 1.3: The time series of the x(t) variable Figure 1.4: The time series of the x(t) variable
and the phase portrait in the (x − y) plane for and the phase portrait in the (x − y) plane for
f = 0.365 pertaining to single band chaotic be- f = 0.35 pertaining to double band chaotic be-
haviour. haviour.

1.7 Result
1. A Python script to solve the set of two coupled first order ordinary differential equations
for the Duffing oscillator is run for different values of the control parameter f and the
time plots and phase portraits obtained

2. The various dynamical behaviours such as period-1T, period-2T, single-band chaos and
double-band chaos are identified using the time plots and phase portraits
1.8. ADDITIONAL EXERCISES 13

1.8 Additional Exercises


1. Run the Python script setting transients = 0 and Nmax = 10000 and plot the timeplot
and phase portrait for this case. Identify whether the fixed point is an attractor or repeller.

2. Modify the Python script to draw the Poincaré map of the Duffing oscillator.

3. Iterate this modified script for the control parameter values 0.33 < f < 0.365 to draw the
bifurcation diagram of the system.

You might also like