0

I would like to have the intersection points of two lines in python using numpy. I wrote a piece of code but I can not complete the code. I have a curve of 1000 points which has been read by numpy and plotted by matplotlib. Also, I plotted the line y=0 and Currently, I want to have the number of intersections that the first curve has with the line y = 0. like the figure below

enter image description here

The code is below:

import matplotlib.pyplot as plt
import numpy as np

#ll = np.linspace(min(x),max(x),1696)


with open('file1.txt', 'r') as f:
    lines = f.readlines()
    x = [float(line.split()[0]) for line in lines]
    y = [float(line.split()[1]) for line in lines]
    a = np.array(y)
    b = np.where (y==0)
print(b)

All favors will be appreciated.

Hossein Amini
  • 35
  • 2
  • 9
  • As per my understanding, this question is not quite straightforward. What you are doing is not basically checking if y coordinates in your files are zero or not while what you want is the intersection of the curve resulting from the points. One way is to find the equation of curve, that’s a machine learning curve fitting problem. Second way you can approximate the solution by assuming a straight line between two consecutive points and see if that line intersect with y=0. – talos1904 Feb 26 '20 at 10:14
  • @talos1904 As I told you, the question is "How to find the number of points that y=0 has with the curve which I do not have the equation of line so it's impossible to use this way. We are dealing with 2 numpy array 1st: current curve and 2nd: the line which we produced. now, the `np.Where` should work but not. Also, another way is doing a math which does not seem quite useful. – Hossein Amini Feb 26 '20 at 10:22
  • @BehzadJamali `b = np.where(a==0)` gets nothing. I mean the result becomes nothing. Not only a==0, even if change to a ==1 or any other value, becomes nothing. – Hossein Amini Feb 26 '20 at 10:39
  • @BehzadJamali **(array([], dtype=int64),)** while I have points equal to zero. – Hossein Amini Feb 26 '20 at 10:40
  • 1
    it depends on the `rtol` value. try to increase it to `0.2` and see if you get any results – Behzad Jamali Feb 26 '20 at 10:42
  • 1
    change `b = np.where(y==0)` to `b = np.isclose(a, b, rtol=0.01)`. Note that you cannot use == to check if two float numbers are equal! – Behzad Jamali Feb 26 '20 at 10:44
  • All thing that are printing is **false** which means there is no point equal to zero while once you plot you will see points like the figure above. I'm totally confused – Hossein Amini Feb 26 '20 at 10:47
  • I dont have your data so can't really tell but this works in my random numbers case. – Behzad Jamali Feb 26 '20 at 10:50
  • @BehzadJamali [link](https://gofile.io/?c=IIc2R8) – Hossein Amini Feb 26 '20 at 10:55

2 Answers2

0

Try this:

import numpy as np

a = np.random.random(20) * 2         # 20 random float numbers between 0 and 2
b = np.ones_like(a, dtype=np.float)  # 20 numbers equal to 1

epsilon = 0.05
solution = np.abs(a-b) < epsilon  # index of items in `a` that are close enough (epsilon) to `b`

print(solution)

alternatively you can use:

solution = np.isclose(a,b, rtol=epsilon)
Behzad Jamali
  • 884
  • 2
  • 10
  • 23
0

What you are trying to do is simply a zero crossing problem. Brute force approach that guarantees zero crossing count. Technically can be used to check for any line as well. simply need to change the conditional statements a little bit. Making the assumption that being at 0 does not count as zero crossing. Note that the points need to be in order for this to work.

y = [0,1,2,3,2,1,0,-1,0,0,1,-2,5,-6,0]
counter = 0
last_non_zero = None #buffer for handling zeroes
for i in range(len(y)-1):
    if y[i] == 0 and i == 0:
        pass
        #First item we ignore for zero crossing if start at 0
    elif y[i] == 0 and i!=0:
        if last_non_zero is not None and last_non_zero*y[i+1]<0
            counter += 1
            #handles 0 values 
    else:
        if y[i] * y[i+1] <0:
            counter +=1 #Condition for last value unnecessary since n*0 = 0 which is not <0
    if y[i] != 0:
        last_non_zero = y[i] #replace buffer

Uses logic of ++ = +, --=+, +-=-, -+=- where last two is zero crossing.

Jason Chia
  • 1,144
  • 1
  • 5
  • 18