find intersections in two graphs in python

Asked

Viewed 99 times

1

I have a very simple but functional code. It obeys two mathematical functions, testing different values of x and returning the coordinates of the point if the result of the two functions is the same (y):


def funcs():
    for x in range(-100, 100):
        funcA = (x**2) + x - 2 
        funcB = 6 - x
        if funcA == funcB:
            print("###intersecção enctontrada###") 
            print(f'({x},{funcA})')
        else: 
            pass


funcs()

output:

###intersecção enctontrada###
(-4,10)
###intersecção enctontrada###
(2,4) 

But I have a big problem, it only works when the intersection meets when the value of x is a number whole, when the intersection occurs when the value is broken, it shows nothing.

Python supports up to 16 decimal places, and making a range of all decimal places is completely unfeasible. So I thought maybe it would be possible to make Python stop calculating the decimals after a certain number of decimals, because I believe that if I just round the value it won’t stop Python from counting the other.

And I need a practical solution to solve this problem and return all intersections regardless of whether they are whole or not.

Somebody help me, please, thank you.

2 answers

2


From mathematics, we know that the intersections of two curves can be defined by equalizing the generating functions. That is, if you have two functions, A(x) and B(x), then the intersection occurs at the values of x such that A(x) = B(x). To find the values of x, just solve the equation.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Equaling the two:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Solving this second-degree equation, you get:

inserir a descrição da imagem aqui

That is, you will find the values of x=2 and x=-4 that solve the equation.

Dynamically, you can use the library sympy for that reason.

import sympy

x = sympy.symbols('x')

A = x**2 + x - 2
B = 6 - x

equation = sympy.Eq(A, B)

print( sympy.solveset(equation) )
# FiniteSet(-4, 2)

Note that Python solved the equation for you and returned a finite set of solutions containing -4 and 2. If you want the pair (x, f(x)) as output, just do:

solutions = sympy.solveset(equation)

for s in solutions:
  print(s, A.subs(x, s))

# -4 10
# 2 4

-1

In Python, use the for i in range returns only integer values.

To get values of type . float, a possible solution would be:

import numpy as np
def funcs():
    for x in np.arange(-100.0, 100.0, 0.1, dtype = float):

But since Woss answered above, I think his equations return only these values, we would need to test another equation (with known intersection points) to test the hypothesis of returning float values.

Very cool your question!

useful links:

Python - Float Range

How to print float number using foor loop

  • 1

    In this case even the range of floats will not solve, because even defining a step of 0.1, still will not be all possible values. Not to mention that limiting the range from -100 to 100 also greatly reduces the range of possible solutions. The solution may be outside this range. When using sympy we have none of these limitations.

  • 1

    THANK YOU, YOU HELPED A LOT!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.