📄 demo_interpol.m
字号:
%% Demonstrate Polynomial Interpolation%disp(' Compute interpolation polynomial using three methods')disp(' and compare the results ')disp(' Hit return to continue')pause% generate problem data% example interpolate the error function at xx = (1:0.2:3)';f = erf(x);% plot the error function and the interpolating polynomials at zz = (0:0.01:4)';fz = erf(z); % Number of points at which to interpolaten = size(x(:),1);% Number of points at which to evaluate interpolation pol.m = size(z,1);% Polynomial interpolation using monomialsV = ones(n,n);for i = 2:n V(:,i) = V(:,i-1).*x;enda = V\f;% Evaluate the interpolation polynomial using Horner's schemeM = a(n)*ones(m,1);for i = n-1:-1:1 M = a(i)*ones(m,1) + M.*z;end% Polynomial interpolation using Lagrange polynomialsL = ones(m,n);for i = 1:n for j = 1:n if( i ~= j ) L(:,i) = L(:,i).*(z-x(j))./(x(i)-x(j)); end endend% Polynomial interpolation using Newton polynomialsa = NewtIntPol( x, f, 0 );N = NewtIntPolEval( x, a, z ); subplot(2,2,1)semilogy( z, fz );xlabel('x') subplot(2,2,2)semilogy( z, abs(fz-M) ); xlabel('x') subplot(2,2,3)semilogy( z, abs(fz-L*f) );xlabel('x') subplot(2,2,4)semilogy( z, abs(fz-N) );xlabel('x')print -deps erf-interp1.eps pausesubplot(1,1,1)semilogy( z, abs(N-M), '--'); hold onsemilogy( z, abs(N-L*f), '-'); hold offlegend('Newton-Monomial','Newton-Lagrange')title('Error between interpolation polynomials')xlabel('x')print -deps erf-interp2.eps
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -