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


5

As Ulises Cervantes Pimentel uncovered in this Wolfram Community thread, Plot and ParametricPlot/ParametricPlot3D for curves can take as MeshFunctions the values "ArcLength" and "CurveLength". This functionality provides straightforward solution for the plots with AspectRatio -> Automatic: mpoints = Table[t, {t, 0, 1, 0.04}]; ...


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

Is this the behaviour you need? Solution Unprotect[Power]; Power[0, -1] = 1 Protect[Power] Examples 0/0 0 Explanation Revert to normal Unprotect[Power]; ClearAll[Power]; Protect[Power];


4

Here is one way to deal with repeated entries in U. One can define a function to compute the coefficient, using one rule when $u_i = u_j$ and the general formula otherwise. One might put extra conditions on the patterns in coeff below, but if the function is called only within NBSpline, then one might assume the conditions are met. ClearAll[coeff]; ...


4

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


4

Fixed the bug in original code Thanks for @Michael E2's good suggestion U_?(VectorQ[#, NumericQ] && OrderedQ[#] &) and smart solution to deal with $u_i=u_{i+1}$ $\frac{u-u_i}{u_{i+p}-u_i} and \frac{u_{i+p+1}-u}{u_{i+p+1}-u_{i+1}}$ coeff[u_, i_, j_, U_] /; U[[i]] == U[[j]] := 0; coeff[u_, i_, j_, U_] := (u - U[[i]])/(U[[j]] - U[[i]]) ...


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



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