Interpolating Polynomials Question Code

3 views (last 30 days)
I'm having some trouble getting this to run. Any suggestions are appreciated. This is what I have done so far and I am getting an error with Y = f(X)
% HW5 - Problem 2
% Chebyshev versus Newton
clear all
f = @(x) exp^(-(x^2)); % function
% Newton
X = -5:5; % equidistant points to compute the interpolating polynomial
Y = f(X); % corresponding y values
x = 0:0.01:10; % points to plot
y = f(x);
C = divdiff(X,Y); % computes the diagonal from the divided difference table
N = zeros(1,length(x));
for i=1:length(x)
N(i) = newtval(C,X,x(i)); % evaluates the newton polynomial at x(i)
end
subplot(1,2,1)
plot(X,Y,'o',x,y,'k',x,N,'r'),title('Newton Interpolation'),
legend('Data','Exact','Newton')
axis([0 10 -2 15])
% Chebyshev
N = length(X); % same number of points as in Newton;
t = zeros(1,N);
c = zeros(1,N);
for k=1:N
t(k) = cos( (2*k-1)*pi/(2*N) ); % Chebyshev points
c(k) = t(k)*(10-0)/2 + (10+0)/2; % Chebyshev points in [0,10]
end
Y = f(c);
C = divdiff(c,Y);
Ch = zeros(1,length(x));
for i=1:length(x)
Ch(i) = newtval(C,c,x(i));
end
subplot(1,2,2)
plot(c,Y,'o',x,y,'k',x,Ch,'r'),title('Chebyshev Interpolation'),
legend('Data','Exact','Chebyshev')
axis([0 10 -2 15])

Answers (1)

Walter Roberson
Walter Roberson on 10 Nov 2015
f = @(x) exp(-(x.^2)); % function

Categories

Find more on Interpolation 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!