function poly_interp( n, icase ) %function poly_interp( n, icase ) % % Matlab function to study polynomial interpolation to various test functions % (determined by switch 'icase'). % % Using equi-spaced vs Chebyshev vs modified Chebyshev nodes on [-1,1] % for the input value of 'n'. % % C.G. (11-96) % m = 1000 ; t = [ 0 : m ]' * 2 / m - 1 ; % number/vector of evaluation points % % Generate data for plotting true function for this test case. % if ( icase == 1 ) func = ' f(x) = sin( pi * x ) , ' ; % smooth function: sin(pi*x) fplot = sin( pi * t ) ; elseif ( icase == 2 ) func = ' f(x) = 1 / ( 1 + 16 x^2 ) , ' ; % Runge's nasty example fplot = 1 ./ ( 1 + 16 * t.^2 ) ; end % % Put the different node sets in the columns of the array tau. % tau = zeros( n + 1, 3 ) ; % initialize an array of the proper dimensions tau(:,1) = [ 0 : n ]' * 2 / n - 1 ; % equally spaced nodes on [-1,1] tau(:,2) = - cos( ( 2 * [0:n]' + 1 ) * pi / ( 2 * n + 2 ) ) ; % Chebyshev nodes tau(:,3) = - cos( [ 0 : n ]' * pi / n ) ; % modified Chebyshev nodes % % Generate the interpolant for this test case and plot against true function. % P = zeros( m + 1, 3 ) ; % initialize an array of the proper dimensions for k = 1 : 3 if ( icase == 1 ) fdata = sin( pi * tau(:,k) ) ; % smooth function: sin(pi*x) elseif ( icase == 2 ) fdata = 1 ./ ( 1 + 16 * tau(:,k).^2 ) ; % Runge's nasty example end P(:,k) = polyval( polyfit( tau(:,k), fdata, n ), t ) ; end clf , plot( t, [ fplot, P ] ) title( [ 'Chebyshev vs Equi-Spaced Interpolation, ', ... func, ' {\it{n}} = ', num2str(n) ] ) xlabel( '\it{x}' ) , ylabel( '\it{y}' )