Matlab and Python programming: how to get started

Matlab and Python programming: how to get started
Equipping readers the skills to write programs to explore complex systems and discover interesting patterns from big data is one of the main goals of this book. In this chapter, we explain how to do Matlab and
Python programming. While Matlab is important for studying both complex systems and big data, Python is
particularly convenient for dealing with big data. The materials presented here are chosen such that 1) after
studying this chapter, laymen will no longer be laymen, and 2) seasoned programmers will also feel inspired.
We organize the materials through five examples.
Example 1 Write a matlab code to generate a sine wave signal from
x(t) = sin(t)
(1)
To generate a curve, we have to decide which sampling time ∆t to use as well as how long the curve should
be plotted. To this end, we first note that sin(t) is a periodic function with period 2π. To generate a smooth
curve, we have to choose ∆t such that each period is sampled a few tens of times. To make the curve look like a
sine wave, we have to plot out a few periods of the signal. Based on these considerations, the complete matlab
code, saved as generate sinewave.m1 , is the following. Note lines 2-4 are the “header”. It is better to include
them in any of your matlab codes. The main commands for our purpose are the lines 6, 7, and 11. In particular,
line 6 says that t starts from 0 and ends at t = 6π. The sampling time interval is ∆t = 0.1. While line 7 is almost
self-explanatory, we note that in matlab, when evaluating trianglular functions, radians instead of angles are
used. Therefore, if you want to compute cos(1800 ), you write cos(pi), where pi denotes π. The “linewidth”
in line 11 specifies the thickness of the curve. Lines 12-15 specify how the curve is plotted. They should be
self-explanatory. The curve is plotted in Fig. 1(a).
What happens if a different sampling time interval ∆t is used? When a smaller ∆t is used, visually the curve
remains the same. However, if a much larger ∆t is used, the curve could look very different. To see this, you
may choose ∆t = 1.6. If you uncomment lines 8 and 9, execute them, and plot out x2 vs. t2, you get the curve
shown in Fig. 1(b). It is quite different from the sine wave everybody knows. This is because ∆t is too big!
So far, all is simple and almost trivial. Now let us play with something more fun. Specifically, we ask: how
may we generate other types of plots from the sine wave signal? One interesting solution is provided by the
time delay embedding technique of chaos theory, which we will study in depth later. As a 2-dimensional (2-D)
graphical representation, the technique tells us to construct vectors of the form:
V (i) = [x(i), x(i + L)]
(2)
Associating the 2 components with the x− and y− axes, the above equation amounts to plotting x(i+L) vs. x(i).
When L = 16, the plot is shown in Fig. 1(c) (blue curve). It is generated by the codes given at lines 17 and 18. It
is almost a perfect circle. Why is it so? The reason is L∆t = 1.6. It is very close to π/2. If we write x(i) as sin(t),
then x(i + L) is close to cos(t). Now recall that in polar coordinates, a circle is represented by [cos θ, sin θ]. That
is why the shape of the curve in Fig. 1(c) is almost a circle. The above argument makes it clear that L < 16 will
1 When
you save your matlab file, you must save it as .m!
1
make the circle-like curve in Fig. 1(c) an ellipse along the main diagonal, while 16 < L < 32 an ellipse along
the 1350 diagonal.
In terms of the terminology of nonlinear dynamics, Fig. 1(c) is a phase space representation. The shape
of the curve will not change with the total time interval sin(t) is evaluated, so long as the total time interval is
slightly longer than one period. That is the beauty of phase space representation — it is more able to capture
the invariants of a dynamical process.
To better appreciate the above statements, let us re-consider the case of ∆t = 1.6. Now we should choose
L = 1 so that the actual time delay L∆t is the same as before. Based on the curve shown in Fig. 1(b), we get
Fig. 1(d). It is not the same as Fig. 1(c). Why? The reason is successive points in Fig. 1(d) are connected.
Since ∆t is so big, successive points on the circle are actually quite far apart. When connected, something like
a circular disk emerges in Fig. 1(d). If we do not connect the points, we get the discrete points designated by
red diamonds in Fig. 1(c). This is obtained by un-commenting line 19 in the code and executing it. They all
perfectly fall on the unit circle. This is the beauty of invariant dynamics!
1
2
3
4
% generate
clc ;
clear all ;
close all ;
s i n e w a v e .m
%r e m o v e s a l l l i n e s i n t h e command window
%d e l e t e s a l l s t o r e d v a r i a b l e s i n t h e w o r k s p a c e
%c l o s e s a l l o f t h e f i g u r e s g e n e r a t e d i n t h e p r o g r a m
5
6
7
8
9
t = 0:0.1:3∗ pi ∗2;
x1 = s i n ( t ) ;
%t 2 = 0 : 1 . 6 : 2 0 ∗ p i ∗ 2 ;
%x2 = s i n ( t 2 ) ;
10
11
12
13
14
15
f i g u r e ; p l o t ( t , x1 , ’ l i n e w i d t h ’ , 2 ) ;
axis tight ;
s e t ( gca , ’ f o n t s i z e ’ , 1 6 ) ; % s p e c i f i e s s i z e o f t h e numbers on x− and y−a x i s
xlabel ( ’ t ’ , ’ f o n t s i z e ’ ,17) ;
ylabel ( ’ sin ( t ) ’ , ’ f o n t s i z e ’ ,17) ;
16
17
18
19
20
21
22
L=16;
f i g u r e ; p l o t ( x1 ( 1 : end−L ) , x1 ( 1 +L : end ) , ’ l i n e w i d t h ’ , 2 ) ; h o l d on ;
%p l o t ( x2 ( 1 : end −1) , x2 ( 1 + 1 : end ) , ’ dr ’ , ’ l i n e w i d t h ’ , 2 , ’ m a r k e r s i z e ’ , 2 ) ;
s e t ( gca , ’ f o n t s i z e ’ , 1 6 ) ;
xlabel ( ’x ( t ) ’ , ’ f o n t s i z e ’ ,17) ;
y l a b e l ( ’ x ( t +L ) ’ , ’ f o n t s i z e ’ , 1 7 ) ;
2
(a) ∆t=0.1
sin(t)
0.5
0
−0.5
0
5
10
15
t
(b) ∆t=1.6
sin(t)
0.5
0
−0.5
0
20
40
60
(c)
x(t+L)
80
t
1
0.5
0.5
0
0
−0.5
−0.5
0
1
120
(d)
1
−1
−1
100
−1
−1
x(t)
0
1
x(t)
Figure 1:
Example 2 Explore the dynamics described by the following equation:
√
x(t) = sin(t) + a sin( 5t), a = 0.3
(3)
This signal is called a quasi-periodic signal. It describes an important type of motion. Since x(t) is a superposition of two sinusoidal signals, is it a periodic signal? The answer is no. To see this, we note that if x(t)
is periodic, then there exists a period T √
such that x(t + T ) √
= x(t). Now, sin(t) is periodic
√ with T1 = 2πn1 .
Similarly, there exists a T2 such that sin( 5(t + T2 )) = sin( 5t). This yields T2 = 2πn2 / 5. For x(t) to be
3
√
periodic, among T1 and T2 , there must exist a common T . Consequently, n1 = n2 / 5. But this equation cannot
hold, since n1 , n2 are integers and n1 /n2 is a rational number. Therefore, the signal is aperiodic.
To explore the dynamics described by Eq. (3), we may plot the 2-D phase space diagrams. The basic code
is given by lines 4-7 and 10 below. The plot is shown in Fig. 2(a). It looks like the tire of a car. In fact, in
nonlinear dynamics, it is called a torus. To better indicate how the signal differs from a true periodic signal, we
have used lines 11 and 12 to indicate the start and end points of the phase space diagram by a red circle and a
red diamond, respectively. Both are “open ended”. This is the essence of aperiodic signals.
To better understand
the difference between quasi-periodic and periodic signals, let us examine what hap√
pens if we replace 5 by its very crude approximation, 2.2. With the codes at lines
√ 19-23, we obtain Fig. 2(b).
This is clearly a periodic signal, albeit with a very long period. If we replace 5 by a better approximation,
then the period will become longer. These considerations should help one understand our earlier arguments
better.
1
2
% g e n e r a t e t o r u s .m
clc ; clear all ; close all ;
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
a = 0.3;
t = 0:0.05:15∗ pi ∗2;
x = s i n ( t ) +a ∗ s i n ( s q r t ( 5 ) ∗ t ) ;
L = 24;
figure ;
subplot (211) ;
p l o t ( x ( 1 : end−L ) , x ( 1 +L : end ) , ’ l i n e w i d t h ’ , 1 . 5 ) ; h o l d on ;
p l o t ( x ( 1 ) , x ( 1 +L ) , ’ o r ’ , ’ m a r k e r s i z e ’ , 1 0 ) ;
p l o t ( x ( end−L ) , x ( end ) , ’ d r ’ , ’ m a r k e r s i z e ’ , 1 0 ) ;
s e t ( gca , ’ f o n t s i z e ’ , 1 6 ) ;
xlabel ( ’x ( t ) ’ , ’ f o n t s i z e ’ ,17) ;
y l a b e l ( ’ x ( t +L ) ’ , ’ f o n t s i z e ’ , 1 7 ) ;
t e x t ( −1.3 ,1.3 , ’ ( a ) ’ , ’ f o n t s i z e ’ ,16) ;
xlim ( [ − 1 . 5 , 1 . 5 ] ) ; ylim ( [ − 1 . 5 , 1 . 5 ] ) ;
18
19
20
21
22
23
24
25
26
27
28
x = s i n ( t ) +a ∗ s i n ( 2 . 2 ∗ t ) ;
subplot (212) ;
p l o t ( x ( 1 : end−L ) , x ( 1 +L : end ) , ’ l i n e w i d t h ’ , 1 . 5 ) ; h o l d on ;
p l o t ( x ( 1 ) , x ( 1 +L ) , ’ o r ’ , ’ m a r k e r s i z e ’ , 1 0 ) ;
p l o t ( x ( end−L ) , x ( end ) , ’ d r ’ , ’ m a r k e r s i z e ’ , 1 0 ) ;
xlim ( [ − 1 . 5 , 1 . 5 ] ) ; ylim ( [ − 1 . 5 , 1 . 5 ] ) ;
s e t ( gca , ’ f o n t s i z e ’ , 1 6 ) ;
xlabel ( ’x ( t ) ’ , ’ f o n t s i z e ’ ,17) ;
y l a b e l ( ’ x ( t +L ) ’ , ’ f o n t s i z e ’ , 1 7 ) ;
t e x t ( −1.3 ,1.3 , ’ ( b ) ’ , ’ f o n t s i z e ’ ,16) ;
4
1.5
(a)
1
x(t+L)
0.5
0
−0.5
−1
−1.5
−1
0
1
x(t)
1.5
(b)
1
x(t+L)
0.5
0
−0.5
−1
−1.5
−1
0
1
x(t)
Figure 2:
Example 3 Examine an exponential function:
x(t) = e−at , a = 0.5
(4)
Now it should be a simple matter to write a few lines of codes to generate the function. Please see the codes
at lines 3-4 and Fig. 3(a). Now let us ask a question: If we observe a curve like that in Fig. 3(a), how do we
determine whether it is an exponential function or not, and if yes, how do we estimate its parameter? The
answer lies in taking logarithm of x(t). Then the curve becomes a straight line — the slope of the straight line
5
should be −0.5. Please see the code at line 14 and Fig. 3(b). Note that exponential functions can appear in
many different contexts, and the simple technique discussed here can be very useful in all those contexts.
1
clc ; clear all ; close all ;
2
3
4
5
6
7
8
9
10
11
a = 0.5;
t = 0:0.1:10;
x = exp (−a ∗ t ) ;
figure ; subplot (211) ;
plot ( t , x , ’ linewidth ’ ,2) ;
s e t ( gca , ’ f o n t s i z e ’ , 1 6 ) ;
xlabel ( ’ t ’ , ’ f o n t s i z e ’ ,18) ;
ylabel ( ’x ( t ) ’ , ’ f o n t s i z e ’ ,18) ;
t e x t (9 ,0.9 , ’ ( a ) ’ , ’ f o n t s i z e ’ ,16) ;
12
15
16
17
18
subplot (212) ;
p l ot ( t , log ( x ) , ’ linewidth ’ ,2) ;
s e t ( gca , ’ f o n t s i z e ’ , 1 6 ) ;
xlabel ( ’ t ’ , ’ f o n t s i z e ’ ,18) ;
ylabel ( ’ ln ( x ) ’ , ’ f o n t s i z e ’ ,18) ;
t e x t (9 , −.5 , ’ ( b ) ’ , ’ f o n t s i z e ’ ,16) ;
1
(a)
0.8
x(t)
14
0.6
0.4
0.2
0
0
2
4
6
8
10
t
0
(b)
−1
ln(x)
13
−2
−3
−4
−5
0
2
4
6
t
Figure 3:
6
8
10
Example 4 Examine a power-law function:
x(t) = t −1.5 , t ≥ 1
(5)
As far as coding is concerned, this example is as simple as the last one. Please see the codes at lines 3-5 and
Fig. 4(a). Now our question becomes the following: If we have observed Fig. 4(a), how do we determine
whether it is a power-law or not, and if yes, how do we estimate its parameters? The idea lies in plotting out the
curve in double-logarithmic scale. Then we get a straight line — the slope of the straight line should be −1.5.
Please see the code at line 14 and Fig. 4(b).
1
clc ; clear all ; close all ;
2
3
4
5
6
7
8
9
10
11
a = −1.5;
t = 1:0.1:100;
x = t .ˆ a;
figure ; subplot (211) ;
plot ( t , x , ’ linewidth ’ ,2) ;
s e t ( gca , ’ f o n t s i z e ’ , 1 6 ) ;
xlabel ( ’ t ’ , ’ f o n t s i z e ’ ,18) ;
ylabel ( ’x ( t ) ’ , ’ f o n t s i z e ’ ,18) ;
t e x t (90 ,0.9 , ’ ( a ) ’ , ’ f o n t s i z e ’ ,16) ;
12
13
14
15
16
17
18
subplot (212) ;
p l ot ( log ( t ) , log ( x ) , ’ linewidth ’ ,2) ; axis t i g h t ;
s e t ( gca , ’ f o n t s i z e ’ , 1 6 ) ;
xlabel ( ’ ln ( t ) ’ , ’ f o n t s i z e ’ ,18) ;
ylabel ( ’ ln ( x ) ’ , ’ f o n t s i z e ’ ,18) ;
t e x t (4.2 , −.6 , ’ ( b ) ’ , ’ f o n t s i z e ’ ,16) ;
7
1
(a)
x(t)
0.8
0.6
0.4
0.2
0
0
20
40
60
80
100
t
0
(b)
ln(x)
−2
−4
−6
0
1
2
3
4
ln(t)
Figure 4:
Example 5 Write a code to generate a plot that resembles a tunnel or a tornado.
This exercise is open-ended, and there should exist many solutions. Our solution involves using sinusoidal
signals, exponential signals, and the time-delay embedding technique. The code is given below and the plot is
given in Fig. 5. It would be beneficial for you to check how the plot changes when you modify part of the code.
1
clc ; clear all ; close all ;
2
3
4
5
6
7
8
9
t = 0:0.1:30∗ pi ∗2;
x = 15∗ s i n ( t ) . ∗ exp ( 0 . 0 3 ∗ t ) ;
L=16;
len=length ( t ) ;
figure ;
p l o t 3 ( x ( 1 : l e n −L ) +50∗ t ( 1 : l e n −L ) , x ( 1 +L : l e n ) , 1 0 ∗ t ( 1 : l e n −L ) , ’ l i n e w i d t h ’ , 2 ) ;
axis off ;
8
Figure 5:
9