5
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ aside from your question, a bspline is not a good way to approximate data. What are you trying to accomplish? $\endgroup$ – george2079 Dec 17 '15 at 15:34
  • $\begingroup$ 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. $\endgroup$ – user126636 Dec 17 '15 at 15:44
  • $\begingroup$ If anyone knows how to distill the piecewise polynomial out of a BSplineFunction that would be interesting to see. $\endgroup$ – george2079 Dec 17 '15 at 20:53
  • $\begingroup$ Why not use Interpolation[] instead? $\endgroup$ – J. M. will be back soon Dec 18 '15 at 2:10
3
$\begingroup$

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] := #[[2]]/#[[1]] &@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]

enter image description here

obtaining the extrema..

ext = Table[ t /. FindRoot[dydx[t], {t, s}] , {s, {.2, .5, .9}}];
(* or use FindRoot[ g[t][[2]] , {t, .. }] *)
Show[Graphics[{ BSplineCurve[pts] , Arrow /@ tangents , Red, 
 PointSize[.02], Point[f /@ ext ] , Blue , Point /@ pts} ],  Frame -> True]

enter image description here

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. )

$\endgroup$
  • $\begingroup$ 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. >> $\endgroup$ – user126636 Dec 17 '15 at 18:00
  • $\begingroup$ Add the ?NumericQ pattern to dydx (see edit). (be sure to do Clear[dydx]; first) $\endgroup$ – george2079 Dec 17 '15 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.