1
$\begingroup$

Let's create some points corresponding to a simple closed curve

C0 = ContourPlot[x^2/4 + y^2/9 == 1, {x, -5, 5}, {y, -5, 5}, PlotPoints -> 100];
data = C0[[1, 1]];
S0 = ListPlot[data, PlotStyle -> {Blue, PointSize[0.005]}]

enter image description here

Now I would like to find the intersection points with the two axes. In this example the solutions should be $x = \pm 2$ and $y = \pm 3$.

IMPORTANT NOTE: The real data file corresponds to a closed curve with unknown analytical equation, so in the suggested solution you should not take into account the equation of the ellipse, only data is known.

Many thanks in advance.

$\endgroup$
5
  • $\begingroup$ What version are you on? C0[[1, 1]] works for extracting points in old versions, but not in the new ones. $\endgroup$ Commented Nov 21, 2015 at 18:00
  • $\begingroup$ @J.M. I have v9.0. $\endgroup$
    – Vaggelis_Z
    Commented Nov 21, 2015 at 18:02
  • $\begingroup$ @J.M. BTW, if I want to extract points in v10 what should I use? $\endgroup$
    – Vaggelis_Z
    Commented Nov 21, 2015 at 18:09
  • 2
    $\begingroup$ @J.M. Actually C0[[1,1]] works just fine in v10.3: it extracts the actual coordinates of the points from the GraphicsComplex generated by ContourPlot. $\endgroup$
    – MarcoB
    Commented Nov 21, 2015 at 18:50
  • $\begingroup$ @Marco, ah, right; nevertheless, it's not really the best way of extracting those points. $\endgroup$ Commented Nov 21, 2015 at 19:05

1 Answer 1

5
$\begingroup$

Get rid of duplicate x values we can feed to an interpolation function

domain = Select[Tally[data[[All, 1]]], #[[2]] == 1 &][[All, 1]];

Create Interpolation functions and evaluate at 0 These are for the top and bottom halves of the fn

Interpolation[Select[data, #[[2]] < 0 && MemberQ[domain, #[[1]]] &]][0]
Interpolation[Select[data, #[[2]] > 0 && MemberQ[domain, #[[1]]] &]][0]

-2.99994
2.99994

Max and Min on the domain can approximate the x intercepts:

Max[domain]
Min[domain]

1.99985
-1.99985

EDIT

Assuming your ellipse isn't 0-centered you will need exact x-intercepts

This requires a second domain and second set of interpolating functions, necessitating a transposition and reevaluation.

data2 = Reverse /@ data;
domain2 = Select[Tally[data2[[All, 1]]], #[[2]] == 1 &][[All, 1]];


Interpolation[Select[data2, #[[2]] < 0 && MemberQ[domain2, #[[1]]] &]][0]
Interpolation[Select[data2, #[[2]] > 0 && MemberQ[domain2, #[[1]]] &]][0]

-1.99996
1.99996
$\endgroup$
3
  • $\begingroup$ It works like a charm! However I think that the second Max and Min of the domain approximate the x intercepts, not y, right? $\endgroup$
    – Vaggelis_Z
    Commented Nov 21, 2015 at 19:16
  • $\begingroup$ @Vaggelis_Z correct! made the edit in the answer $\endgroup$ Commented Nov 21, 2015 at 19:18
  • $\begingroup$ @Vaggelis_Z please see the edit I made to actually solve for the x-intercepts, which will allow for your data to not be centered on 0 $\endgroup$ Commented Nov 23, 2015 at 1:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.