I'm trying to replace "t" in the code with ti=a + (b−a)*((xi+1)/2) to plot the Lagrange polynomial interpolation with Chebyshev points.

1 view (last 30 days)
clear
n = 8; % the order of the polynomial
a = 1; % left end of the interval
b = -1; % right end of the interval
h = (b - a)/n; % interpolation grid size
t = a:h:b; % interpolation points
f = 1./(1 + 25*t.^2); % f(x) = 1./(1 + 25*t.^2), This is the function evaluated at interpolation points
%%%% pn(x) = \sum f(t_i)l_i(x)
hh = 0.01; % grid to plot the function both f and p
x = a:hh:b;
fexact = 1./(1 + 25*x.^2); %exact function f at x
l = zeros(n+1, length(x)); %%%% l(1,:): l_0(x), ..., l(n+1): l_n(x)
nn = ones(n+1, length(x));
d = ones(n + 1, length(x));
for i = 1:n+1
for j = 1:length(x)
nn(i,j) = 1;
d(i,j) = 1;
for k = 1:n+1
if i ~= k
nn(i,j) = nn(i,j) * (x(j) - t(k));
d(i,j) = d(i,j) * (t(i) - t(k));
end
end
l(i,j) = nn(i,j)/d(i,j);
end
end
fapp = zeros(length(x),1);
for j = 1:length(x)
for i=1:n+1
fapp(j) = fapp(j) + f(i)*l(i,j);
end
end
En = 0;
Ed = 0;
for i = 1:length(x)
Ed = Ed + fexact(i)^2;
En = En + (fexact(i) - fapp(i))^2;
end
Ed = sqrt(Ed);
En = sqrt(En);
E = En/Ed;
display(E)
plot(x,fexact,'b*-')
hold on
plot(x,fapp,'ro-' )
  1 Comment
ebtisam almehmadi
ebtisam almehmadi on 29 Jul 2021
I have no clue how to code xi to make the code works, where xi=−cosθi, θi=ih,h=π/n, i= 0:n.
the plot for n=8 should be the same as above figure.
code must give nine chebychev points for n=8

Sign in to comment.

Answers (1)

Ashutosh Singh Baghel
Ashutosh Singh Baghel on 6 Aug 2021
Edited: Ashutosh Singh Baghel on 6 Aug 2021
Hi ebtisam,
I understand you faced issue with 'plot', please update the variable 'hh' with proper value as a decrement, instead of increment.
example =
hh = -0.01; %provide a negative sign as a>b
Also, from the comment you provided, try to write the equation into a code format.
The reference guide for creating a linear, equally spaced vector can be found here.

Categories

Find more on Polynomials in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!