#include <stdio.h> #include <math.h> #define N 10 #define NUM_X 5 int main() { int i,nfunc; array double f[NUM_X],x[NUM_X],y[NUM_X]; array float xa[N],ya[N]; /* Generation of interpolation tables of sin(x) function */ linspace(xa, 0, M_PI); ya = sin(xa); linspace(x, 0.15, M_PI-0.15); f=sin(x); /*Spline interpolation */ printf("\nCubic spline interpolation of function sin(x)\n"); printf("\n%9s %13s %17s\n","x","f(x)","interpolation"); interp1(y, x, xa, ya, "spline"); for(i=0;i<NUM_X;i++) printf("%12.6f %12.6f %12.6f\n",x[i],f[i],y[i]); /*Linear interpolation */ printf("\nLinear interpolation of function sin(x)\n"); printf("\n%9s %13s %17s\n","x","f(x)","interpolation"); interp1(y, x, xa, ya, "linear"); for(i=0;i<NUM_X;i++) printf("%12.6f %12.6f %12.6f\n",x[i],f[i],y[i]); }
Cubic spline interpolation of function sin(x) x f(x) interpolation 0.150000 0.149438 0.149431 0.860398 0.758102 0.758072 1.570796 1.000000 0.999960 2.281194 0.758102 0.758072 2.991593 0.149438 0.149431 Linear interpolation of function sin(x) x f(x) interpolation 0.150000 0.149438 0.146972 0.860398 0.758102 0.746562 1.570796 1.000000 0.984808 2.281194 0.758102 0.746562 2.991593 0.149438 0.146972