2
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – Öskå Aug 5 '14 at 10:01
  • $\begingroup$ @Öskå Doesn't SplineDegree of 1 just generate lines between the points? $\endgroup$ – Kaisey Aug 5 '14 at 19:17
  • $\begingroup$ 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. $\endgroup$ – Öskå Aug 5 '14 at 19:18
  • $\begingroup$ Do you expect the {{1,0},{5,1}} line shown in Plot[f[x], {x,0,1}] to be here in the Piecewise function? $\endgroup$ – Öskå Aug 6 '14 at 12:29
4
$\begingroup$

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]

enter image description here

Here's a plot:

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

Mathematica graphics

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

Mathematica graphics

$\endgroup$
  • $\begingroup$ Would one not expect f = Interpolation[pts, InterpolationOrder -> 1]; PiecewiseExpand[f] to work? $\endgroup$ – chris Aug 6 '14 at 14:50
  • $\begingroup$ @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? $\endgroup$ – Mark McClure Aug 6 '14 at 15:07
  • $\begingroup$ well it would return A very large output was generated… :-) $\endgroup$ – chris Aug 6 '14 at 15:58

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.