Beruflich Dokumente
Kultur Dokumente
Marion Darbas
LAMFA UMR CNRS 7352, Universit de Picardie Jules Verne
CIMPA SCHOOL
NUOL University, Vientiane, Laos
January 03-13 2017
Python
1 The basics
The basics
The basics
>>> 10 + 10000
10010
>>> 42 1.5
40.5
>>> 15./6.
2.5
>>> 23
8
The basics
>>> math.pi
3.141592653589793
>>> math.e
2.7182818284590451
The basics
>>> x = 0.5
>>> x2
0.25
>>> x = y = z = 0 # initialise x, y and z with 0
Notation +=
>>> x = 4
>>> x+ = 1
>>> print(x)
5
Other operators : *=, -=, /=.
The basics
The basics
>>> a = 45
>>> type(a)
int
>>> b =0 This is a string0
>>> type(b)
str
>>> c = 2 + 3j
>>> type(c)
complex
>>> d = [4, 5, 6]
>>> type(d)
list
The basics
Complex numbers
>>> x = 1 + 3j
>>> abs(x) # modulus
3.1622776601683795
>>> x.imag # imaginary part
3.0
>>> x.real # real part
1.0
The basics
Sequence type : list
A list is a sequence of objects. The objects can be of any type. For
example, integers or strings :
The basics
Indexing sequences
Individual objects in lists can be accessed by using the index of the object
and square brackets. Note that Python starts counting indices from zero !
The basics
Some operations
The basics
Some operations
You can add one object to the end of a list using the append() method
You can delete an object from a list by calling the remove() method
>>> a = [34, 56, 23, 42]
>>> a.remove(56)
>>> print(a)
[34, 23, 42]
The basics
Slicing
A subsequence of a sequence is called a slice.
The basics
If you are not sure what the right index is, it is always a good technique
to play around with a example of small size to test before or while you
write your program.
The basics
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(3, 10) # start = 3
[3 , 4 , 5 , 6 , 7 , 8 , 9 ]
>>> range(3, 10, 2) # start = 3 , step = 2
[3, 5, 7, 9]
>>> range(10, 0, 1) # start = 10 , step = 1
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
The basics
If-then-else
The if statement allows conditional execution of code.
The if-statement can also have an else branch which is executed if the
condition is wrong.
The basics
There is the elif keyword that allows checking for several (exclusive)
possibilities.
Comparison operators
The basics
For loop
The for-loop allows to iterate over a sequence.
The basics
While loop
The while keyword allows to repeat an operation while a condition is true.
The listing of a file that shows how the function square.py can be used :
f (x + h) f (x h) f (x + h) 2f (x) + f (x h)
f 0 (x) ' , f (x) '
2h h2
We import the sin function from the math module, but the name math is
not introduced into the current namespace. It only introduces the name
sin.
Using the "zeros method" (ex. : matrix with 5 rows and 4 columns)
Matrix-vector product
An example
with y (0) = 1.
2
Exact solution : y (t) = e t .
Exercise 1
1 Open a new file with name testodeint.py file.
2 Write a program that computes the solution y (t) of this ODE using
the odeint algorithm
Cauchy problem
Consider the system of ODEs of first order :
find u : t [t 0 , T ] 7 u(t) Rd
0
u (t) = f (t, u(t)), t 0 t T ,
(1)
u(t 0 ) = u 0 ,
T t0
1 Fix a discretization step h = , with N N .
N
2 Construct the discrete times ti = t 0 + ih, 0 i N (t0 = t 0 ,
tN = T ). We have I = 0iN [ti , ti+1 ].
Approximation
Compute the approximated values (ui )0iN , where ui Rd approaches
the value u(ti ) with u the exact solution of (1).
u0 = u(t 0 ) = u 0 Rd
ui+1 = ui + hf (ti , ui ), i = 0, 1, . . . , N 1.
u0 = u(t 0 ) = u 0 Rd
K1 = f (ti , ui )
h h
K2 = f (ti + 2 , ui + 2 K1 )
h h
K3 = f (ti + , ui + K2 )
K = f (t n + 2h, u n + 2hK )
4 3
h
i+1 = ui + (K1 + 2K2 + 2K3 + K4 ), i = 0, 1, . . . , N 1.
u
6
Exercise 2
1 Open a new file with name ODE1D.py file.
2 Write a program that computes the solution y (t) of this ODE using
the previous explicit numerical schemes
There are two equilibrium points : (0, 0) (saddle point) and (d /c, a/b)
(center). Oscillatory solutions.
Marion Darbas (LAMFA) CIMPA SCHOOL - Vientiane - January 03-13 2017
The basics
Functions and modules
Numerical Python (NUMPY) : arrays
Visualising data (Matplotlib)
Numerical Methods using Python (SCIPY)
Concrete examples and exercises
Exercise 3
1 Write a function which implements the forward Euler explicit
method for the resolution of a system of two ODEs of order 1.
2 Apply it for solving the Lotka-Volterra system fixing T = 10, a = 3,
b = 1, c = 2 and d = 1 and the different initial conditions
(x0 , y0 ) = (5, 1), (x0 , y0 ) = (3, 3) and (x0 , y0 ) = (1, 2).
What is the conclusion ?
3 Same questions with the RK4 scheme. What is the conclusion ?
n+1
u ujn n
uj+1 2ujn + uj1
n
j , 1 n N, 1 j J,
t 2x (6)
u 0 = u0 (xj ), 1 j J,
jn
n
u0 = uJ+1 = 0, 0 n N,
The scheme writes equivalently under the form
t
ujn+1 = ujn + (uj+1
n
2ujn + uj1
n
) = 0, := .
2x
At each step n, u n is known and we compute u n+1 such that
u n+1 = Au n
with A MJ (R) a tridiagonal matrix.
Marion Darbas (LAMFA) CIMPA SCHOOL - Vientiane - January 03-13 2017
The basics
Functions and modules
Numerical Python (NUMPY) : arrays
Visualising data (Matplotlib)
Numerical Methods using Python (SCIPY)
Concrete examples and exercises
n+1 n+1
uj ujn uj+1 2ujn+1 + uj1
n+1 n
uj+1 2ujn + uj1
n
= 0,
(1 )
t 2x 2x
1 n N, 1 j J,
0
u = u (x ), 1 j J,
0 j
jn
n
u0 = uJ+1 = 0, 0 n N.