# Tag Info

## Hot answers tagged splines

28

I'm not sure if this addesses all of the issues you are having but here is an implementation I put together some time ago that allows us to use LinearModelFit and BSplineBasis to do spline regression. The benefit of this approach is that all of the properties of FittedModel are immediately available to us. This allows for checking for fit, residual ...

20

You can use MeshFunctions to do the trick: g = BSplineFunction[{RandomReal[1, 20], RandomReal[1, 20]}\[Transpose]]; dg = g'; ParametricPlot[ g[t], {t, 0, 1}, MeshFunctions -> Function[{x, y, t}, dg[t].{0, 1}], Mesh -> {{0}}, MeshStyle -> Directive[AbsolutePointSize[5], Red] ] Here the MeshFunctions specifies the value of dg[t]...

18

If my answer for the 2D case lacks detail, it's because TypesetMakeBoxes is an internal, undocumented function. That makes it hard to say anything authoritative about how it works. Essentially though, we are defining the custom primitive in such a way that the definition only applies during conversion of a graphics expression to boxes. Here's a version of ...

18

General When given an array of integrands NIntegrate is run separately over each array element. That is not necessary though, the core NIntegrate integration strategies can work with any integrands as long as the error estimates are real numbers. The motivation for implementing ArrayOfFunctionsRule is to provide a significant speed-up for integrands that ...

16

The natural way to go is BSplineFunction[]. The problem is that it needs a rectangular array of data as input and you collected a different number of points for each z plane. So what we will do is to get an interpolating function for each z == const plane and generate an equal number of points at each plane. To be somewhat more clever, we could generate ...

15

For a simple image like that (high-contrast object in front of a monochrome background), calling EdgeDetect is actually enough to find the edges: img = Import["http://i.stack.imgur.com/UhLrU.png"]; edges = EdgeDetect[img]; Then we can find the leftmost and rightmost points on every line to get an array of radii: radii = Map[Max[#[[All, 1]]] - Min[#[[...

15

A simple line strip should be sufficient for most purposes and there you can use VertexColors as usual: pts = {{0, 0}, {1, 1}, {2, 0}}; Graphics[{Thick, Line[BezierFunction[pts] /@ #, VertexColors -> (Blend[{Red, Green}, #] & /@ #)] &[Range[0, 1, .01]], Line[{{0, 0}, {2, 0}}, VertexColors -> {Red, Green}]} ]

15

The OP linked to an answer of mine for interpolating over general point sets; for constructing a single interpolating function, a slight modification of my procedure is needed. (In particular, you don't need centripetal or chord-length parametrization in this case.) Let's start with some data: data = {{0, 0}, {1/10, 3/10}, {1/2, 3/5}, {1, -1/5}, {2, 3}, {3,...

14

I've used the method I'm about to show in this answer, but I suppose having it explicitly answer an interpolation question would be convenient. Starting with your points, testData = {{10, 10}, {10, 20}, {10, 25}, {10, 27}, {10, 28}, {9, 26}, {8, 25}, {5, 20}, {3, 1}}; we use Lee's centripetal parametrization scheme to generate corresponding ...

14

I have long been looking for a good implementation of cubic spline smoothing with adjustable roughness penalty parameter for Mathematica. Your module gave me enough hints to understand how to make this work in Mathematica, so I basically made a cubic spline smoothing code from your code with minor adjustments (about knots, a little bit about performance) ...

14

Fortunately there is a solution but it appears to be undocumented and takes a bit of guess work. The magic is: FilledCurveBoxOptions -> {Method -> {"SplinePoints" -> (* integer value *)}} This may be set globally or for a Notebook: SetOptions[InputNotebook[], FilledCurveBoxOptions -> {Method -> {"SplinePoints" -> 30}}] (Use $FrontEnd ... 12 Here is my implementation of quadric spline arbitrary precision interpolation which is defined exactly as in Interpolation with options Method->"Spline", InterpolationOrder->2. Theoretical background Quadric spline interpolation for n datapoints is defined as a Piecewise function which consists of n-2 parabolas which are spliced together in the ... 12 I appreciate kguler's elegant solution, but it doesn't join the points. To be more precise, it joins only every third point because Bezier line additionally takes 2 anchor points for each point. There are different methods to obtain these points. The simplest one is the following (pictures taken here) In Mathematica it looks like the following code (for an ... 12 f[n_, j_, u_] := PiecewiseExpand[BernsteinBasis[n, j, u]] cv[pt_, n_, u_] := (f[n, #, u] & /@ Range[0, n]).pt Test: pts = {{0, 0}, {2, 4}, {4, 5}, {6, 0}}; ParametricPlot[cv[pts, Length@pts - 1, v], {v, 0, 1}, Epilog -> {Red, PointSize[0.02], Point[pts], Green, Line[pts]}, PlotRange -> {0, 6}] Or DynamicModule[{p = {{0, 0}, {2, 4}, {4, 5}, {... 12 Here is a Fourier Basis approach: ClearAll[FourierBasis2D]; FourierBasis2D[{numx_, numy_}, {λx_, λy_}, x_, y_] := N[With[{ωn = 2 π/λx, ωm = 2 π/λy}, Flatten[ {1}~Join~ Table[ {Cos[ n ωn x] Cos[m ωm y], Cos[ n ωn x] Sin[ m ωm y], Sin[ n ωn x] Cos[m ωm y], Sin[ n ωn x] Sin[ m ωm y]}, {n, numx}, {m, ... 12 Update Since version 12, this functionality in integrated in Mathematica via the Option FitRegularization Following on @Ajasja's answer in the spirit of this answer one can in fact provide controlled smoothing va an explicit Tichonov like penalty as follows: ff = Function[{x, y}, basis // Evaluate]; a = ff @@ # & /@ (Most /@ data); so that fit[x_,... 12 This was solved with help from Shutao Tang, J.M., Sander Huisman and Eric Rimbey. Why do BezierFunction[pt] and BezierCurve[pt] not agree? Because BezierCurve uses SplineDegree -> 3 by default and BezierFunction always uses degree Length[pt] - 1 (not settable). Why does DiscretizeGraphics give a bad result? Because it appears to use (the equivalent of) ... 12 So DiscretizeGraphics seems to always miss the first or last point of a BSplineCurve (it seems to do it with a BezierCurve as well). Here's the simplest example of this, pts = {{.5, 0}, {1, 0}, {1, 1}, {.5, 1}, {0, 1}, {0, 0}, {.5, 0}}; GraphicsRow[{Graphics@#, DiscretizeGraphics@#} &@BSplineCurve[pts], ImageSize -> 600] Why does it do this? Not ... 12 This might help you get an idea: n = 4; BlockRandom[SeedRandom[42, Method -> "Legacy"]; (* for reproducibility *) cpts = Table[{i, j, RandomReal[{-1, 1}]}, {i, n + 1}, {j, n + 1}]]; GraphicsRow[{ParametricPlot3D[BezierFunction[cpts][u, v], {u, 0, 1}, {v, 0, 1}, Evaluated -> True], (* built-in function *) ... 12 Graphics[{ Yellow, FilledCurve[{BSplineCurve /@ {pts1, pts4, Reverse@pts2, pts3}}], Green, Line[Join[pts1, pts4, Reverse@pts2, pts3]], Red, Point[Join[pts1, pts2, pts3, pts4]] }] 12 Extracting a spline from the image is easiest with ImageMesh. I've used the midpoints of the lines on the mesh boundary for the BSplineCurve control points. img = Import["https://i.stack.imgur.com/Yy0uN.png"]; mesh = ImageMesh@img; midpoints = Midpoint /@ MeshPrimitives[mesh, 1]; Graphics[{Thick, Darker[Red], BSplineCurve[midpoints, SplineClosed -> ... 11 Yes, there seems to be a bug in there. You still may use BSplineFunctionif you are OK with numerical results: << NumericalCalculus d = 2; kV = {0, 0, 0, 1, 1, 1}; P = {{0, 0}, {0, 1}, {1, 1}}; W = {1, 1/Sqrt[2], 1}; x[t_] := BSplineFunction[P[[All, 1]], SplineWeights -> W, SplineDegree -> d, SplineKnots -> kV][t] /; 0 < t < 1 x[r_] :... 11 Here is a (simplified) implementation of Reinsch's smoothing spline, which is effectively equivalent to csaps() in MATLAB's Curve Fitting Toolbox. Fancier methods have come along since then (e.g. Wahba's cross-validation splines), but this old workhorse has still proved serviceable: SmoothingSplineFunction[dat_?MatrixQ, p : (_?NumericQ | Automatic) : ... 11 Major update, version 2.0 What changed: Is a different, more clever way of solving the problem. Overcomes most of the issues of previous versions. Problem summary: I will rephrase the problem in simplest terms. There exests a named curve called limacon (french, pronounced [ˈlɪməsɒn], means snail), described by a simple equation$r=a+b\cos{\theta}\$ in ...

11

The following works for your curve: points = {{1, 4}, {.5, 6}, {5, 4}, {3, 12}, {11, 14}, {8, 4}, {12, 3}, {11, 9}, {15, 10}, {17, 8}}; deg = 3; pointsCLOSE1 = Join[points, points]; n = Length@pointsCLOSE1; knotsCLOSE1 = Range[0, 1, 1/(n + 1)]; ParametricPlot[deBoor[pointsCLOSE1, {deg, knotsCLOSE1}, t], {t, deg/(n + 1), 1}, Axes -&...

11

Update Fully compiling the code to C makes it as fast as the built-in: cBernstein = Compile @@ (Hold[{n, {j, _Real, 1}, u}, Table[expr u^i (1 - u)^(n - i), {i, j}], CompilationTarget -> C] /. expr -> FunctionExpand[Binomial[n, i]]) BezierSurface4 = With[{cBernstein = cBernstein}, With[{AllBasis = Function[{deg, u0}, cBernstein[deg, ...

11

Here is my approach: pts = {{0, 0}, {2, 1}, {4, 3}, {6, 1}}; paras = FoldList[Plus, 0, Normalize[(Norm /@ Differences[pts]), Total]] // N mat = Outer[BernsteinBasis[3, #1, #2] &, Range[0, 3], paras] // Transpose; ctrlpts = LinearSolve[mat, pts] (* {{0., 0.}, {2.71043, -0.262717}, {3.94236, 6.32778}, {6., 1.}} *) Graphics[{BezierCurve[ctrlpts], PointSize[...

11

BezierCurve normally gives a composition of local 4point-Bezierfunctions. You get equal curves by setting SplineDegree->1+Length[pts] Show[Graphics[{Red, Point[pts], Green, Line[pts]}, Axes -> True], ParametricPlot[f[t], {t, 0, 1}], Graphics[{Blue, Dashed,BezierCurve[pts, SplineDegree -> 1 + Length[pts]]}]]

10

pts = {{0, 0}, {1, 1}, {2, 0}}; f = BezierFunction[pts]; Show[ ParametricPlot[f[t], {t, 0, 2}, ColorFunction -> Function[{x}, RGBColor[1 - x, x, 0]]], Graphics[{Thick,Line[{{0, 0}, {2, 0}}, VertexColors -> {Red, Green}]}], Axes -> None ]

Only top voted, non community-wiki answers of a minimum length are eligible