0

I would like to compute the intersection points between two curves (Xdot and the Poincaré section in the figure below: Plot of the two curves

Xb and Xdot are two numpy arrays of shape (len(Xb),1) computed in another function. I use an anonymous function to get the coordinates and plot the second curve.

To get the intersections, I found different techniques and tried them: np.argwhere(np.diff.np(sign(--)))

def section_Poincare(Xb,Xdot, y_fun= lambda x: 1666*x):
    y_cut = y_fun(Xb)
    idx = np.argwhere(np.diff(np.sign(y_cut-Xdot))) 
    y_points = Xdot[idx]
    idx = Xb[idx]
    return idx, y_points, y_cut

or a tolerance one ( if xdot[i] - poincare_section[i] < 1e-6: ...), but I don't think they work in my case. As you can see, the x-axis has very small values (10^-2) while the y-axis has bigger ones (10^1) hence errors can spread easily.

I also tried doing a linear interpolation but did not get satisfying results:

def section_Poincare(Xb,Xdot, y_fun= lambda x:1666*x):
    
    y_cut = y_fun(Xb)
    idx = np.array([])
    y_points = np.array([])

    for i in range(len(Xb)):
        if 0 <= abs(y_cut[i]-Xdot[i]) and abs(y_cut[i]-Xdot[i]) <= abs(Xdot[i+1]-Xdot[i]) :  #### Curve goes through the section
            a = (Xdot[i+1]-Xdot[i])/(Xb[i+1]-Xb[i]) ###### linear interpolation
            b = Xdot[i+1] - a * Xb[i+1]
            np.append(idx, (y_cut[i+1]-b)/a)
            np.append(y_points, y_cut[i]
    x_cut = np.array(x_cut).reshape(len(x_cut), 1)

    return x_cut,y_cut, y_cut

I do not know what I am doing wrong, and I know it might be a stupid mistake from me but I would really need a bit of your help. Thanks a lot for your time!

Reinderien
  • 11,755
  • 5
  • 49
  • 77
Masch
  • 1
  • 1
    Hi, just to make sure, neither [this](https://stackoverflow.com/questions/28766692/how-to-find-the-intersection-of-two-graphs) nor [this](https://stackoverflow.com/questions/3252194/numpy-and-line-intersections) question answers yours? – My Work Jul 31 '23 at 18:59
  • Does this answer your question? [Numpy and line intersections](https://stackoverflow.com/questions/3252194/numpy-and-line-intersections) – J_H Aug 01 '23 at 03:01
  • Thank your for your answers. I indeed succeeded in finding the intersections using numpy. My problem was the fact that the array for the cut section coordinates was not sorted hence curves were never intersecting. – Masch Aug 01 '23 at 09:54

0 Answers0