Professional Documents
Culture Documents
[ ]: import numpy as np
import matplotlib.pyplot as plt
import sympy as sp
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
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
(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)
2
[ ]: #Code
b=np.array([[1],[3/2],[7/3]])
np.linalg.solve(A,b)
[ ]: #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.
Z b
Recall that the left rectangle approximation is used to estimate I ( f ) = f ( x ) dx, where f
a
is a continuous function on [ a, b], and whose formula is given by:
n −1
c
IRg (f) = h ∑ f ( x k ),
k =0
(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,
1 2 − 2x2
with M = 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 and M = 0.025
x ∈[1,2]
b−a
n +1 = x n − f ( x n ),
x
( MI )n : f (b) − f ( a)
x0 ∈ [ a, b],
[ ]:
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 ).
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
2x
d f (x) = − cos( x )
1 + x2
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)
........................................................................................................................................................
........................................................................................................................................................
[ ]: # 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)