January 2022
January 2022
[ ]: import numpy as np
import matplotlib.pyplot as plt
import sympy as sp
Problem 1 (5 points)
Compared to other animals, the snake is more flexible in its motions because of the impressive
number of vertebrae that it has: while humans have 33 vertebrae, snakes have around 400! That’s
why the snake can easily move its body in a wavy way. In order to understand the snake motions
well, different mathematical functions have been proposed and used. Among them, there is the
function f , where the graph is depicted in the figure below, over the interval [−1, 1]:
1
1. Observe the graph of the function f and determine the number of roots of the equation
f ( x ) = 0.5 over [−1, 1]. (1 point)
The equation f ( x ) = 0.5 admits 8 roots on the interval [−1, 1].
2. Give the sign of f ′ , the first derivative of f , over [−0.25, 0[. Justify your answer. (1.5 points)
f ′ ( x ) < 0 on the interval [−0.25, 0[ because f is strictly decreasing.
3. Give the sign of f ”, the second derivative of f , over [−0.25, 0[. Justify your answer. (1 point)
Graphically, f ” is strictly positive on the interval [−0.25, 0[ because f is convex.
4. In order to solve numerically the equation f ( x ) = 0.5 over [−0.25, 0[, the Newton’s recurrent
sequence ( xn )n≥0 is used. Among −0.25 and 0, which choice of x0 ensures the convergence
of ( xn )n≥0 ? Justify your answer. (1.5 points)
Let g( x ) = f ( x ) − 0.5.
We choose x0 = −0.25 because g(−0.25) ∗ g”(−0.25) = ( f (−0.25) − 0.5) ∗ f ”(−0.25) > 0.
(The choice of x0 = 0 is wrong because g′ (0) = f ′ (0) = 0).
Problem 2 (7 points)
Let A ∈ Mn (R) be an invertible matrix, b ∈ Rn and w ∈]0, 2[. The goal of this problem is to solve
numerically the linear system AX = b, X ∈ Rn , using the following iterative method:
2
X (0) ∈ Rn : a given initial vector
( MI )n
( 1 D − E) X (k+1) = ( 1 − w D + F ) X (k) + b,
k ≥ 0,
w w
with A = D − E − F, where D, E and F are illustrated as follows:
a1,1 0 ··· 0 0 ··· ··· 0 0 − a1,2 · · · − a1,n
.. .. .. .. .. .. ..
0 . . 0 − − a2,1
. 0 − .
. . .
A= .. . ..
.. .. .. .. .
0 .. ..
. . . . . 0 . − an−1,n
0 ··· 0 an,n − an,1 · · · − an,n−1 0 0 ··· ··· 0
| {z } | {z } | {z }
D E F
1
We assume that the matrix D − E is invertible.
w
1. Fill in the following function iterative(A, b, X0, w, Tol) where inputs are: the square
matrix A of order n, the constant term b, the initial vector X0, the parameter w and a precision
Tol. The function returns the approximated solution of AX = b using ( MI )n method, and
the number of performed iterations using the stop condition: || AX (k) − b|| ≤ Tol. (2.5 points)
NB: All comments must be taken into account when filling in the following code cell.
[ ]:
def iterative(A, b, X0,w, Tol):
D=np.diagflat(np.diag(A))
E=-np.tril(A,-1)
F=-np.triu(A,1)
M = (1/w)*D-E # 0.25pt*10
N =((1-w)/w)*D+F # or N=M-A
invM=np.linalg.inv(M)
B=invM.dot(N)
C=invM.dot(b)
k=0
while np.linalg.norm(A.dot(X0)-b,1)>Tol:
X0=B.dot(X0)+C
k+=1
return X0,k
a) Using python syntax, declare the matrix A and the vector b. Then, give only the ob-
tained solution X of (S) using the command np.linalg.solve(). (1 point)
3
[ ]: A=np.array([[66,1,-9,-2],[1,35,-5,-9],[-9,-5,50,27],[-2,-9,27,85]])
b=np.array([[97],[95],[-196],[-186]])
Solution=np.linalg.solve(A,b)
1
when Tol = 10−6 and w=1. (1 point)
[ ]:
X0=np.ones((4,1))
w=1
Tol=10**(-6)
iterative(A, b, X0,w, Tol)[1]
3. This part aims to study the number of performed iterations when reaching the stop condition
of the iterative method, as a function of the parameter w.
a) Fill in the following code cell to plot the number of performed iterations N_it as a
1 9
function of W, where W is composed of 17 equidistant values in [ , ].(1.5 points)
5 5
NB: Consider the same initial vector and precision as for the question 2. b).
plt.figure(figsize=(20,10))
plt.plot(W,N_it,'ro--', linewidth=3,markersize=12) #0.5pt
plt.yscale('log')
4
b) Determine graphically the value of W for which the minimum number of iterations. We
denote this value by wb. (0.5 point)
wb=1.1
c) Knowing that for w=1, the iterative method ( MI )n corresponds to the Gauss-Seidel one, com-
pare the obtained results for w=1 and w=wb in term of number of iterations. (0.5 point)
According to the graph, the number of iterations for w = 1 is greater than the number of
iterations for wb=1.1
Problem 3 (8 points)
We recall hereafter the general form of a Cauchy Problem (CP) defined over I = [t0 , t0 + T ] ⊂ R:
x ( t ) = e −2 e G ( t ) , t ∈ [0, 2],
where G denotes the primitive function of g, with a zero constant.
1. Using the "sympy" module, give the expression of the primitive function G, as well as the
expression of the final analytical solution x , ∀t ∈ [0, 2]. (1,5 points)
2 −2t +2) et
x ( t ) = e −2 e ( t , ∀t ∈ [0, 2]
5
2. Using now the "numpy" module, give the needed instruction to implement the obtained
solution x. (1 point)
[ ]:
x=lambda t: np.exp(-2)*np.exp((t**2-2*t+2)*np.exp(t))
1. Implement the function scheme( f ,x0 ,t0 ,T,N) where the inputs are: the function f of
the Cauchy problem (CP), the initial value x0 , the left endpoint t0 of the interval
I = [t0 , t0 + T ] on which x is defined, the length T > 0 of I, and the number of sub-intervals
N of I. The output of this function is the sequence of solutions [ x0 , x1 , ..., x N ], generated
using the iterative scheme (S). (1.5 points)
6
[ ]: def schema(f, x0, t0,T,N):
x = [x0]
h=T/N
for k in np.arange(0, N):
p1 = f(t0+k*h,x[k])
p2 = f( t0+k*h + h / 2, x[k] + h * p1 / 2)
x.append(x[k] + h * p2)
return x
2. For N = 20, and over [0, 2], plot on the same figure, the curve of the exact solution of ( E)
and the curve of the approximated one using the iterative scheme (S). To do so, proceed in
4 steps as follows:
a) Declare all variables of the function scheme to solve the Cauchy problem ( E). (1 point)
[ ]:
N=20
T=2
f=lambda t, x: t**2*np.exp(t)*x
t0=0
x0=1
b) Give the needed instruction to assign the approximate solutions [ x0 , x1 , ..., x N ] of ( E),
obtained by (S), to the variable xapp. (0.5 point)
[ ]:
x_app=schema(f, x0, t0,T,N)
c) Give the needed instruction to declare the vector t, that contains the discretization
points tn , n ∈ {0, · · · , N − 1} over [0, 2]. (0.5 point)
[ ]:
t=np.linspace(t0, t0+T,N+1)
d) Fill in the following code cell in order to plot both curves of exact and approximated
solutions, in a logarithmic scale. (1 point)
[ ]: plt.figure(figsize=(20, 10))
plt.plot(t, x(t), 'b*-',t,x_app, 'ro-',linewidth=2,markersize=8)
plt.legend(('Exact solution', 'Approximate solution'),fontsize=30)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
plt.yscale('log')
7
plt.show()
3. Observe the plotted curves of x and xapp and interpret graphically the quality of approxi-
mation over the following two sub-intervals: [0, 1] and [1, 2]. (1 point)
On the interval [0, 1], the approximate solution coincides with the exact solution, on the other
hand on the interval [1, 2], the approximate solution does not.
Good luck!