Implement functions for interpolation of tabulated data.
Problems:You can use either arrays, or gsl_vector
's (or
gsl_matrix
) to keep your tabulated data. Depending on that
the signature of your function might be something like
double linterp(int n, double x[], double y[], double z); double linterp(gsl_vector x, gsl_vector y, double z); double linterp(gsl_matrix xy, double z);
The function must first find (by binary search) the interval where z is located, x[i]≤z≤x[i+1], and then calculate the interpolated value using linear spline,
double slope=(y[i+1]-y[i])/(x[i+1]-x[i]); double value=y[i]+slope*(z-x[i]); return value;
Implement a function that calculates the intergral of the linear spline of the tabulated data from the point x[0] to the given point z. The integral must be calculated analytically,
∫dx (yi+pi(x-xi)) = yi(x-xi)+pi(x-xi)2/2 + const .The signature could be something like
double linterp_integ(gsl_vector x, gsl_vector y, double z);
Make some indicative plots to prove that your linear spline
and your integrator work as intended. Compare with GSL's
[gsl_interp_linear
]
and
[gsl_interp_eval_integ
]
(see this example: "gsl_interp").
int binsearch(int n, double* x, double z){/* locates the interval for z by bisection */ assert(n>1 && x[0]<=z && z<=x[n-1]); int i=0, j=n-1; while(j-i>1){ int mid=(i+j)/2; if(z>x[mid]) i=mid; else j=mid; } return i; }
(3 points) Quadratic spline
Implement quadratic spline with derivative and integral. Make some illustrative plots.
Note that quadratic splines should only be used for learning, for practical applications always use cubic splines instead.
Hints:
(1 points) Cubic spline
Compare with the cubic spline routines from GSL.