Implement functions for interpolation of tabulated data.
double linterp(int n, double *x, double *y, double z); /* C */ def linterp(x:list, y:list, z:float): # procedural python
Implement a function that calculates the intergral of the linear spline from the point x[0] to the given point z. The integral must be calculated analytically of course as it is an integral of a linear function. The signature could be something like
double linterp_integ(int n, double *x, double *y, double z); /* C or procedural C++ */ def linterp_integ(x:list, y:list, z:float): # procedural python
Make some plots to prove that your linear spline and your integrator work as intended.
Hints:
graph
utility from
plotutils
. Install it with sudo apt-get install
plotutils
. It is a simple filter which takes data in the form
of two columns, for x and y, from the standard input (or from a file)
and sends a plot in the given format to the standard output, for example,
graph --output-format svg < data > plot.svgA blank line in the data-file separates different data sets. An example of
graph
usage can be found
here and
here.
Implement quadratic spline with derivative and integral. Note that quadratic spline is only for learning, for practical applications always use cubic spline instead.
Hints:
typedef struct {int n; double *x, *y, *b, *c;} qspline;
struct qspline * qspline_alloc(int n, double *x, double *y); /* allocates and builds the quadratic spline */ double qspline_evaluate(struct qspline *s, double z); /* evaluates the prebuilt spline at point z */ double qspline_derivative(struct qspline *s, double z); /* evaluates the derivative of the prebuilt spline at point z */ double qspline_integral(struct qspline *s, double z); /* evaluates the integral of the prebuilt spline from x[0] to z */ void qspline_free(struct qspline *s); /* free memory allocated in qspline_alloc */
def qspline(x:list,y:list) : # calculate b[i],c[i] def eval(z:float, deriv:int=0) if deriv==1 : # calculate and return derivative elif deriv==-1 : # calculate and return integral else : # calculate and return spline return eval # functional style
(1 points) Cubic spline
For C,C++: check that the GSL cubic spline functions give similar results to your cubic spline.
For languages with available libraries (like NumPy): check that the library implementation of cubic spline gives the same result as your spline.
In case of no libraries: Check that the spline
utility from plotutils produces
a similar cubic spline to your implementation.