Uploaded by samimahjoub080

examen SR correction 2021

advertisement
**NB: You are required to:
1. use an arrow −→ to specify an indentation,
2. import the modules “numpy“,“matplotlib.pyplot“ and “sympy“ using the aliases shown
below:
[ ]: import numpy as np
import matplotlib.pyplot as plt
import sympy as sp
Exercise 1 : (10 points)
In this exercise, we are interested in approaching the value of the integral I =
2x
two different quadrature formulas. We define f as f ( x ) =
,
∀
x
∈
1, 2 .
1 + x2
Z 2
1
f ( x ) dx using
1. Find the exact value of the integral I using a built-in function in the “sympy“ library.(1 point)
[ ]: #Code:
x=sp.symbols('x')
I=sp.integrate((2*x/(1+x**2)),(x,1,2)).evalf()
I
The exact value of I equals 0.916290731874155
1
2. We want to approach the value of I using the following quadrature formula:
3
Q( f ) = C0 f (1) + C1 f ( ) + C2 f (2),
2
where C0 , C1 and C2 are real numbers.
Knowing that C0 , C1 and C2 are identified in the following system of linear equations (S) :


C0 + C1 + C2
(S) C0 + 32 C1 + 2C2


C0 + 49 C1 + 4C2
=1
= 32
= 73
(a) Prove that the system (S) has a unique solution in R3 using a built-in function.(1 point)
[ ]: #Code:
A=np.array([[1,1,1],[1,3/2,2],[1,9/4,4]])
np.linalg.det(A)
The system (S) has a unique solution in R3 since det( A) = 0.25000000000000006 ̸= 0.
(b) Find the values of C0 , C1 and C2 using the function solve of the sub-module linalg in
numpy.(1 point)
2
[ ]: #Code
b=np.array([[1],[3/2],[7/3]])
np.linalg.solve(A,b)
• The value of C0 (4 digits after the decimal) is 0.1666
• The value of C1 (4 digits after the decimal) is 0.6666
• The value of C2 (4 digits after the decimal) is 0.1666
(c) Using the values of C0 , C1 et C2 and the quadrature formula Q, compute the integration
error of I.(1,5 point)
[ ]: #Code:
Q=0.1666*f(1)+0.6666*f(3/2)+0.1666*f(2)
erreur=abs(I-Q)
erreur
The integration error found using the quadratic formula Q (4 digits after the decimal)
equals 0.0010
3. We are now interested in approaching the value of I using the left rectangle approximation.
Recall that the left rectangle approximation is used to estimate I ( f ) =
is a continuous function on [ a, b], and whose formula is given by:
c
IRg
(f) = h
Z b
a
f ( x ) dx, where f
n −1
∑
f ( x k ),
k =0
we have xk = a + kh (0 ≤ k ≤ n) are the integration points from the subdivision of [ a, b] in n
b−a
sub-intervals with a fixed step h =
.
n
(a) Write a function leftcomposit(f,a,b,n) taking as input, the continuous function f , the
bounds of the interval a and b and the number of sub-intervals n and it returns the
c ( f )(1,5 points)
approached value of I by IRg
3
[ ]: def leftcomposit(f,a,b,n):
h=(b-a)/n
I=0
for k in np.arange(n):
I+=f(a+k*h)
return h*I
(b) Considering 5 sub-intervals, compute the integration error of I using the left rectangles
approximation.(1 point)
[ ]: #Code:
RG=leftcomposit(f,1,2,5)
abs(I-RG)
The integration error using the left rectangle approximation (4 digits after the decimal)
with n = 5 equals 0.0192
(c) Compare the results of the left rectangle method approximation and the Q quadrature
formula for approximating I in terms of accuracy.(1 point)
........................................................................................................................................................
........................................................................................................................................................
c is defined by
4. Recall that, the error estimation of the integration of I, denoted ERg
c
ERg
≤ M,
with M =
1
2 − 2x2
.
max | f ′ ( x )| and f ′ ( x ) =
2n x∈[1,2]
(1 + x 2 )2
(a) Let g be a function defined, on [1, 2], by g( x ) = | f ′ ( x )|, write the instructions to draw
the curve of g on [1, 2].(1 point)
[ ]:
x=np.linspace(1,2,100) # discretize [1,2] in 100 nodes.
g=lambda x: abs((2-2*x**2)/(x**2+1)**2)
plt.plot(x,g(x))
plt.grid(True)
plt.xlabel('x-axis ')
plt.ylabel('y-axis')
plt.title('Curve of g')
4
(b) Deduce, graphically, the approached value of max g( x ), then give the value of M for
x ∈[1,2]
n = 5.(1 point)
max g( x ) = 0.25
x ∈[1,2]
and
M = 0.025
Exercise 2 : (10 points)
Let f be a continuous function on a, b such that f ( a) ̸= f (b).
We wish to numerically approach the root of the equation ( E) : f ( x ) = 0 using the following
iterative schema:

b−a
x
f ( x n ),
n +1 = x n −
f (b) − f ( a)
( MI )n :

x0 ∈ [ a, b],
where ( xn ) is a sequence that is converging to the exact solution x ∗ of ( E) ( lim xn = x ∗ ).
n→+∞
1. To achieve this goal, complete the function iterativemethod(a,b,f,x0,epsilon) which
takes the following parameters: a and b the two extremities of [ a, b], the function f,
the initial value x0 and the tolerance epsilon. This function must return two lists of
which the first contains all the iterates x = [ x0 , x1 , x2 , · · · ] and the second contains
y = [ f ( x0 ), f ( x1 ), f ( x2 ), · · · ].(3 points)
In this function, we use the stop test f( xn ) < epsilon, ∀ n ≥ 0.
[ ]:
def iterativemethod(a,b,x0,f,epsilon):
x=[x0]
y=[f(x0)]
c=((b-a)/(f(b)-f(a)))
while (abs(y[-1])>epsilon):
x0=x0-c*f(x0)
x.append(x0.evalf())
y.append(f(x0).evalf())
return x,y
5
We consider the continuous function f , on [1, 2], by
f ( x ) = ln(1 + x2 ) − sin( x ).
NB: The logarithm function is written in python as: np.log() or sp.log().
2.
(a) Write the instructions justifying that ( E) has at least one solution in the interval
[1, 2].(1,5 point)
[ ]:
x=sp.symbols('x')
f=sp.Lambda(x,sp.log(x**2+1)-sp.sin(x))
(f(1)*f(2)).evalf()
Justification: .................................................................................................................................
.........................................................................................................................................................
.........................................................................................................................................................
(b) Give the instruction that compute the expression of the derivative of f , denoted d f .(1
point)
[ ]:
df=sp.Lambda(x,sp.diff(f(x),x))
df
The expression of the derivative d f is
d f (x) =
2x
− cos( x )
1 + x2
(c) Plot d f , the derivative function of f on [1, 2].(0,75 point)
6
[ ]: #Give the appropriate instructions
t=np.linspace(1,2,100)
df=lambda t: (2*t/(t**2+1))-np.cos(t)
plt.plot(t,df(t))
plt.grid(True)
plt.xlabel('x-axis ')
plt.ylabel('y-axis')
plt.title('Curve of df')
(d) Deduce that ( E) has a unique solution on 1, 2 .(1 point)
........................................................................................................................................................
........................................................................................................................................................
We assume that x0 = 2 and epsilon = 10−2 .
3.
(a) Give the instruction that displays the estimated solution of x ⋆ to the nearest epsilon
using the "iterativemethod" function.(1 point)
[ ]: # code
a=1
b=2
x0=2
epsilon=10**(-2)
iterativemethod(1,2,x0,f,epsilon)[0][-1]
x ⋆ = 1.257202187322231.25720218732223
(b) Fill in the table below that contains the first three iterates (only two digits after the
decimal ) of the ( MI )n scheme.(1,75 points)
X
Y
x0 = 2
f ( x0 ) = 0.70
x1 = 1.17
f ( x1 ) = −0.05
7
x2 = 1.24
f ( x2 ) = −0.01
x3 = 1.25
f ( x3 ) = −0.00
Download