0% found this document useful (0 votes)
45 views8 pages

January 2022

The document discusses solving differential equations numerically using iterative methods like the Gauss-Seidel method. It analyzes the convergence properties of an iterative method (MI)n for solving linear systems as a function of the parameter w. The method is applied to solve a Cauchy problem with an analytical solution given by x(t) = e−2e(t^2−2t+2)et for comparison.

Uploaded by

Jasser Chtourou
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)
45 views8 pages

January 2022

The document discusses solving differential equations numerically using iterative methods like the Gauss-Seidel method. It analyzes the convergence properties of an iterative method (MI)n for solving linear systems as a function of the parameter w. The method is applied to solve a Cauchy problem with an analytical solution given by x(t) = e−2e(t^2−2t+2)et for comparison.

Uploaded by

Jasser Chtourou
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/ 8

**NB: All along this exam, you are asked to:

1. use an arrow pointing right −→ to indicate an indentation,


2. import “numpy“,“matplotlib.pyplot“, and “sympy“ modules using the following alias:

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

2. We consider the following linear system (S) : AX = b, where


   
66 1 −9 −2 97
 1 35 −5 −9  , b =  95 
 
A= −9 −5 50 27   −196
−2 −9 27 85 −186

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)

The solution of (S) is X = (1, 2, −3, −1)..


 
1
1
b) For X (0) = 

1, give only the number of performed iterations by the ( MI )n method,

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

[ ]: W=np.linspace(1/5,9/5,17) # ou W=np.arange(1/5,9/5+0.1,0.1) 0.5pt


N_it=[]
for w in W:
sol=iterative(A, b, X0,w, Tol)
N_it.append(sol[1]) #0.5pt

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) = f (t, x (t)), t ∈ [t0 , t0 + T ],



(CP)
x ( t0 ) = x0 ∈ R : initial condition,

where f is a continuous function in [t0 , t0 + T ] × R and T > 0.

Part 1: Exact solution


In what follows, we consider the Cauchy problem ( E), corresponding to a particular case of (CP)
for which f (t, x (t)) = g(t) x (t) :

x ′ (t) = g(t) x (t), t ∈ [0, 2],



( E)
x (0) = 1,
where g(t) = t2 et is a continuous function over [0, 2].
The analytical solution of ( E) is of the following form:

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)

G (t) = (t2 − 2t + 2)et , ∀t ∈ [0, 2],

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

Part 2: Approximated solution


The goal of the second part is to approximate numerically the exact solution of ( E). To do so, we
use the following iterative scheme (S) that approximates the solution of ( PC ) in general terms:

xn+1 = xn + hp2 , n ∈ {0, · · · , N − 1},




h h


p2 = f ( t n + , x n + p1 ),

(S) 2 2


 p1 = f ( t n , x n ),

x ( t0 ) = x0 ,
where
tn = t0 + nh, n ∈ {0, · · · , N − 1}, denote the discretization points of I = [t0 , t0 + T ] with a fixed
T
step h = (N corresponds to the number of sub-intervals of I), and xn is the approximate value
N
of x (tn ).

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)

Give the needed instructions.

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!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy