# Lab Solutions

## Introduction

For each of the following problems, we will use the data generated here.

function lab_10_solns()
close all;

format short;

N = 5;
xdata = linspace(0,1,N+1)';
prob1(xdata);
prob2(xdata);
prob3(xdata);
prob4(xdata);
end  % end of lab_10_solns

Back to the top

## Problem #1

function prob1(xdata)

Solve the Vandermonde matrix system to get the coefficients. We could solve for each set of coefficients independently, or we could just get the inverse of the Vandermonde matrix. Column j contains the coefficients for the Lagrange poylnomial $\ell_j(x)$.

V = vander(xdata);

fprintf('Coefficients are \n');
coeffs = inv(V);
disp(coeffs);
Coefficients are
-26.0417  130.2083 -260.4167  260.4167 -130.2083   26.0417
78.1250 -364.5833  677.0833 -625.0000  286.4583  -52.0833
-88.5417  369.7917 -614.5833  510.4167 -213.5417   36.4583
46.8750 -160.4167  222.9167 -162.5000   63.5417  -10.4167
-11.4167   25.0000  -25.0000   16.6667   -6.2500    1.0000
1.0000         0         0         0         0         0



Show that the sum of the coefficients is 1.

disp(sum(coeffs,2)');
   -0.0000    0.0000   -0.0000    0.0000   -0.0000    1.0000



Since only the last entry in the sum vector is 1, the polynomial represented as the sum of the coefficients for each Lagrange polynomial is the polynomial equal to the constant function 1.

end % end of prob1

Back to the top

## Problem #2

function prob2(xdata)

Using polyfit, we have to loop over each column of the identity matrix separately.

N = length(xdata)-1;
E = eye(N+1);
coeffs = zeros(N+1,N+1);
for j = 1:N+1,
ydata = E(:,j);
coeffs(:,j) = polyfit(xdata,ydata,N);
end
fprintf('Coefficients are \n');
disp(coeffs);
Coefficients are
-26.0417  130.2083 -260.4167  260.4167 -130.2083   26.0417
78.1250 -364.5833  677.0833 -625.0000  286.4583  -52.0833
-88.5417  369.7917 -614.5833  510.4167 -213.5417   36.4583
46.8750 -160.4167  222.9167 -162.5000   63.5417  -10.4167
-11.4167   25.0000  -25.0000   16.6667   -6.2500    1.0000
1.0000   -0.0000    0.0000   -0.0000    0.0000   -0.0000



Show that the sum of the coefficients is 1.

disp(sum(coeffs,2)');
    0.0000   -0.0000    0.0000   -0.0000    0.0000    1.0000


end % end of prob2

Back to the top

## Problem #3

function prob3(xdata)

In this method, we can't really obtain the coefficients of the Lagrange polynomials, but can only evaluate $\ell_j(x)$ at particular $x$ values. This is the function that was used in the demonstration of Lagrange polynomials.

    % jth Lagrange polynomial
function lx = ell_j(xdata,xvec,j)

% Construct a vector
xj = xdata(j);  % Save for below
xdata(j) = [];  % Delete entry xdata(j)

lx = 0*xvec;   % we want to return a vector lx that
% has the same shape as vector xvec

for m = 1:length(xvec),
x = xvec(m);

num = prod(x-xdata);
denom = prod(xj - xdata);
lx(m) = num/denom;
end
end
end  % end of prob3

Back to the top

## Problem #4

function prob4(xdata)

We can construct the coefficients of the Lagrange polynomial explicitly using conv. This command produces the coefficients of the product of two polynomials. For example, suppose we want the coefficients of the polynomial product $$p(x) = (x - 3)(x - 5)$$

We use conv as

a = conv([1 -3],[1 -5]);
disp(a);
     1    -8    15



The result gives us the coefficients of the expanded polynomial $$p(x) = (x - 3)(x - 5) = x^2 - 8x + 15$$

We can use this idea to obtain the coefficients of the Lagrange polynomial explicitly.

N = length(xdata)-1;
coeffs = zeros(N+1,N+1);
for j = 1:N+1
coeffs(:,j) = lagrange_coeffs(xdata,j);
end
fprintf('Coefficients are \n');
disp(coeffs);
Coefficients are
-26.0417  130.2083 -260.4167  260.4167 -130.2083   26.0417
78.1250 -364.5833  677.0833 -625.0000  286.4583  -52.0833
-88.5417  369.7917 -614.5833  510.4167 -213.5417   36.4583
46.8750 -160.4167  222.9167 -162.5000   63.5417  -10.4167
-11.4167   25.0000  -25.0000   16.6667   -6.2500    1.0000
1.0000         0         0         0         0         0



Show that the sum of the coefficients is 1.

disp(sum(coeffs,2)');
    0.0000   -0.0000    0.0000   -0.0000    0.0000    1.0000



Function used to compute the coefficients of the Lagrange polynomials.

    function a = lagrange_coeffs(xdata,j)

xj = xdata(j);
xdata(j) = [];
N = length(xdata);

a = 1;
for i = 1:N,
a = conv(a,[1 -xdata(i)]);
end

a = a(:)/polyval(a,xj);
end
end % end of prob4

Back to lab exercises.

Back to the top

## Get the code

Do you want to try the above code fragments on your own? Download the Matlab script that produces this page here. (lab_10_solns.m) Published with MATLAB® 8.2