% Compare Error of Various Interpolation Schemes % Newton, Hermite, Cubic Splines % function [errN,errH,errS] = CompareInterp(func,funcH,xmin,xmax,N,plt) % Inputs: % func = function to interpolate, returns f(x) % funcH = same function to interpolate, but returns f(x) and f'(x) % xmin,xmax = range of interpolation set % N = array of number of interpolation points to try, % evenly spaced from xmin to xmax % plt = 1 to plot errors % = 0 not to % Outputs: % errN = error of Newton Interpolation with N(i) points % on 1000 numbers between xmin, xmax % errH = error of Hermite Interpolation with N(i) points % on 1000 numbers between xmin, xmax % errS = error of cubic spline Interpolation with N(i) points % on 1000 numbers between xmin, xmax % function [errN,errH,errS] = CompareInterp(func,funcH,xmin,xmax,N,plt) for i = 1:length(N), n = N(i); x = xmin:(xmax-xmin)/(n-1):xmax; [c,errN(i),NewtonTable] = NewtonInterp(func,x,0); [c,errH(i),NewtonTable] = HermiteInterp(funcH,x,0); [xout,yout,errS(i)] = SplineFit(func,x,0); end errN = errN'; errH = errH'; errS = errS'; if (plt==1) figure(1) hold off, clf semilogy(N,errN,'b',N,errH,'r',N,errS,'k') grid title('Interpolation error (Newton = blue, Hermit = red, Cubic Spline = black)') xlabel('Number of Interpolation points') end