# How to generate specific function using BSpline command?

I want to generate an approximation function that fits a curve to points. My goal is to obtain an actual formula. Is this possible with BSplineFunction? Using the following code:

pts = {{1, 1}, {2, 3}, {3, -1}, {4, 1}, {5, 0}};
f = BSplineFunction[pts]


the PiecewiseExpand command does not work. Is there another way to obtain the formula?

• The formula depends on the SplineDegree you ask for. If you ask for a SplineDegree of 1 it could indeed be divided in Piecewise functions, is that what you are looking for? – Öskå Aug 5 '14 at 10:01
• @Öskå Doesn't SplineDegree of 1 just generate lines between the points? – Kaisey Aug 5 '14 at 19:17
• It does indeed, but so does ListLinePlot. I don't think there is a command for what you want, you need to make something out. – Öskå Aug 5 '14 at 19:18
• Do you expect the {{1,0},{5,1}} line shown in Plot[f[x], {x,0,1}] to be here in the Piecewise function? – Öskå Aug 6 '14 at 12:29

It's difficult to answer this question definitively, without more detail on the type of function that you're after. As you refer to the PiecewiseExpand function, I'm rather guessing that you're looking for a single, piecewise linear function that passes through the points. If so, perhaps this works:

affineFormula[{{x1_, y1_}, {x2_, y2_}}, x_] :=
y1 + (y2 - y1) (x - x1)/(x2 - x1) /; x1 != x2;
piecewiseFormula[pts : {{_, _} ..}, x_] := Piecewise[Table[
{affineFormula[pair, x], pair[[1, 1]] <= x < pair[[2, 1]]},
{pair,Partition[pts, 2, 1]}]];


Thus, for your points we have

pts = {{1, 1}, {2, 3}, {3, -1}, {4, 1}, {5, 0}};
pw[x_] = piecewiseFormula[pts, x]


Here's a plot:

Plot[pw[x], {x, 1, 5}]


But again, there are many types of functions that can pass through the points. Here's a simple way to obtain a polynomial, for example.

p[x_] = InterpolatingPolynomial[pts, x]
Plot[p[x], {x, 1, 5}, Epilog -> Point[pts]]
(* Out: 1 + (2 + (-3 + (2 - 7/8 (-4 + x)) (-3 + x)) (-2 + x)) (-1 + x) *)


• Would one not expect f = Interpolation[pts, InterpolationOrder -> 1]; PiecewiseExpand[f] to work? – chris Aug 6 '14 at 14:50
• @chris I think it might be reasonable to implement PiecewiseExpand for InterpolatingFunctions but I don't see any reason to expect it. Interpolation is much older (V2) and lives in the numerical domain. PiecewiseExpand (V5) is primarily algebraic and is simply meant to simplify nested Piecewise expressions. How might PiecewiseExpand work with an InterpolatingFunction generated by NDSolve and containing zillions of sub-intervals? – Mark McClure Aug 6 '14 at 15:07
• well it would return A very large output was generated… :-) – chris Aug 6 '14 at 15:58