QUIZ 7
We have given the following MATLAB code to evaluate the Lagrange polynomial,
function [C,L]=lagran(X,Y)
%Input - X is a vector that contains a list of abscissas
% - Y is a vector that contains a list of ordinates
w=length(X);
n=w-1;
L=zeros(w,w);
for k=1:n+1
V=1;
for j=1:n+1
if k~=j
V=conv(V,poly(X(j)))/(X(k)-X(j));
end
end
L(k,:)=V;
end
C=Y*L;
where
The poly command creates a vector whose entries are the coefficients of a polynomial with specified roots.
>>P=poly(2)
>> 1 -2
>>Q=poly(3)
>> 1 -3
The conv command produces a vector whose entries are the coefficients of a polynomial that is the product of two other polynomials.
>>conv(P,Q)
>> 1 -5 6 %Thus the product of P(x) and Q(x) is x^2-5x+6
And
>> X=[1 2 3 5]
>> Y=[1.06 1.12 1.34 1.78]
>> [C,L]=lagran(X,Y)
C = -0.0200 0.2000 -0.4000 1.2800
L =
-0.1250 1.2500 -3.8750 3.7500
0.3333 -3.0000 7.6667 -5.0000
-0.2500 2.0000 -4.2500 2.5000
0.0417 -0.2500 0.4583 -0.2500
Describe the meaning of the vector C.