You can implement custom end conditions using the `csape`

function. Suppose you want to enforce the following condition at the leftmost endpoint, $\mathit{e}=$`x(1)`

$$\lambda (s)\equiv aDs(e)+b{D}^{2}s(e)=c$$

for the given scalars $\mathit{a}$,$\mathit{b}$, and $\mathit{c}$. You can compute the cubic spine interpolation $\mathit{s}$ as the sum of $${s}_{1}$$ (the cubic spine interpolation of the given data using the default end conditions) and $${s}_{0}$$ (the cubic spine interpolation of zero data using some nontrivial end conditions):

$$s={s}_{1}+\frac{c-\lambda ({s}_{1})}{\lambda ({s}_{0})}{s}_{0}$$

The end conditions you specify in $${s}_{0}$$ do not have to be the final desired end conditions $$\lambda (s)$$.

This example uses the titanium test data, a standard data set used in data fitting. Load the data using the `titanium`

function.

Define the coefficients for $$\lambda (s)$$.

The end condition applies to the leftmost end of the data set.

Now, calculate the cubic spline interpolation of the data set without imposing the end conditions.

To calculate $${s}_{0}$$, use zero data of the same length as `y`

with an additional set of nontrivial end conditions.

The 1-by-2 matrix `conds`

sets the end conditions by specifying the spline derivatives to fix. This example uses end conditions only on the left end of the data, so use `conds`

to fix the first derivative at the left end. At the right end, fix the value of the function itself.

To specify the values to fix the function or its derivatives to, add them as additional values to the data set for fitting - in this case, `yZero`

. The first element specifies the value at the left end, while the last element specifies the value at for the right end.

At the left end, fix the first derivative of the spline to have a value of 1. At the right end, fix the function itself to be 0 (the original value of the final element of `yZero`

). Concatenate these end condition values at the respective ends of `yZero`

and use `csape`

to find the spline that fits the data with these end condition values.

Calculate the fully fitted spline from that data by using the aforementioned expression for $\mathit{s}$. To do this, calculate the values for $${\lambda}_{0}=\lambda ({s}_{0})$$ and $${\lambda}_{1}=\lambda ({s}_{1})$$ using the first and second derivatives of the splines $${s}_{0}$$ and $${s}_{1}$$.

Calculate the derivatives of the first polynomial piece of the spline, as the end conditions apply to the left end of the data only.

Now use $${\lambda}_{1}$$ and $${\lambda}_{0}$$ to calculate the final, fully fitted spline.

Plot the spline to compare the results of the default fit and the end conditions.

The stationary point near the first data point shows that the end conditions are implemented in the fit.