Tag Info

Hot answers tagged

25

So, what is the best way to join points in Mathematica? There is no one "best way" (not only in Mathematica, but in general); an interpolation scheme that behaves nicely for data set A might be a crapshoot when applied to data set B. It depends on the configuration of your points, and impositions you have on the interpolant (e.g. $C^1$/$C^2$ continuity, ...


16

May not turn out to be a very general method but here I will adapt the Interpolation function of MMA in a way so that smoother result can be obtained for your specific data set. Interpolation vs. ListLinePlot First lets see how the default Interpolation behaves compared to the interpolating function used when we call ListLinePlot with same interpolation ...


15

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


11

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


11

The built-in functionality can do this directly: {xs, ys} = Transpose[points]; xinterp = Interpolation[xs, Method -> "Spline"]; yinterp = Interpolation[ys, Method -> "Spline"]; ParametricPlot[{xinterp[t], yinterp[t]}, {t, 1, Length@points}, Epilog -> Point /@ points] Without the "Spline" method, the interpolating functions are not always ...


10

Reason for incompatibility Yes, the doc shouldn't mention that it is fully compatible (that's an oversight...). BSplineFunction and BSplineCurve are fully compatible as far as I remember, but not BezierCurve and BezierFunction. The reason is that what BezierCurve is doing when it has more than d+1 control points for SplineDegree->d case is something ...


9

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


8

BSplineCurve is based on BSplineFunction. But BSplineFunction is analytic expression - so you do not need to interpolate it - you can use it as a (parametric) function: g = BSplineFunction[points]; ParametricPlot[g[t], {t, 0, 1}] If you still need points - this will work with any step: bspts=Table[g[t], {t, 0, 1, .1}]


8

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


8

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


7

Here's a way with Interpolation. It passes through the points as the OP desires. It specifies the velocity (derivative) through the points by bisecting the exterior angle of the polygon; if the path turns back on itself, it specifies a velocity at a right angle (to the "left" of the direction of approach to the vertex) to the edge. The weights multiply the ...


7

A more interesting example with multiple extrema.. g = BSplineFunction[{{1, 2}, {2, 4}, {3, -1}, {4, 2}, {5, 0}, {6, 1}}]; gp = g'; gpy[t_?NumericQ] := gp[t][[2]]; This is utilising Plot to generate the curve and look for zero crossings, which we then pass as starting points to FindRoot loc = Flatten[ t /. # & /@ FindRoot[gpy[t] , ...


6

The warning is probably caused by premature evaluation (no pun intended). Because of the symbolic parameter t, s[t] evaluates to BSplineFunction[{{0.,1.}},<>][t] instead of a list of the form {x,y}, and only evaluates to numeric values when t assumes numeric values, too. The normal solution to this is to postpone the access of the y-variable [[2]] to ...


6

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


5

The following is a shameful plug of J. M. 's answer you already linked to show how to get the parametrization by using BSplineBasis[] with arbitrary precision and packed into a function: splineInterp[data_, order_, prec_] := Module[{parametrizeCurve, tvals, bas, ctrlpts, knots}, parametrizeCurve[pts_ /; MatrixQ[pts, NumericQ], a : (_?NumericQ) : 1/2] := ...


5

This isn't perfect by any means, but can be of some use: f1 = BSplineFunction[Table[{x, Sin@x, 0}, {x, 0, 6 Pi, .1}]]; f2 = BSplineFunction[Table[{x, Cos@x, 0}, {x, 0, 6 Pi, .1}]]; g[u_, v_] := Norm[f1[u] - f2[v]] i = ColorNegate@Binarize@ContourPlot[g[u,v], {u, 0, 1}, {v, 0, 1}, PlotRange->{0, .5}, Frame-> None]; mc = MorphologicalComponents@i; areas ...


5

Taking your data in account from link you provided: data={{......}}; Find the model: model = Fit[data, x^# & /@ Range[0, 10], x] 20.2513 + 43.3389 x - 0.208411 x^2 + 0.193888 x^3 - 0.0341689 x^4 + 0.00281455 x^5 - 0.000131003 x^6 + 3.64629*10^-6 x^7 - 6.01724*10^-8 x^8 + 5.43205*10^-10 x^9 - 2.06702*10^-12 x^10 Verify it is more or less ...


5

Just to show that this question is a duplicate of the link given in its body: By using ssch's answer one can get the coordinates of the shape: img = Import["http://i.stack.imgur.com/UhLrU.png"]; img = ImageTake[img, All, {1, ImageDimensions[img][[2]]/2}]; img = ColorNegate[GradientFilter[img, 4]]; {width, height} = ImageDimensions[img]; width = width - 3; ...


5

SeedRandom[4]; pts = Table[{x, y, 0.25 + UnitStep[30 - Abs[x] - 0.001] UnitStep[50 - Abs[y] - 0.001] (0.25/RandomReal[{0.1, 2}]^2)}, {x, -30, 30, 5}, {y, -50, 50, 5}]; ParametricPlot3D[ BSplineFunction[pts, SplineDegree -> 2][u, v], {u, 0, 1}, {v, 0, 1}, PlotRange -> All, Boxed -> False, Axes -> False, Mesh -> None, ...


5

As an alternative one can use J.M.'s nice implementation of a spline with centripetal parametrization which gives (IMO) "natural" and undisturbed curve. @belisarius has packed J.M.'s code into a function in this answer (which would be much more suitable here than in that thread because despite large number of upvotes it does not answer the original ...


4

Inverse of your function sampled and interpolated: g = InverseFunction@Interpolation[f /@ Range[0, 1, .1]] (* gives x for y = .6 *) g[.6] 0.35 Using FindRoot: FindRoot[second[f[t]] == .6, {t, .5}] {t -> 0.5} f[.5] {0.35, 0.6} Delaying evaluation: second[r_?(VectorQ[#, NumericQ] &)] := r[[2]] More general delay: ...


4

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_] ...


4

functions={bsplinefunc1,bsplinefunc2,bsplinefunc3}; Outer[#1[#2] &, functions, Range[0, 1, .1]] {{bsplinefunc1[0.], bsplinefunc1[0.1], bsplinefunc1[0.2], bsplinefunc1[0.3], bsplinefunc1[0.4], bsplinefunc1[0.5], bsplinefunc1[0.6], bsplinefunc1[0.7], bsplinefunc1[0.8], bsplinefunc1[0.9], bsplinefunc1[1.]}, {bsplinefunc2[0.], ...


4

llp = ListLinePlot[points, ImageSize -> 500]; llp2 = llp /. Line[x___] :> BSplineCurve[x, SplineDegree -> 2]; Row[{llp, llp2}] where points = {{-0.137445, -0.0103507}, {-0.0452845, 0.0343154}, {0.30498, -0.0118266}, {-0.0224633, 0.0197979}, {-0.168469, -0.0197066}, {0.0973217, 0.0478612}, {-0.0441388, 0.0360607}, ...


4

An alternative way to force Interpolation to construct a differentiable interpolation is to specify derivatives at the interior points. To get a good curvature, I used 70% of the vector (difference) between the points adjacent to a given point. xyfn = Interpolation @ Thread @ {List /@ Range @ Length @ points, points, Join[{Automatic}, 0.7 ...


3

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


3

The data points you provided to BSplineFunction act as spline control points. Generally, splines do not go through them. The effect you want to achieve can be gotten using Interpolation: First plotting your function with a prettier aspect ratio: ParametricPlot[fFit[n], {n, 0, Length[testData] - 1}, Epilog -> Point[testData], AspectRatio -> ...


3

One approach is to turn your expression into functions. I give a few extra variations just to show what can be done and which you might find useful to learn. Here's the whole thing as a function (to be used, for example, in your Plot3D): Bfn = Evaluate[B /. {x -> #1, y -> #2}] &; Bfn[x, y] == B (* True *) Component functions can be ...


3

You can perform a replace: B[[2,3]]/.{x->-1,y->0} If you have a table of x and y values (call it xyvals) for given matrix elements then you could do: Table[B[[m,n]]/.{x->xyvals[[m,n,1]],y->xyvals[[m,n,2]]},{m,Length[B]},{n,Length[B[[1]]]}]



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