2

I have a data that describes a closed shape, let here assume it is a circle

data1 = Table[{x, Sqrt[4 - x^2]}, {x, -2, 2, 0.01}];
data2 = Table[{-x, -Sqrt[4 - x^2]}, {x, -2, 2, 0.010}];
Data = Join[data2, data1];
ListLinePlot[{Data}, Axes -> False, AspectRatio -> 1, 
 ImageSize -> 300, Frame -> True]

enter image description here

the data of the circle is stored in Data and I would like to make Interpolation so I used

G = Interpolation[Data];

but did not work and this error showed up enter image description here

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
MMA13
  • 4,664
  • 3
  • 15
  • 21
  • 2
    how about bsF=BSplineFunction[Data]; and then ParametricPlot[bsF[t], {t,0,1}]? – kglr Apr 29 '20 at 03:33
  • 2
    If you want all Data points to be hit, set Option: SplineDegree -> 1 , – Akku14 Apr 29 '20 at 07:46
  • Interpolation is trying to evaluate a function, what you have is actually 2 functions (upper and lower half circles). Can you clarify what you are trying to do ? – A.G. Apr 29 '20 at 08:12
  • @A.G., I have a data that describes a closed shape, not necessarily a circle, and I would like to use Interpolation to fill in the gaps in the data. I provided a data of a circle as an example but it can any other closed shape such as ellipse,..etc – MMA13 Apr 29 '20 at 12:23
  • @kglr, that is what I want. Can you make it an answer so I can accept it? – MMA13 Apr 29 '20 at 12:58
  • @kglr, I have another question related to ListPlot and ListLinePlot, may you please have a look? check here https://mathematica.stackexchange.com/questions/220561/listplot-and-listlineplot-don-not-give-the-same-results-regarding-the-color – MMA13 Apr 29 '20 at 13:13
  • 1
    You could interpolate your x and y values separately and then use a parametric plot. – Hugh Apr 29 '20 at 15:54
  • 1
    Following up on @Hugh's proposal, you can use the method in this answer for both plane curves and space curves (under the assumption that the underlying curve is sufficiently smooth). – J. M.'s missing motivation Apr 29 '20 at 22:58

2 Answers2

4

Below I present a way to get an InterpolatingFunction and a comparison of @kglr's and @Akku14's solutions:

Data = Join[Most@data1, data2]; (* Probably should do for BSplineFunction, too *)

gIF = Interpolation[
  MapIndexed[{(#2 - 1.)/(Length@Data - 1), #1} &, Data],
  PeriodicInterpolation -> True
  ]

Below one can see that the InterpolatingFunction and linear BSplineFunction pass through the data points. One can also see that the InterpolatingFunction and regular BSplineFunction are curved instead of polygonal (no surprise). Which to use depends on the application.

bsF = BSplineFunction[Data]                      (* @kglr   *)
bsF1 = BSplineFunction[Data, SplineDegree -> 1]  (* @Akku14 *)

ParametricPlot[{gIF[t], bsF1[t], bsF[t]}, {t, 0, 0.01}, 
 AspectRatio -> 1, PlotRange -> All, Epilog -> {Red, Point@Data}, 
 PlotLegends -> "Expressions"]

ParametricPlot[{gIF[t], bsF1[t], bsF[t]}, {t, 0.245, 0.255}, 
 AspectRatio -> 1, PlotRange -> All, Epilog -> {Red, Point@Data}, 
 PlotLegends -> "Expressions"]

enter image description here

Michael E2
  • 235,386
  • 17
  • 334
  • 747
2

You can use BSplineFunction:

bsF = BSplineFunction[Data];

ParametricPlot[bsF[t], {t, 0, 1}]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896