EXERCISES
Using the functions provided and studied during the semester, and using the Python programming language, solve
the following exercises:
Exercise 01 — Polynomial Interpolation
Consider the following points:
P0(1.0, 9.0), P1(1.5, 5.5), P2(2, 8), P3(5, 2.5)
1. Determine the minimum degree of the polynomial P(x) that passes through the four given points.
2. Using the NumPy function polyfit, determine the expression of the interpolation polynomial P(x).
3. Using the NumPy function polyval, compute the ordinate (y) corresponding to the abscissa (x = 3).
4. Using the function lagrange_interpolation, compute the ordinate (y) corresponding to the abscissa (x = 3).
5. Using the function newton_interpolation, compute the ordinate (y) corresponding to the abscissa (x = 3).
6. Plot the interpolation polynomial P(x) over the interval [1, 5].
o Create an abscissa vector x ranging from 1 to 5 containing 70 equally spaced elements.
o Compute the ordinate vector y1 using the NumPy function polyval.
o Compute the ordinate vector y2 using the function lagrange_interpolation.
o Compute the ordinate vector y3 using the function newton_interpolation.
o Plot the three curves (x, y1), (x, y2), and (x, y3) in the same figure arranged as a (1 ✗ 3)
subplot layout:
Plot (x, y1) in red with a solid line.
Plot (x, y2) in blue with a dashed line.
Plot (x, y3) in green with a dotted line.
o Add appropriate titles, axis labels, and grids to all subplots.
Exercise 02 — Numerical Differentiation and Integration
−𝒙𝒙
Consider the function: k(𝒙𝒙) = 𝒆𝒆 𝟐𝟐 𝒔𝒔𝒔𝒔𝒔𝒔(𝟐𝟐𝟐𝟐) + 𝒙𝒙𝟐𝟐 𝒍𝒍𝒍𝒍(𝒙𝒙 + 𝟏𝟏)
1. Define the function k(x) in Python and plot its curve over the interval [0, 5].
2. Compute the approximate value of the first derivative of k(x) at the point ( x = 2.5 ) using:
o The forward difference formula.
o The backward difference formula.
o The central difference formula (three points).
3. Plot the curve of k’(x) in the interval [1 , 4]
4. Plot the curve of k’’(x) in the interval [0 , 4]
5. Plot the curve of k(5)(x) (the fifth order derivative) in the interval [0.25 , 4.5]
6. Using numerical integration methods, compute the approximate value of the integral:
5 −𝒙𝒙
I = � 𝒆𝒆 𝟐𝟐 𝒔𝒔𝒔𝒔𝒔𝒔(𝟐𝟐𝟐𝟐) + 𝒙𝒙𝟐𝟐 𝒍𝒍𝒍𝒍(𝒙𝒙 + 𝟏𝟏) dx
0
by applying:
• The trapezoidal method.
• Simpson’s method.
List of functions (Studied in the Practical Work)
POLYNOMIAL INTERPOLATION
def lagrange_interpolation(X, Y, newX):
n = len(X)
L = 0
for i in range(n):
P = 1
NUMERICAL INTEGRATION
for j in range(n):
if i != j: def trapezoidal_rule(f, a, b, n=20):
P *= (newX - X[j]) / (X[i] - X[j]) h = (b-a)/n
L += Y[i] * P x = [Link](a, b, n+1)
y = f(x)
return L
s = (h/2) * (y[0] + 2*[Link](y[1:-1]) + y[-1])
def newton_interpolation(X, Y, newX): return s
n = len(Y)
A = [Link]() def simpson_rule(f, a, b, n=20):
if n % 2 != 0:
for i in range(1, n): n += 1
A[i:n] = (A[i:n]-A[i-1:n-1])/(X[i:n]-X[0:n-i])
h = (b - a)/n
PN = 0 x=[Link](a, b, n+1)
for i in range(n): y = f(x)
P = 1
for j in range(i): sum_odd = [Link](y[1:-1:2])
P *= (newX - X[j]) sum_even = [Link](y[2:-1:2])
PN += A[i] * P
s = (h/3)*(y[0] + 4*sum_odd + 2*sum_even + y[-1])
return PN return s
NUMEERICAL DIFFERENTIATION
def first_der_2pf (f, x, h = 0.01):
return (f(x+h)-f(x)) / h
def first_der_2pb (f, x, h = 0.01):
return (f(x)-f(x-h)) / h
def first_der_3p (f, x, h = 0.01):
return (f(x+h)-f(x-h)) / (2*h)
def second_der (f, x, h = 0.01):
return (f(x+h)-2*f(x)+f(x-h))/(h*h)
def n_der (f, n, x, h = 0.01):
if n == 1:
return first_der_3p (f, x, h)
else:
return (n_der(f, n-1, x+h/2, h) - n_der(f, n-1, x-h/2, h))/h
SOLUTION
Exercise 01 — Polynomial Interpolation
1. the minimum degree of the polynomial P(x) that passes through the four given points is: 3
2. determining the expression of the interpolation polynomial P(x) using the NumPy function polyfit:
import numpy as np
import [Link] as plt
px = [Link]([1.0, 1.5, 2.0, 5.0])
py = [Link]([9.0, 5.5, 8.0, 2.5])
P = [Link](px, py, 3)
print(P)
# [ -3.48809524 27.69642857 -59.67261905 44.46428571]
𝑃𝑃(𝑥𝑥) = −3.48𝑥𝑥 3 + 27.69𝑥𝑥 2 − 59.67𝑥𝑥 + 44.46
3. computing the ordinate (y) corresponding to the abscissa (x = 3) using the NumPy function polyval:
x = 3
y = [Link](P, 3)
print(f'f({x}) = {y}')
# f(3) = 20.535714285714533
4. computing the ordinate (y) using the function lagrange_interpolation:
y = lagrange_interpolation(px, py, 3)
print(f'lagrange intrp : f({x}) = {y}')
# lagrange intrp : f(3) = 20.535714285714285
5. computing the ordinate (y) using the function newton_interpolation:
y = newton_interpolation(px, py, 3)
print(f'newton intrp : f({x}) = {y}')
# newton intrp : f(3) = 20.535714285714285
6. Plotting the curves:
o Create an abscissa vector x ranging from 1 to 5 containing 70 equally spaced elements:
x = [Link](1, 5, 70)
o Compute the ordinate vector y1 using the NumPy function polyval:
y1 = [Link](P, x)
o Compute the ordinate vector y2 using the function lagrange_interpolation:
y2 = lagrange_interpolation(px, py, x)
o Compute the ordinate vector y3 using the function newton_interpolation:
y3 = newton_interpolation(px, py, x)
o Plot the three curves (x, y1), (x, y2), and (x, y3) in the same figure arranged as a (1 ✗ 3)
subplot layout:
Plot (x, y1) in red with a solid line.
Plot (x, y2) in blue with a dashed line.
Plot (x, y3) in green with a dotted line.
[Link](1,3,1)
[Link](x, y1, 'r')
[Link](1,3,2)
[Link](x, y2,'--b')
[Link](1,3,3)
[Link](x, y3,':g')
[Link]()
o Add appropriate titles, axis labels, and grids to all subplots.
[Link](1,3,1)
[Link](x, y1, 'r')
[Link](True)
[Link]('Numpy polyfit')
[Link]('X')
[Link]('Y1')
[Link](1,3,2)
[Link](x, y2,'--b')
[Link](True)
[Link]('Lagrange')
[Link]('X')
[Link]('Y2')
[Link](1,3,3)
[Link](x, y3,':g')
[Link](True)
[Link]('Newton')
[Link]('X')
[Link]('Y3')
[Link]()
Exercise 02 — Numerical Differentiation and Integration
1. Defining the function k(x) and plotting its curve over the interval [0, 5]:
k = lambda x:[Link](-x/2)*[Link](2*x)+x**2*[Link](x+1)
x = [Link](0, 5, 100)
y = k(x)
[Link](x, y)
[Link](True)
[Link]('The function K(x)')
[Link]()
2. Compute the approximate value of the first derivative of k(x) at the point ( x = 2.5 ) using:
o The forward difference formula:
forward_df = first_der_2pf(k, 2.5)
print(f"k'(2.5) = {forward_df} \t Using forward diff formula")
# k'(2.5) = 8.378016650395015 Using forward diff formula
o The backward difference formula:
backward_df = first_der_2pb(k, 2.5)
print(f"k'(2.5) = {backward_df} \t Using backward diff formula")
# k'(2.5) = 8.320814953140054 Using backward diff formula
o The central difference formula (three points):
central_df = first_der_3p(k, 2.5)
print(f"k'(2.5) = {central_df} \t Using central diff formula")
# k'(2.5) = 8.349415801767535 Using central diff formula
3. Plotting the curve of k’(x) in the interval [1 , 4]:
dx = [Link](1, 4, 50)
dy = first_der_3p(k, dx)
[Link](dx, dy)
[Link](True)
[Link]('The first order derivative of K')
[Link]()
4. Plotting the curve of k’’(x) in the interval [0 , 4]:
ddx = [Link](0, 4, 50)
ddy = second_der(k, dx)
[Link](ddx, ddy)
[Link](True)
[Link]('The second order derivative of K')
[Link]()
5. Plotting the curve of k(5)(x) (the fifth order derivative) in the interval [0.25 , 4.5]:
d5x = [Link](0.25, 4.5, 50)
d5y = n_der(k, 5, d5x)
[Link](d5x, d5y)
[Link](True)
[Link]('The fifth order derivative of K')
[Link]()
6. Using numerical integration methods, computing the approximate value of the integral:
5 −𝒙𝒙
I = � 𝒆𝒆 𝟐𝟐 𝒔𝒔𝒔𝒔𝒔𝒔(𝟐𝟐𝟐𝟐) + 𝒙𝒙𝟐𝟐 𝒍𝒍𝒍𝒍(𝒙𝒙 + 𝟏𝟏) dx
0
by applying:
• The trapezoidal method:
int_trapez = trapezoidal_rule(k, 0, 5)
print(f'The integral using the trapezoidal method = {int_trapez}')
# The integral using the trapezoidal method = 64.47726063426025
• Simpson’s method:
int_simpson = simpson_rule(k, 0, 5)
print(f"The integral using the Simpson's method = {int_trapez}")
# The integral using the Simpson's method = 64.37329357710706