$24
Please submit to CANVAS a .zip le that includes the following Matlab functions:
Lagrange interp.m
test Lagrange interpolation.m
compute Lebesgue function.m
test Lebesgue function.m
Exercise 1 Write a function Lagrange interp.m that computes the Lagrangian interpolant of a given set of data points (xi; yi), i = 1, 2, :::. The function should be of the form
function [y] = Lagrange interp(xi,yi,x)
Input:
xi: vector of interpolation nodes
yi: vector of data points at interpolation nodes
x: vector of points at which we evaluate the polynomial interpolant
Output:
y: polynomial interpolant evaluated at x
Hint: Compare the output of your function with the output of the Matlab/Octave built-in function,
y=polyval(polyfit(xi,yi,length(xi)-1),x)
(see the Matlab/Octave documentation).
Exercise 2 Consider the nonlinear function
f(x) =
1
;
x 2 [ 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 fxi; f(xi)gi=0;::;N in the following cases:
1
1. Evenly-spaced grid with N + 1 points
j
xj = 1 + 2 N
;j = 0; ::; N
(2)
2. Unevenly-spaced grid with N + 1 points (Chebyshev-Gauss-Lobatto points)
xj = cos
j ; j = 0; 1; :::; N;
(3)
N
In particular, write a Matlab function test Lagrange interpolation.m
function [x,f,P1,P2,P3,P4]=test Lagrange interpolation()
that returns the follwing items:
x: vector of 1000 evenly-spaced nodes in [ 1; 1] (use the command x=linspace(-1,1,1000)).
f: vector representing (1) evaluated at x.
P1: Lagrangian interpolant of (1) built on the grid (2) with N = 8 nodes and evaluated at x.
P2: Lagrangian interpolant of (1) built on the grid (2) with N = 20 nodes and evaluated at x.
P1: Lagrangian interpolant of (1) built on the grid (3) with N = 8 nodes and evaluated at x.
P1: Lagrangian interpolant of (1) built on the grid (3) with N = 20 nodes and evaluated at x.
The function should also plot (1) (in blue) and the Lagrangian interpolants (in red) obtained by using both the evenly-spaced and the unevenly-spaced grids for the cases N = 8 and N = 20 (4 di erent gures). Each gure should include the graph of f(x), the data points fxi; f(xi)g and the interpolant N f(x) through those points.
Hint: See the code uploaded in CANVAS for examples of similar plots.
Exercise 3 Let fli(x)gi=0;::;N be the set of Lagrange characteristic polynomials associated with the nodes fxj gj=0;::;N . We have seen in class that the polynomial interpolation error is related to the Lebesgue function
N
Xj
jlj(x)j
N (x) =
(Lebesgue function);
(4)
=0
and the Lebesgue constant
N = max
N (x)
(Lebesgue constant).
(5)
x2[ 1;1]
Given the vector of interpolation nodes xi=[xi(1) ... xi(N+1)], write a Matlab/Octave function compute Lebesgue function.m that returns the Lebesgue function (4) evaluated at
2
1000 evenly-spaced nodes between xi(1) and xi(N+1). Such function should also return the Lebesgue constant (5).
function [lambda,L]=compute Lebesgue function(xi)
Input:
xi: vector of interpolation nodes xi=[xi(1) ... xi(N+1)]
Output:
lambda: Lebesgue function N (x) evaluated at 1000 evenly-spaced nodes between xi(1) and xi(N+1).
L: Lebesgue constant N .
Apply the function compute Lebesgue function(xi) to the four cases of evenly- and unevenly-spaced grids you studied in Exercise 2. To this end, write a function
function [L1,L2,L3,L4]=test Lebesgue function()
that plots the Lebesgue function (4) corresponding to the aforementioned four cases (in 4 di erent Figures), and returns the value of the Lebesgue constant for each case.
Remark: Recall, that the smaller the Lebesgue constant the smaller the approximation error of the Lagrangian polynomial interpolation. If fact, we have seen in class that
kf(x) N (x)k1
(1 + N ) infN kf(x) (x)k1
(6)
2P
3