when I run the code, I only get one plot for f. I need to have a plot for p as well. I need to code this part p(x) =f(x0)l0(x) +f(x1)l1(x) +f(x2)l2(x) to make it works
Mostrar comentarios más antiguos
clear all
n = 3; % the order of the polynomial
a = 2.0; % left end of the interval
b = 3.0; % right end of the interval
h = (b - a)/n; % interpolation grid size
t = a:h:b; % interpolation points
f = 1./t; % f(x) = 1./x, 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;
l = zeros(n+1, length(x)); %%%% l(1,:): l_0(x), ..., l(n+1): l_n(x)
for i = 1:n+1
j = 1:length(x)
n(i,j) = 1;
d(i,j) = 1;
for k = 1:n+1
if i ~= k
n(i,j) = n(i,j) * (x(j) - t(k));
d(i,j) = d(i,j) * (t(i) - t(k));
end
end
l(i,j) = n(i,j)/d(i,j);
end
sum = 0;
for i=1:n+1
sum = sum + f(i)*L(i,j);
end
plot(t,f,'-b')
hold on
plot(x,sum,'--r' )
1 comentario
ebtisam almehmadi
el 13 de Jul. de 2021
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!