Sie sind auf Seite 1von 62

The basics

Functions and modules


Numerical Python (NUMPY) : arrays
Visualising data (Matplotlib)
Numerical Methods using Python (SCIPY)
Concrete examples and exercises

Introduction to Python and some applications.

Marion Darbas
LAMFA UMR CNRS 7352, Universit de Picardie Jules Verne

CIMPA SCHOOL
NUOL University, Vientiane, Laos
January 03-13 2017

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

Aims : Introduce the Python programming language


Basics
Useful Python librairies
Using Python for solving ordinary differential equations
(ODEs) and partial differential equations (PDEs)

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

Python

Open source general-purpose language


Object Oriented Procedural, Functional
Easy to interface with C/ObjC/Java/Fortran
Easy to interface with C++ (via SWIG)
Great interactive environment

Downloads : http ://www.python.org


Documentation : http ://www.python.org/doc/
Free book : http ://www.diveintopython.org

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

Packages for scientific computing

NUMPY module contains among other things :


a powerful N-dimensional array object
linear algebra tools
sophisticated (broadcasting) functions
tools for integrating C/C++ and Fortran code

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

Packages for scientific computing

SCIPY provides a multitude of numerical algorithms


Numerical integration
Interpolation
Solving ordinary differential equations, . . .

Matplotlib is a high quality plotting library.

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

Outline of the course

1 The basics

2 Functions and modules

3 Numerical Python (NUMPY) : arrays

4 Visualising data (Matplotlib)

5 Numerical Methods using Python (SCIPY)

6 Concrete examples and exercises

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

The basics

Python is an interpreted language.

Enter individual commands at the Python prompt (>>>) which are


immediately evaluated and carried out by the Python interpreter.
Collect sequences of commands into a text file and save this file as
a Python program. Extension ".py"

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

The basics

Python is a calculator : basic operations such as addition (+),


subtraction (-), multiplication (*), division (/) and exponentiation (**).

>>> 10 + 10000
10010
>>> 42 1.5
40.5
>>> 15./6.
2.5
>>> 23
8

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

The basics

Mathematical functions : mathematics module with name math.


(sin, cos, tan, exp, log, . . .)

>>> import math


>>> math.exp(1.0)
2.7182818284590451
help(math), help(math.exp) provide more information.

>>> math.pi
3.141592653589793
>>> math.e
2.7182818284590451

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

The basics

Variables : A variable is created through assignment.

>>> 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 : *=, -=, /=.

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

The basics

The print() command


The print command is the most commonly used command to print
information.
>>> a, b = 10, 20
>>> print(a)
10
>>> print(a, b)
10 20
>>> print(The answer is, a)
The answer is 10

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

The basics

Different data types

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

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

The basics
Complex numbers

>>> x = 1 + 3j
>>> abs(x) # modulus
3.1622776601683795
>>> x.imag # imaginary part
3.0
>>> x.real # real part
1.0

>>> import cmath


>>> cmath.sqrt(x)
(1.442615274452683 + 1.0397782600555705j)

cmath means Complex MATHematics.


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

The basics
Sequence type : list
A list is a sequence of objects. The objects can be of any type. For
example, integers or strings :

>>> a = [34, 12, 54]


>>> a = [dog, cat, mouse]

It is also possible to mix different types in the same list :

>>> a = [1022, elephant, 0, 4, duck]

An empty list is presented by

>>> a = [ ] #empty list

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

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 !

>>> a = [12, 10, 18, 7, 15, 3] # Create a list


>>> print(a)
[12, 10, 18, 7, 15, 3]
>>> a[0]
12
>>> a[2]
18
>>> a[1] = 11
>>> print(a)
[12, 11, 18, 7, 15, 3]

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

The basics

Some operations

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

The basics
Some operations
You can add one object to the end of a list using the append() method

>>> a = [34, 56, 23]


>>> a.append(42)
>>> print(a)
[34, 56, 23, 42]

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]

c=a[:] will return a copy of a.


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

The basics
Slicing
A subsequence of a sequence is called a slice.

fraise[i:j] returns elements i up to j 1.


>>> fraise[2 : 4] #index of end not included
[18, 7]
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

The basics

Indexing versus slicing

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.

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

The basics

The range() command


A special type of list is frequently required (often together with for-loops).
A command exists to generate that list : the range(n) command
generates a list of integers starting from 0 and going up to n-1.

>>> 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]

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

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.

Python requires the colon symbol : after each condition.


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

The basics
There is the elif keyword that allows checking for several (exclusive)
possibilities.

Comparison operators

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

The basics
For loop
The for-loop allows to iterate over a sequence.

This could be a string or a list, for example.

The output is [1.0, 0.5, 0.33333333333333331, 0.25].


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

The basics
While loop
The while keyword allows to repeat an operation while a condition is true.

To create the list [1, 1/2, 1/3, 1/4], we write

Remark 1 : It is important not to forget the instruction n+ = 1.


Remark 2 : The command break allows to quit a loop.
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

Functions and modules


Functions allow us to group a number of statements into a logical block.
We communicate with a function through a clearly defined interface,
providing certain parameters to the function and receiving some
information back.

Ex. : we define a function that computes the square of a given variable.

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

Functions and modules

The listing of a file that shows how the function square.py can be used :

This program will produce the following output

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

Functions and modules

In the following example, the function derivatives computes an


approximation of the first and second derivatives of a function f at point
x using the formula

f (x + h) f (x h) f (x + h) 2f (x) + f (x h)
f 0 (x) ' , f (x) '
2h h2

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

To approximate the values of the first and second derivatives of the


function x 7 cos(x) at x = /2, we write

df, ddf = derivatives(math.cos, math.pi/2, 1.0e 5)


print(First derivative, df)
print(Second derivative, ddf)

Another example : the input is a list

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

Functions and modules


A module is a collection of functions. Pythons standard library contains
a vast collection of modules.
Importing modules
We introduce the name math into the namespace in which the import
command was issued. More than one module can be imported in the
same statement.

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.

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

Numerical Python (NUMPY) : arrays

The NumPy package (read as NUMerical PYthon) provides access to a


new data structure called arrays which allows efficient vector and matrix
operations. It also provides a number of linear algebra operations (such
as solving of systems of linear equations, computation of eigenvectors
and eigenvalues).
An array appears to be very similar to a list but an array can keep only
elements of the same type (whereas a list can mix different kinds of
objects).

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

Numerical Python (NUMPY) : arrays


Vectors (1d-arrays)
Conversion of a list into an array using numpy.array :

Creation of vector with zeros

Creation of a vector using ArrayRange :

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

Numerical Python (NUMPY) : arrays


Once the array is established, we can set and retrieve individual values.

Calculations are performed on every element in the vector with a single


statement :

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

Numerical Python (NUMPY) : arrays


Matrices (2d-arrays)
There are two ways to create a 2d-array :
By converting a list of lists into an array :

Using the "zeros method" (ex. : matrix with 5 rows and 4 columns)

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

Numerical Python (NUMPY) : arrays


Individual elements can be accessed and set using this syntax

Create the unity matrix A M3 (C)

>>> A = N.eye(3) # numpy .eye

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

Numerical Python (NUMPY) : arrays


Standard linear algebra operations

Matrix-vector product

Resolution of the linear system Ax = b

Computation of the eigenvalues and eigenvectors of a matrix A


>>> evalues, evectors = LA.eig(A)

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

Numerical Python (NUMPY) : arrays


Standard linear algebra operations
Numpy provides the routine polyfit(x,y,n) which takes two lists x
and y of same length and a desired order n of the polynomial that
will be determined to fit the data in the least-square sense.

Numpy creates a polynomial function p from coefficients :


p = numpy.poly1d(z)
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

Visualising data (Matplotlib)

The Python library Matplotlib is a python 2D plotting library which


produces publication quality figures.
To plot the graph of a function f : [a, b] 7 R, Python needs a finite
number of points (xi , f (xi )). To generate the points (xi ), two commands
exist (NUMPY module)
ba
linspace(a,b,n) generates a list of n points xi = a + i ,
n1
i = 0, . . . , n 1 (x0 = a, xn1 = b).
b a
arange(a,b,h) generates a list of n = E + 1 elements
h
xi = a + ih, i = 0, . . . , n 1.

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

Visualising data (Matplotlib)

An example

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

Visualising data (Matplotlib)


Some commands

figure(figsize=(5,5)) sets the figure size to 5 by 5.


plot(x,y1,label=sin(x)) The label keyword denotes the
name of this line.
plot(x,y,b-,linewidth=2) will plot a blue solid line with two
pixel thickness 2.
axis([-2,2,-1,1]). This fixes the displayed area to go from
xmin= 2 to xmax= 2 in x-direction, and from ymin= 1 to
ymax=1 in y-direction.
grid() This command will display a grid on the backdrop.
xlabel(...), ylabel(...) allow labelling the axes.

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

Visualising data (Matplotlib)


Some options of Pylab

The full list of options can be found when typing help(pylab.plot)

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

Visualising data (Matplotlib)


2D
Consider the function f (x, y ) = (1 x)2 + (y x 2 )2 .

Mesh the rectangle [1, 1] [1, 2]

Display the contour lines of the function f

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

Numerical Methods using Python (SCIPY)


SCIPY package (SCIentific PYthon) provides a multitude of numerical
algorithms. SCIPY is built on NUMPY.
Solving ordinary differential equations
To solve an ODE of the type

y 0 (t) = f (t, y (t))

with a given y (t0 ) = y0 , we use SCIPYs odeint function.


Example : find y (t) for t [0, 2] solution to

y 0 (t) = 2ty (t)

with y (0) = 1.
2
Exact solution : y (t) = e t .

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

Numerical Methods using Python (SCIPY)

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

Numerical Methods using Python (SCIPY)

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

Numerical Methods using Python (SCIPY)

Root finding : Find x such that f (x) = 0.


Dichotomy method or Bisect algorithm
Assume f (a)f (b) 0.
Initialization : n = 0, x0 = a, x1 = b, x2 = (x0 + x1 )/2
While (|f (x2 )| ) and (n itermax)
If f (x0 )f (x2 ) 0 set x1 = x2 , else set x0 = x2
x2 = (x0 + x1 )/2
n=n+1

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

Numerical Methods using Python (SCIPY)


Suppose we need to compute the roots of f (x) = x 2 3. Here is a
program that determines this root numerically

This produces the following output :

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

Numerical Methods using Python (SCIPY)


A more efficient algorithm than the bisection one is implemented in the
general purpose fsolve() function for root finding of (multidimensional)
functions. This algorithm needs only one starting point close to the
suspected location of the root. Ex : f (x) = x 3 2x 2 .

This produces the following output :

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

Concrete examples and exercises

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

y 0 (t) = exp(t)(10 sin(10t) + cos(10t))

from t = 0 to t = 10. The initial value is y (0) = 1.


3 Display the solution graphically at t = 0, t = 0.01, . . . , t = 10.

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

Concrete examples and exercises

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

Concrete examples and exercises

Solving ordinary differential equations

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 ,

with t 0 , T > 0, f : [t 0 , T ] Rd Rd a regular function and u 0 Rd .

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

Concrete examples and exercises

Explicit numerical schemes


Discretization of the interval I = [t 0 , T ]

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).

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

Concrete examples and exercises

Forward Euler explicit scheme (order 1)

u0 = u(t 0 ) = u 0 Rd


ui+1 = ui + hf (ti , ui ), i = 0, 1, . . . , N 1.

Modified Euler explicit scheme (order 2)


0 0 d
u0 = u(t ) = u R

K1 = f (ti , ui )

h h
K2 = f (ti + , ui + K1 )


2 2
ui+1 = ui + hK2 , i = 0, 1, . . . , N 1.

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

Concrete examples and exercises

Runge-Kutta method of order 4

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

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

Concrete examples and exercises

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

y 0 (t) = 2ty (t)

from t = 0 to t = 1. The initial value is y (0) = 1.


3 Display the exact solution and its approximation graphically.
4 Compute numerically the convergence order of each method and
compare with the respective theoretical orders.

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

Concrete examples and exercises


Lotka-Volterra equations : predator-prey model
A pair of first-order, non-linear differential equations describes the
dynamics of biological systems in which two species interact, one as
predator and the other as prey :
0
x (t) = x(t)(a by (t))
y 0 (t) = y (t)(c + dx(t)) (2)
x(0) = x0 , y (0) = y0

with a, b, c, d , x0 , y0 non-negative constants.

x(t) is the number of preys at time t


y (t) is the number of predators at time t

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

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 ?

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

Concrete examples and exercises


1D finite difference method - Stationary problem
Consider the problem

u 00 (x) = cos x, /2 < x < 3/2,



(3)
u(/2) = 0, u(3/2) = 0.

Discretization of [/2, 3/2] : Constant mesh step h = /N, with


N N . We introduce the nodes (xi )i=0, ,N , xi = /2 + ih
(x0 = /2, xN = 3/2)
Finite difference scheme : We denote by ui the approximation of the
value u(xi ) where u is the exact solution of (5). The discrete
problem reads : Find uh = (ui )i=0, ,N RN+1 such that
ui1 2ui + ui+1
(
= cos ui , 1 i N 1,
h2 (4)
u0 = uN = 0.

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

Concrete examples and exercises


Exercise 4
1 Let uh = (u1 , , uN1 ) RN1 . Write the discrete problem under
matricial form Ah uh = bh with Ah R(N1)(N1) , bh RN1 .
2 Write a Python function which computes the solution of this linear
system.
3 Display on the same figure the exact solution u(x) = cos x and its
approximation at a fixed N.
4 Compute for different values of N (for instance N = 5, . . . , 100) the
error ku uh k . Report it in function of the parameter N in log-log
scale.
5 Propose a method for estimating numerically the order of
convergence of the finite difference scheme. Recall that the
theoretical order equals 2.
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

Concrete examples and exercises


1D finite difference method - Non-stationary problem
Consider the 1D heat equation subject to homogeneous Dirichlet
boundary conditions
t u x2 u = 0, 0 < t < T , 0 < x < 1,

u(0, x) = u0 (x), 0 < x < 1, (5)


u(t, 0) = u(t, 1) = 0, 0 < t < T ,

with u0 a given initial condition.


Time discretization : Discrete times tn = nt , n = 0, . . . , N where
t = T /N, N N .
Space discretization : Nodes xj = jx , j = 0, . . . , J + 1, where
x = 1/(J + 1), J N.
Approximation : We denote by ujn the approximation of the exact
value u(tn , xj ), and by u n = (ujn )1jJ the approximation at time t n .

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

Concrete examples and exercises


Explicit scheme

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

Concrete examples and exercises

The -scheme, [0, 1]

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.

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

Concrete examples and exercises


Exercise 5
Consider the initial condition
1 1
u0 (x) = 2x, 0 x , and u0 (x) = 2 2x, x 1.
2 2
The exact solution is

X 2 8 n
u(t, x) = cn e (n) t sin(nx), cn = sin( ).
n=1
n2 2 2

1 Implement the explicit scheme.


2 Discuss the convergence of the numerical method in function of the
parameter .
3 Same questions with the Crank-Nicholson scheme ( = 1/2).

Marion Darbas (LAMFA) CIMPA SCHOOL - Vientiane - January 03-13 2017

Das könnte Ihnen auch gefallen