Interpolating Polynomials Question Code
조회 수: 1 (최근 30일)
이전 댓글 표시
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])
댓글 수: 0
답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Polynomials에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!