write matlab function called lagrange basis whose inputs are x:array of points,
ID: 673457 • Letter: W
Question
write matlab function called lagrange basis whose inputs are x:array of points, j:index of the basis function, and xi:column vector of interpolation points xi, and whose output is y=Lj(x), the value of the nth Lagrange basis function at the point x. xi=0,5,10,15,20,25
function y = lagrange_basis_eval(x,j,xi)
% Description: Evaluate the jth Lagrange basis function Lj(x) at x. We
% construct Lj using the nodes xi, in particular
%
% Lj(x) = prod_{i~=j} (x - xi(i)) / prod_{i~=j} (xi(i)-xi(j)).
%
%
% Usage: y = lagrange_basis_eval(x,j,xi)
%
%
% Inputs
%
% x: double, array of points at which we want to evaluate the basis function.
%
% j: int, index of Lagrange basis function.
%
% xi: double, interpolation points needed to construct the Lagrange basis
% functions.
%
%
% Output
%
% y: double, array the size of x whose entries are y = Lj(x).
%
%
% Dependencies
%
% none
%
%
% YOUR NAME HERE
% October 2, 2015
% ------------------------------------------------------------------------------
y = ones(size(x)); % initialize y = 1
n = length(xi)-1; % determine number of interpolation points
for j = 0:n
% YOUR CODE HERE
end
Explanation / Answer
LAGRANGE function:
for j = 0:n
for k=0:n
if (j~=k)
y(j,:)=y(j,:).*(x-pointx(k))/(pointx(j)-pointx(k));
end
end
end
p=0;
for m=0:n
p=p+pointy(m)*y(m,:);
end
% call the function lagrange y
y(x,j,xi {0,5,10,15,20,25)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.