I have two curves, one is a line, for example y = x/4 and another one is the set of points which I connect with line segments (for example : x = [1, 2.5, 3.4, 5.8, 6]
, y = [2, 4, 5.8, 4.3, 4]
which forms 5 points in 2D plane that forms a piecewise linear curve), and I should find the intersecting point between this two curves. I should first form this piecewise linear curve and then find the intersection point. Fortunately, I found that I could use numpy.polyfit
to find the polynomial coefficients of every line segment as below:
import numpy as np
import matplotlib.pyplot as plt
x = [0, 1, 2, 4, 6] # in my primary problem i dont have just 5 points and i have approximately 200 points !
y = [0, 0, 3, -1, 2]
x = np.array(x)
y = np.array(y)
a = [np.polyfit(x[i:(i+2)], y[i:(i+2)], 1) for i in range(len(x)-1)]
plt.plot(x, y, 'o')
plt.show()
But now I am really confused about how to use these coefficients to find the intersecting point between the two graphs!? (in my primary problem i don't have just 5 points and i have approximately 200 points !) One idea to solve this problem is to use ((solve)) command to check the intersection of the line and line segments but this is very time consuming and this command doesn't work with line ((segment)).