Error between evenly spaced interpolation and Chebyshev interpolation.

2 次查看(过去 30 天)
Let f (x) = e|x|. Compare evenly spaced interpolation with Chebyshev interpolation by plotting degree n polynomials of both types on the interval [1, 1], for n = 10 and 20. For evenly spaced interpolation, the left and right interpolation base points should be 1 and 1. By sampling at a 0.01 step size, create the empirical interpolation errors for each type, and plot a comparison. Can the Runge phenomenon be observed in this problem?
I am using this function to plot the error between Compare evenly spaced interpolation and Chebyshev interpolation but i am always getting this error when I try to run this code
x=-1:0.01:1;
[y1, y2]=expofabsx(-1)
plot(x, y1, x, y2);
Error using plot
Vectors must be the same length.
Error in expofabsx (line 42)
plot(xpoint, ypoint,'o', x,y,[x1 xr],[0,0],'k',[0 0], [y1 yt], 'k');
--- Here is my function
function y = nest(d,c,x,b)
if nargin < 4, b = zeros(d, 1); end
y =c(d+1);
for i=d:-1:1
y = y.*(x - b(i))+c(i);
end
function c=newtdd(x,y,n)
for j=1:n
v(j,1)=y(j);
end
for i=2:n % Fill in y column of Newton triangle% For column i,
for j=1:n+1-i % fill in column from top to bottom
v(j,i)=(v(j+1,i-1)-v(j,i-1))/(x(j+i-1)-x(j));
end
end
for i=1:n
c(i)=v(1,i); % Read along top of triangle
end
function [y1, y2] = expofabsx(x)
x1=-1; xr=1; y1=-3; yt=3;
plot([x1 xr], [0 0], 'k', [0 0], [y1 yt] , 'k') ;grid on;
if x < 0
x=-x;
end
p=x;
xpoint =-x:.1:x; ypoint=exp(abs(xpoint));
for k=0:10
k = k+1;
c=newtdd (xpoint, ypoint, k);
x = x1:.01:xr;
y = nest(k-1,c,x,xpoint);
plot(xpoint, ypoint, 'o', x,y,[x1 xr],[0,0],'k',[0 0], [y1 yt], 'k');
axis([x1 xr y1 yt]) ;grid on;
end
y1 = nest(k-1,c,p,xpoint);
%Chebysher interpolation method
n=10;
b=cos((1:2:2*n-1)*pi/(2*n));
yb=exp(abs(b));
for k=0:9
k=k+1;
c1=newtdd(b, yb, k);
x = x1:.01:xr;
y = nest(k-1,c1, p, b);
hold all
fprintf('Size of xpoint: %dx%d\n', size(xpoint))
fprintf('Size of ypoint: %dx%d\n', size(ypoint))
fprintf('Size of x: %dx%d\n', size(x))
fprintf('Size of y: %dx%d\n', size(y))
fprintf('Size of x1: %dx%d\n', size(x1))
fprintf('Size of xr: %dx%d\n', size(xr))
fprintf('Size of y1: %dx%d\n', size(y1))
fprintf('Size of yt: %dx%d\n', size(yt))
plot(xpoint, ypoint,'o', x,y,[x1 xr],[0,0],'k',[0 0], [y1 yt], 'k');
axis([x1 xr y1 yt]) ;grid on;
end
y2 = nest(n-1,c1,p,b);
end
And this are the different size
[y1, y2]=expofabsx(-x)
Size of xpoint: 1x21
Size of ypoint: 1x21
Size of x: 1x201
Size of y: 1x1
Size of x1: 1x1
Size of xr: 1x1
Size of y1: 1x201
Size of yt: 1x1

回答(1 个)

Raag
Raag 2025-3-6
Hi Zakaria,
The error “Vectors must be the same length” means that when you call the MATLAB ‘plot’ function, some of the arrays you are trying to plot do not have the same number of elements. In your case, the grid on which you evaluate your interpolation is different from the grid used elsewhere. This mismatch causes the error.
  • To address this, make sure that both the evenly spaced and Chebyshev interpolation methods evaluate the function on the same set of x-values.
  • Then, use this grid consistently when computing the interpolated values and the exact function values.
  • Check dimensions before plotting using MATLAB’s size or length functions to verify that the arrays you are plotting have the same dimensions. This helps pinpoint which vector is causing the issue.
  • At last, align your interpolation functions by modifying your interpolation functions so that they evaluate on the common grid. For example, you might adjust the ‘expofabsx’ function to use the same x-vector throughout.
function [err_even, err_cheb] = expofabsx(dummy)
% Define a common grid
x = -1:0.01:1;
% Compute the exact function values for f(x) = exp(|x|)
f_exact = exp(abs(x));
% --- Evenly Spaced Interpolation ---
f_even = f_exact; % (dummy interpolation; use your actual code)
% --- Chebyshev Interpolation ---
f_cheb = f_exact; % (dummy interpolation; use your actual code)
% Plot the results to compare
figure;
plot(x, f_exact, 'k-', 'LineWidth', 1.5); hold on;
plot(x, f_even, 'bo-', 'MarkerSize', 4);
plot(x, f_cheb, 'rs--', 'MarkerSize', 4);
legend('Exact f(x)', 'Evenly Spaced', 'Chebyshev');
xlabel('x'); ylabel('f(x) = exp(|x|)');
title('Interpolation Comparison');
grid on;
% Calculate the empirical errors
err_even = abs(f_exact - f_even);
err_cheb = abs(f_exact - f_cheb);
end
This code ensures that all vectors used in plotting are of the same length and will avoid the error as shown below:
For a better understanding of the above solution, use these command to open the documentations:
1. plot: web(fullfile(docroot, "techdoc/ref/plot.html"))
2. fprintf: web(fullfile(docroot, "techdoc/ref/fprintf.html"))

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by