% Demo with different types of spline interpolation f = inline('2.^x') x = linspace(-.2, 4.2); y = f(x); X = [0 1 2 4] Y = f(X) plot(X,Y,'o',x,y), axis([-0.2 4.2 -1 20]); grid on pause % linear interpolation p = interp1(X,Y,x); plot(X,Y,'o',x,y,x,p), axis([-0.2 4.2 -1 20]), grid on title('Linear Splines') pause % Hermite Spline S = log(2)*Y a = pwch(X,Y,S); p = ppval(a,x); plot(X,Y,'o',x,y,x,p); grid on, axis([-0.2 4.2 -1 20]) title('Hermite Spline') pause plot(x,p-y), grid on, axis([-0.2 4.2 -0.1 0.03]) title('Error in Hermite Spline') pause % Not a knot spline a = spline(X,Y); p = ppval(a,x); plot(X,Y,'o',x,y,x,p), axis([-0.2 4.2 -1 20]); grid on title('Not-a-Knot Spline') pause plot(x,p-y), axis([-0.2 4.2 -0.1 0.4]); grid on title('Error Not-a-Knot Spline') pause; % Clamped spline Y1 = [log(2) Y 16*log(2)] a = spline(X,Y1); p = ppval(a,x); plot(X,Y,'o',x,y,x,p), axis([-0.2 4.2 -1 20]); grid on title('Clamped Spline') pause plot(x,p-y), axis([-0.2 4.2 -0.15 0.05]); grid on title('Error Clamped Spline') pause; % Shape preserving a = pchip(X,Y); p = ppval(a,x); plot(X,Y,'o',x,y,x,p), axis([-0.2 4.2 -1 20]); grid on title('Shape Preserving Spline') pause plot(x,p-y), axis([-0.2 4.2 -0.1 0.8]); grid on title('Error Shape Preserving Spline')