Tag Info

Hot answers tagged

24

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


15

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


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


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

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

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

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


6

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


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


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

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


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

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


3

You can use Thread to apply a function to a list of arguments: Thread[bsplinefunc1[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.]} Now you can use Map to use the ...


3

Given the discussion, another approach you might want to consider would be to explicitly parameterize a function -- -- this way you get to control very precisely the form of the function and you can choose it to have a nice shape. As an added bonus, you can perhaps even find an analytical inverse, which greatly simplifies that part of the problem. For ...


3

If you absolutely must use B-splines, you can explicitly build the component functions that make up the B-spline, using the usual definitions: pts = {{0, 0}, {0.2, 0.7}, {1, 1}}; n = Min[Length[pts] - 1, 3]; (* B-spline degree *) m = Length[pts]; (* clamped uniform knots for B-spline *) knots = {ConstantArray[0, n + 1], Range[m - n - 1]/(m - n), ...


3

BSplineFunction[{{0., 1.}, {0., 1.}}, "<>"] is a map from parametric space to "plotting" space, i.e. $\mathrm{BSplineFunction}:(u,v)\mapsto (x,y,z)$, so instead of Plot3D[surfFn[x/7, y/5][[3]], {x, 0, 7}, {y, 0, 5}], it should be written as ParametricPlot3D[surfFn[u, v], {u, 0, 1}, {v, 0, 1}]. cpts = Table[{x, y, RandomReal[{0, 2}]}, {x, 0, 7}, {y, 0, ...


3

data = {{9.93114`, 379022.8`}, {11.71875`, 321705.7`}, {13.46983`, 280830.8`}, {15.625`, 243949.8`}, {18.60119`, 206915}, {21.70139`, 179663.2`}, {24.93351`, 158942.4`}, {29.29688`, 138396.2`}, {33.48214`, 123873.8`}}; spline = BSplineFunction[data, SplineDegree -> 3]; Clear[tstfc]; You need to restrict the definition of tstfc to numeric arguments. ...


2

(1) I get a reasonable result interpolating separately. (2) I don't think your derivatives are doing what you expect. x1 = Interpolation[table[[All, 1]]]; y1 = Interpolation[table[[All, 2]]]; Show[ListPlot[table], ParametricPlot[{x1[t], y1[t]}, {t, 1, 21}], AspectRatio -> 1] Here are those derivatives. Plot[{x1'[t], y1'[t]}, {t, 0, 21}, ...


2

When manipulating B-splines in this manner, it is often convenient to fall back on the definitions. Luckily, since Mathematica supplies the function BSplineBasis[], using the definitions are easy: pts = {{0, 0}, {1, 1}, {2, -1}, {3, 0}, {4, -2}, {5, 1}}; n = 3; (* B-spline degree *) m = Length[pts]; (* clamped uniform knots for B-spline *) knots = ...



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