# How to use BSplineFunction for calculating derivative

I have a set of data like the following

data = {{x1,y1},{x2,y2},{x3,y3},......}


Now I use

b = BSplineFunction[data, SplineDegree -> 5];


Now I don't know how to extract the interpolating function from b to calculate it's derivative. Thanks in advance.

• aside from your question, a bspline is not a good way to approximate data. What are you trying to accomplish? – george2079 Dec 17 '15 at 15:34
• I want to find the maximum of the function represented by "data". I have tried spline, but it gives a fluctuating plot near the maximum. So it will be hard to find position of the maximum by using Bisection method. – user126636 Dec 17 '15 at 15:44
• If anyone knows how to distill the piecewise polynomial out of a BSplineFunction that would be interesting to see. – george2079 Dec 17 '15 at 20:53
• Why not use Interpolation[] instead? – J. M.'s discontentment Dec 18 '15 at 2:10

You can simply take the derivative, but note you are differentiating w/ respect to the parameter:

pts = {{1, 1}, {2, 3}, {3, -1}, {4, 1}, {5, 0}};
f = BSplineFunction[pts];
g[t_] = D[f[t], t]  (* g is a new BSplineFunction *)


use chain rule to get dydx:

dydx[t_?NumericQ] := #[]/#[] &@g[t];
tangents = Table[{f[t], f[t] + .2 {1, dydx[t]}}, {t, 0, 1, .1}];
ParametricPlot[f[t], {t, 0, 1} ,
Epilog -> Arrow /@ tangents, AspectRatio -> 1]


obtaining the extrema..

ext = Table[ t /. FindRoot[dydx[t], {t, s}] , {s, {.2, .5, .9}}];
(* or use FindRoot[ g[t][] , {t, .. }] *)
Show[Graphics[{ BSplineCurve[pts] , Arrow /@ tangents , Red,
PointSize[.02], Point[f /@ ext ] , Blue , Point /@ pts} ],  Frame -> True] note that a bspline doesn't pass through its control points (in blue) so this is not a good way to approximate data.

( note this works fine w/ SplineDegree->5, but you need at least 6 points. )

• when i run your code, it gives me the following errors: Part 2 of BSplineFunction[{{0.,1.}},<>][t] does not exist. >> , The expression #2 cannot be used as a part specification. >> – user126636 Dec 17 '15 at 18:00
• Add the ?NumericQ pattern to dydx (see edit). (be sure to do Clear[dydx]; first) – george2079 Dec 17 '15 at 19:29