from scipy.optimize import fsolve
import numpy as np
import matplotlib.pyplot as plt
# Defining function to simplify intersection solution
def findIntersection(func1, func2, x0):
return fsolve(lambda x : func1(x) - func2(x), x0)
# Defining functions that will intersect
funky = lambda x : np.cos(x / 5) * np.sin(x / 2)
line = lambda x : 0.01 * x - 0.5
# Defining range and getting solutions on intersection points
x = np.linspace(0,45,10000)
result = findIntersection(funky, line, [15, 20, 30, 35, 40, 45])
# Printing out results for x and y
print(result, line(result))
plt.plot(x, funky(x),x,line(x))
plt.scatter(result,line(result),color="red")
plt.savefig('ex325.png', dpi=100)
plt.show()
Recent Comments