demo_runge.m

来自「Interpolation routines in matlab」· M 代码 · 共 79 行

M
79
字号
%%  interpolation of Runge's function (1+x^2)^(-1)%  on the interval [-5, 5]%clear;% Interval on which Runge's function is to be approximatedA = -5;B =  5;% evaluate Runge's function at zm = 1000;h = (B-A)/(m-1);z = (A:h:B);r = 1 ./(1+z.^2);n = input(' number of interpolation points  n = ')%   compute equidistant interpolation points%   and function values to be interpolated    h = (B-A)/(n-1);    x = (A:h:B);         f = 1 ./ (1+x.^2);%   compute Newton's interpolating polynomial    a = NewtIntPol( x, f, 0 );%   evaluate Newton's interpolating polynomial at the %   points z and plot it together with Runge's function.    p = NewtIntPolEval( x, a, z );%   plot the interpolating polynomial    subplot(2,1,1)    plot(z, p, 'r'); hold on   % plot polynomial    plot(z, r, 'g'); hold off  % plot Runge's function    title(['Interpolation of Runge''s Function at Equidistant Points (n=',...            int2str(n),')'] )%   compute Chebyshev interpolation points%   and function values to be interpolated    for i = 1:n       x(i) = 0.5 * ( (A+B) + (B-A) * cos( (2*i-1)*pi/ (2*n) ) );            f(i) = 1 / (1+x(i)^2);    end%   compute Newton's interpolating polynomial    a = NewtIntPol( x, f, 0 );%   evaluate Newton's interpolating polynomial at the %   points z and plot it together with Runge's function.    p = NewtIntPolEval( x, a, z );%   plot the interpolating polynomial    subplot(2,1,2)    plot(z, p, 'r'); hold on   % plot polynomial    plot(z, r, 'g'); hold off  % plot Runge's function    title(['Interpolation of Runge''s Function at Chebyshev Points (n=',...            int2str(n),')'] )

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?