You can implement custom end conditions using the csape
function. Suppose you want to enforce the following condition at the leftmost endpoint, x(1)
for the given scalars ,, and . You can compute the cubic spine interpolation as the sum of (the cubic spine interpolation of the given data using the default end conditions) and (the cubic spine interpolation of zero data using some nontrivial end conditions):
The end conditions you specify in do not have to be the final desired end conditions .
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 .
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 , 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 . To do this, calculate the values for and using the first and second derivatives of the splines and .
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 and 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.