I am trying to compute the intersection points between a line and a closed curve (stored in a file)
I tried to adapt this
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
from scipy.optimize import bisect
import numpy as np
import csv
from scipy.interpolate import interp1d
from scipy.optimize import bisect
#reading curve from file
r = ...
z = ...
# vertical line
x = 1.885, 1.885
y = [-3,3]
z_ves = interp1d(r_ves, z_ves, fill_value="extrapolate")
plt.figure(num=None, figsize=(10, 6), dpi=100, facecolor='w', edgecolor='k')
plt.plot(r_ves, z_ves(r_ves))
plt.axvline(x=1.885,ymin=-3,ymax=3)
# #use interp1d to get interpolated points
y = interp1d(x, y, fill_value="extrapolate")
# stress = interp1d(strain, stress)
# #set starting points
x1 = max(x[0], r_ves[0])
x2 = min(x[-1], r_ves[-1])
max_err = .01
# #create function
f = lambda x : z_ves(x) - y(x)
#find x1 where f(x1) = 0
x1 = bisect(f, x1, x2, xtol = .001)
y1 = z_ves(x1)
#
plt.figure(num=None, figsize=(10, 6), dpi=100, facecolor='w', edgecolor='k')
plt.plot(r_ves, z_ves(r_ves))
plt.plot(x, y(x))
plt.scatter(x1, y1)
plt.show()
I get a Runtime Warning as I think the line is vertical.
RuntimeWarning: invalid value encountered in multiply y_new = slope*(x_new - x_lo)[:, None] + y_lo
I know there are two intersections (see picture)
how do I get the intersections?