Computational methods and applications HOMEWORK 3 solution




Please submit to CANVAS a .zip file that includes the following Matlab functions:

Lagrange interp.m

test Lagrange interpolation.m

plot Lebesgue function.m

test Lebesgue function.m

EXERCISE 1 Write a Matlab function Lagrange interp.m that computes the Lagrangian interpolant of a given set of data points (xi, yi), i = 1, 2, …. The Matlab function should be in the form

function [y] = Lagrange interp(xi,yi,x)


xi: vector of interpolation nodes

yi: vector of data points at interpolation nodes

x: vector of points in which we evaluate the polynomial interpolant


y: polynomial interpolant evaluated at x

Hint: You can compare the output of your function with the output of the Matlab function, y=polyval(polyfit(xi,yi,length(xi)-1),x) (see the Matlab documentation).

EXERCISE 2 Consider the nonlinear function

f (x) = 1 ,x ∈ [−1, 1]. (1)
1 + 20x2

By using the Matlab function you coded in Exercise 1, determine the Lagrangian interpolant of f , i.e. the polynomial ΠN f (x) that interpolates the set of data {xi, f (xi)}i=0,..,N in the following cases:

1. Evenly-spaced grid with N + 1 points
xj = −1 + 2 N ,j = 0, .., N (2)


  1. Unevenly-spaced grid with N + 1 points (Chebyshev quadrature points)



xj = cos      N j  ,   j = 0, 1, …, N,                                                (3)

In particular, write a Matlab function test Lagrange interpolation.m

function test Lagrange interpolation()

that plots the function (1) (in blue) and the Lagrangian interpolants (in red) obtained by using both the evenly-spaced and the unevenly-spaced grids in the cases N = 8 and N = 20 (4 different figures). Each figure should include the graph of f (x), the data points {xi, f (xi)} and the interpolant ΠN f (x) through those points. Which set of nodes gives us the smallest error? Which one the largest?


EXERCISE 3 Let {li(x)}i=0,..,N be the set of Lagrange characteristic polynomials associated with the nodes {xj }j=0,..,N . We have seen in class that the polynomial interpolation error is related to the Lebesgue function

λN (x) = X |lj (x)| (Lebesgue function), (4)


and the Lebesgue constant

ΛN =  max  λN (x) (Lebesgue constant). (5)


  1. Write a Matlab function plot Lebesgue function.m that plots the Lebesgue function (4) and returns the value of the Lebesgue constant (5) for any given set of interpolation nodes. The function should be in the form

function [L]=plot Lebesgue function(xi)


xi: vector of interpolation nodes xi=[xi(1) …     xi(N+1)]


Plot of the Lebesgue function λN (x)

L: Lebesgue constant ΛN

  1. Apply the function plot Lebesgue function(xi) to the four cases of evenly- and unevenly-spaced grids you studied in Exercise 2. To this end, write a Matlab function

function [L1,L2,L3,L4]=test Lebesgue function()

that plots the Lebesgue function (4) corresponding to the aforementioned four cases (4 differ-ent figures), and returns the value of the Lebesgue constant for each case.

Remark: In general, the smaller the Lebesgue constant the smaller the approximation error of polynomial interpolation. If fact, the following error estimate holds true


kf (x) − ΠN (x)k ≤ (1 + ΛN ) inf  kf (x) − ψ(x)k (6)



































error: Content is protected !!