0% found this document useful (0 votes)
14 views6 pages

Paper Oscillator

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)
14 views6 pages

Paper Oscillator

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/ 6

WSEAS TRANSACTIONS on ADVANCES in ENGINEERING EDUCATION

DOI: 10.37394/232010.2023.20.1 Joan Jani

Simulation of Chaotic Operation Of A Damped Driven Pendulum Using


Python
JOAN JANI
Department of Engineering Physics
Polytechnic University of Tirana
Rr. S. Delvina, 1001
ALBANIA
Abstract: In this paper, we are presenting a new pedagogical method for the introduction of the study of non-
linear systems. Our approach is based on the use of open-source software which is publicly available. In response
to this motivation, we are using the Python programming language which offers a holistic approach to scientific
research. We will present the analysis of the pendulum motion under the influence of an external force. The
differential equation governing the system will be presented and solved using numerical methods. Moreover, the
phase diagram of the system will be presented for various system parameters. We will describe the transition to
a chaotic operation and the key factors of this procedure. The chaotic behaviour is verified by calculating the
maximum Lyapunov exponent. This pedagogical approach emerging is based on the physical properties of the
system and not on the numerical methods used so that the student can understand the dynamics of the system more
comprehensively.
Key-Words: Chaos, simple harmonic oscillator, damped driven pendulum, phase space, Lyapunov exponent

Received: October 19, 2021. Revised: October 23, 2022. Accepted: November 25, 2022. Published: January 25, 2023.

Introduction suited for developing physics-based models and sim-


ulations due to its powerful graphics capabilities. Fi-
Computer modeling is an essential part of engineer- nally, Python is a great choice for physics research
ing education, as it provides students with the op- and engineering, as it can be used to automate data
portunity to develop and apply their knowledge in a analysis and the development of models and simula-
practical setting. By utilizing computer models, en- tions. The Python programming language has been
gineers can gain a better understanding of how dif- generally accepted as a scientific tool in the field of
ferent components interact within complex systems, the study of dynamical systems, [4].
[1]. For this purpose, the knowledge of computer
programming should be an integral part of the edu- There are several publicly available packages for
cation of engineers and scientists. In the past, lan- the Python programming language that offer ready-
guages such as Fortran, C, C++, etc. have been used to-use numerical methods, [5]. The use of these
for expressing the mathematical models which are de- methods focuses students’ interest in understanding
scribing the system, [2]. Using these languages re- the model and the main parameters and constants
quires experience and a deep understanding of how that govern it. As an example of the application of
computers work which should not be a prerequisite these methods, we will present the use of the function
for a new scientist to get an introduction to the field odeint() for solving differential the system of dif-
of dynamical system study. On the other hand, the ferential equations which describe the behavior of the
Python programming language offers a user-friendly system could be found in the library scipy which is
working environment, offers fairly good documenta- a scientific computation library that apart from ODE
tion, and can be used even by the new scientist, [3]. (Ordinary Differential Equations) solvers offers mod-
Python is an ideal language for physics simula- ules for optimization, integration, interpolation, linear
tion and modeling due to its ease of use, flexibil- algebra, FFT (Fast Fourier transform ), signal, and im-
ity, and availability of a wide range of libraries for age processing, [6]. The use of Python for teaching
physics. Python allows for fast prototyping of simu- purposes in the context of courses such as Computa-
lations and models and can be used to quickly develop tional Physics and Dynamical Systems is constantly
user interfaces for visualization and control. Addi- gaining ground over other languages, [7].
tionally, Python can be used to integrate data from Chaos is the appearance of a lack of order in a
different sources, such as sensor data, to develop com- system that nevertheless follows laws or rules. [8]
plex models and simulations. Python is also well- Chaos is present in systems with non-linear behavior,

E-ISSN: 2224-3410 1 Volume 20, 2023


WSEAS TRANSACTIONS on ADVANCES in ENGINEERING EDUCATION
DOI: 10.37394/232010.2023.20.1 Joan Jani

the non-linearity in a system means that the measured For our purpose, we will use the numpy which is a
values in the output of the system are not propor- Python library used for working with arrays, and the
tional to the values of the input. The presence of non- matplotlib which is a plotting library for the Python
linearity in a system does not mean that the system programming language.
will behave chaotically but a form of non-linearity if
required to achieve chaotic behavior, [9]. A system # Oscilator
showing chaotic behavior undergoes transitions be- from s c i p y . i n t e g r a t e import o d e i n t
tween non-chaotic and chaotic states in general, [10]. import numpy a s np
A similar approach could be found in [11] where the import m a t p l o t l i b . p y p l o t a s p l t
transition to chaos is made by changing the angular # System Parameters
frequency of external force, in our approach the tran- [ k , m] = [ 9 , 1 ]
sition to chaos is made by changing the amplitude of # I n i t i a l Conditions
the external force applied to the system. x = [5 ,0]
# Simulation parameters
N = 1000
1 Simple oscillator t = np . l i n s p a c e ( 0 , 1 0 ,N)
We begin our pedagogical method by using the inte- def f ( x , t ) :
gration method on a well-known problem such as that d v d t = −( k /m) * x [ 0 ]
of the oscillator. dxdt = x [1]
return [ dxdt , dvdt ]
solution = odeint ( f , x , t )
The above code implements the solution. The
code can be easily understood by someone who has
no programming knowledge. In the beginning, the
necessary packages are loaded into the program. The
commands for the system parameters and the ini-
Fig. 1: Simple Harmonic Oscillator with a spring tial conditions are given following with the function
mass system f(x,t) with the differential equations of the system.
The function odeint solves the system and the results
In figure 1 a simple harmonic oscillator with a are saved to variable solution
spring mass system is presented. Since the constant
of elasticity of the spring is denoted with k and the plt . plot ( t , solution [: ,0])
mass of the object with m, we can express the differ- plt . x l a b e l ( ’ Time t [ s ] ’ )
ential equation of the motion of the system as follows: plt . y l a b e l ( ’ x [m] ’ )
plt . show ( )
d2 x To plot the solution which we are getting from
m = −kx (1)
dt2 odeint, the above code is used. The presented code
the system is governed by a second-order linear dif- uses the library matplotlib which has a very similar
ferential equation, [12]. To apply a numerical method syntax to MATLAB. The result is presented in figure (2)
of integration for equation (1) we rewrite it as a pair of from were the sinusoidal correlation of the position of
first-order differential equations, which are presented the oscillator in relation to time can be seen.
at equation (2). The phase space of the system is created with some
minor changes to the above code. (fig. 3). The phase
dv k dx diagram gives us an overview of the evolution of the
=− x and =v (2)
dt m dt system. Τhe position of the oscillator is shown on the
horizontal axis, while its speed is on the vertical axis.
We suppose the system with parameters k = Furthermore, from the Fig. 3 we observe that the pe-
9 N /m and m = 1 kg and with initial conditions riod of the system is √ T = 2.09 s as we expect from
x = 5 m and v = 0 m/s. The simulation will cal- the relation T = 2π/ k/m
culate the position and velocity for the time interval
of the oscillator from t = 0 s to t = 10 s. Will be cal-
culated N = 1000 points of the trajectory. Increas- 2 Driven damped pendulum
ing the sampling (N>1000) of the trajectory would Considering a simple pendulum in a constant gravita-
require more computing power to achieve the solu- tional field as it is presented in fig. 4. The equation of
tion, while reducing it would result in an increase motion is given by the differential equation 3 where
in the computational error of the numerical method. its current angle is denoted by θ and angular velocity

E-ISSN: 2224-3410 2 Volume 20, 2023


WSEAS TRANSACTIONS on ADVANCES in ENGINEERING EDUCATION
DOI: 10.37394/232010.2023.20.1 Joan Jani

2
x[m]

0 2 4 6 8 10
Time t[s]

Fig. 4: A simple pendulum


Fig. 2: Position of the oscillator vs time

Phase Space and θ̇ = 0 rad/s. The simulation will be performed


15 for 0 < t < 200 s using N = 20000 time samples.
10 # D r i v e n dumped pendulum
from s c i p y . i n t e g r a t e import o d e i n t
5 import numpy a s np
v[m/s]

0 import m a t p l o t l i b . p y p l o t a s p l t
5 # System Parameters
10 [ g , L , b ] = [ 1 0 , 10 , 0 . 5 ]
[w , A] = [ 2 / 3 , 1 ]
15
# I n i t i a l Conditions
4 2 0 2 4
x[m] θ = [0.5 ,0]
# Simulation parameters
Fig. 3: Phase space of the harmonic oscillator N = 20000
t = np . l i n s p a c e ( 0 , 2 0 0 ,N)
def f (θ , t ) :
by θ̇. d v d t = −( g / L) * np . s i n ( θ [ 0 ] ) . . .
−mlθ̈ = mg sin(θ) (3) . . . − b*θ [ 1 ] +A* np . s i n (w* t )
dθ d t = θ [ 1 ]
The reason that the small-angle approximation is r e t u r n [ dθ d t , d v d t ]
made is that otherwise the differential equation which solution = odeint ( f ,θ , t )
describes the system could not be solved using analyt- We run the code and the results are saved in the
ical methods. The solution of the equation could eas- variable “solution”. The time needed for the solu-
ily get using the code below. Regardless of the initial tion is relatively short although the number of cal-
conditions, the system will go to a steady state. [11] culation points is significantly larger. Then we pro-
Equation (4) shows the differential equation of a ceed with the creation of the corresponding graphical
damped-driven pendulum. The motion of the pendu- representations of the angular displacement with time
lum is damped by a force proportional to its velocity and phase space.
which is expressed in the equation with the term −β θ̇. The graph is shown in Fig. 5 where we see that
Moreover, equation (4) represents a pendulum with a after the time of 100 seconds the system performs
time-varying external force with amplitude A and fre- harmonic oscillation of constant amplitude and fre-
quency ω expressed by the term A sin(ωt). quency. At this point we let the students experiment
g with the various initial conditions of the system. By
θ̈ = − sin θ − β θ̇ + A sin(ωt) (4) choosing different initial conditions we see that the
L system exhibits the same behavior and finally per-
For solving the differential equation (4) we con- forms harmonic oscillations.
sider a system with parameters, g = 10 m/s2 , L = To construct the phase diagram we will use the
10 m, β = 0.5 N s/m, A = 1 N and ω = 2, 3 rad/s. states of the system after harmonic oscillation is
The simulation starts from initial position θ = 0.5 rad reached. We should also ensure that the value of the

E-ISSN: 2224-3410 3 Volume 20, 2023


WSEAS TRANSACTIONS on ADVANCES in ENGINEERING EDUCATION
DOI: 10.37394/232010.2023.20.1 Joan Jani

o=1.0, = 0.5, A=1.35, =0.667


3 2
2
1

Speed [rad/s]
1
[rad]

0 0

1
1
2
2
3 o=1.0, = 0.5, A=1, =0.667
0 25 50 75 100 125 150 175 200 3 2 1 0 1 2 3
Time t[s] Position [rad]

Fig. 5: Position of the oscillator vs time Fig. 7: Phase space of the system with A = 1.35 N

angle should not exceed the range −π ≤ θ ≤ π . o=1.0, = 0.5, A=1.45, =0.667

2
o=1.0, = 0.5, A=1, =0.667
1
2 Speed [rad/s]
0
1
Speed [rad/s]

1
0
2
1
3 2 1 0 1 2 3
Position [rad]
2
2 1 0 1 2 3 Fig. 8: Phase space of the system with A = 1.45 N
Position [rad]

Fig. 6: Phase space of the system with A = 1 N as it shown in Fig. 10. It has to be mentioned that
the motion of the system is not random. There is
In order to bring the system described by the equa- a periodicity but the period is large enough. This
tion (4) in chaotic operation we use the method of makes a small observation of the operation look to-
period doubling by changing the amplitude of the tally stochastic. In this state, the system is called a
external force. We have changed the angular fre- strange attractor. In this situation, a small change in
quency of the external force to ω = 2/3 rad/s and the initial conditions of the system will cause a big
we change the amplitude of the external force to A = difference in its motion, regardless that the shape of
[1.35, 1.45, 1.47, and 1.5] the phase space will not change.
The transition to chaos is shown in figure 7 where The chaotic operation could be characterized by
the phase space of the system is presented when the the rate of separation of infinitesimally close trajecto-
amplitude of the driving force is A = 1.35. The tra- ries. this is done by the calculation of the Lyapunov
jectory of the system in the phase space diagram is a exponent described below. The presence of chaos is
closed loop as expected. Increasing the amplitude to verified also in electronic circuits [13].
A = 1.45 the trajectory in the phase space is chang-
ing, and a second path is appearing denoting the pe- 3 Lyapunov exponent calculation
riod doubling of the system as it is shown in fig. 8. The chaotic behavior of a systems is verified by the
By increasing the amplitude to A = 1.47 the phase calculation of the Lyapunov exponent, [14]. The cal-
space diagram of the system changes again as it is pre- culation of this exponent is done by equation (5):
sented in figure 9. The system doubles its period, but
its behavior is still predictable.
1 ∑
N
δt,j
By choosing the amplitude of the external force δt ∼
= δ0 eλt λ= log (5)
A = 1.5 the operation of the system becomes chaotic N δ0,j
j=1

E-ISSN: 2224-3410 4 Volume 20, 2023


WSEAS TRANSACTIONS on ADVANCES in ENGINEERING EDUCATION
DOI: 10.37394/232010.2023.20.1 Joan Jani

o=1.0, = 0.5, A=1.47, =0.667


108
2

1 106
Speed [rad/s]

[radians]
0 104

1 102

2 100

3 2 1 0 1 2 3 0 25 50 75 100 125 150 175 200


Position [rad] t [s]

Fig. 9: Phase space of the system with A = 1.47 N Fig. 11: Exponential increase of the difference be-
tween two very close orbits with respect to time. The
red line represents the linear regression.
o=1.0, = 0.5, A=1.5, =0.667

2
4 Discussion
1 This approach is applied in the context of the Compu-
Speed [rad/s]

tational Physics course taught to second-year students


0 in the Applied Physics department of the Polytechnic
University of Tirana in Albania. After the explanation
1 of the basic principles governing the phenomenon, the
students, with the help of the lecturer, construct the
2
mathematical model that describes it. This model is
3 2 1 0 1 2 3 then run on the computer and the graphs describing
Position [rad] its evolution are created.
Our didactic approach presents clearly and com-
Fig. 10: Phase space of the system with A = 1.5 prehensively the transition of a nonlinear system from
order to chaos. The demonstration could be presented
in the lecture or a laboratory environment giving stu-
were λ is the Lyapunov exponent δt is the separation dents a hands-on experience in the field of dynamic
of trajectories after t time, δ0 is the initial separation. systems and chaos.
For discrete time systems, like in our case, N is the In the future, we will try to make a create the code
time samples needed for the calculation. In order to that implements the Poincare section of the system.
make the calculation, we simulate two trajectories of
the system whose initial conditions differ very little.
The initial conditions θ1o and θ2o where θ2o = θ1o +ϵ, 5 Conclusion
where ϵ is value very close to the machine epsilon, for In this paper, we demonstrate the use of Python pro-
our case ϵ = 2.220e − 16. so the initial separation of gramming language in simulating a simple linear sys-
the trajectories in the phase space is: tem. We showed that this methodology can be used in
simulations of a non-linear system. This demonstra-
δ0 = |θ2o − θ1o | tion can be used as a didactic tool for introduction to
dynamical system analysis of its elegance and ease of
After the calculation of each trajectory for the cor- use.
responding initial condition we calculate their differ-
ence through the relation.
References:
δ0 = ∆θ = |θ2 − θ1 | [1] E. A. Gago, C. L. Brstilo, and N. de Brito,
“Computational simulation: Multidisciplinary
the difference is growing exponentially with time as teaching of dynamic models from the linear
it is shown in fig. 11. The graph is assumed to be algebra perspective,” WSEAS Transactions On
linear when we create a log-linear plot. From our Advances In Engineering Education, 2022.
calculations, the Lyapunov exponent of this system
is λ = 0.06132158503950399. The positive value [2] P. W. Gaffney, “A performance evaluation of
verifies the chaotic operation as was expected. some fortran subroutines for the solution of

E-ISSN: 2224-3410 5 Volume 20, 2023


WSEAS TRANSACTIONS on ADVANCES in ENGINEERING EDUCATION
DOI: 10.37394/232010.2023.20.1 Joan Jani

stiff oscillatory ordinary differential equations,” [10] H. Nagashima, Y. Baba, and M. Nakahara, In-
ACM Transactions on Mathematical Software troduction to chaos: physics and mathematics
(TOMS), vol. 10, no. 1, pp. 58–72, 1984. of chaotic phenomena. CRC Press, 2019.
[3] P. Borcherds, “Python: a language for computa- [11] E. Ayars, “Computational physics with python,”
tional physics,” Computer Physics Communica- California State University, 2013.
tions, vol. 177, no. 1, pp. 199–201, 2007. Pro-
[12] K. N. Anagnostopoulos, Computational
ceedings of the Conference on Computational
Physics, Vol I: A Practical Introduction to Com-
Physics 2006.
putational Physics and Scientific Computing.
[4] B. W. l. Margolis, “Simupy: A python frame- Konstantinos Anagnostopoulos, 2014.
work for modeling and simulating dynamical [13] M. Hanias, I. Giannis, and G. Tombras, “Chaotic
systems,” Journal of Open Source Software, operation by a single transistor circuit in the
vol. 2, no. 17, p. 396, 2017. reverse active region,” Chaos: An Interdisci-
[5] D. R. Gwynllyw, K. L. Henderson, E. G. Guil- plinary Journal of Nonlinear Science, vol. 20,
lot, et al., “Using python in the teaching of nu- no. 1, p. 013105, 2010.
merical analysis.,” MSOR Connections, vol. 18, [14] J. Jani and P. Malkaj, “Numerical calculation of
no. 2, 2020. lyapunov exponents in various nonlinear chaotic
systems,” International Journal of Scientific &
[6] J. Nunez-Iglesias, S. Van Der Walt, and H. Dash-
Technology Research, vol. 3, no. 7, pp. 87–90,
now, Elegant SciPy: The art of scientific python.
2014.
” O’Reilly Media, Inc.”, 2017.
[7] J. Kusalaas, “Numerical methods in engineering
Contribution of Individual Authors to the
with python 3,” 2013.
Creation of a Scientific Article (Ghostwriting
[8] A. BenSaïda, “A practical test for noisy chaotic Policy)
dynamics,” SoftwareX, vol. 3-4, pp. 1–5, 2015. The author contributed in the present research, at all
stages from the formulation of the problem to the
[9] S. Strogatz, Nonlinear Dynamics and Chaos: final findings and solution.
With Applications to Physics, Biology, Chem-
istry, and Engineering. CRC Press, 2018. Sources of Funding for Research Presented in a
Scientific Article or Scientific Article Itself
No funding was received for conducting this study.
Conflict of Interest
The author has no conflict of interest to declare that
is relevant to the content of this article.

Creative Commons Attribution License 4.0


(Attribution 4.0 International, CC BY 4.0)
This article is published under the terms of the
Creative Commons Attribution License 4.0
https://creativecommons.org/licenses/by/4.0/deed.en
_US

E-ISSN: 2224-3410 6 Volume 20, 2023

You might also like