I would like to compute the intersection points between two curves (Xdot and the Poincaré section in the figure below:
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!