Find the points at which two given functions intersect

Submitted by: SciPy Central, 17 July 2011
Update history: Revision 2 of 2: previous 
Updated by: kevindunn, 21 July 2011
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from scipy.optimize import fsolve
import numpy as np

def f(input_vector):
   x, y = input_vector
   return  np.array([y - x**2, y - x - 1.0])


# Solve the function, using (x=1, y=2) as the initial guess
fsolve(f, [1.0, 2.0])
Software license: Creative Commons Zero. No rights reserved.
Users have permission to do anything with the code and other material on this page. (More details)
More information:

The code considers the case of finding the intersection of a polynomial, \(y=x^2\) and a line, \(y=x+1\).

Write these functions in the form \(\mathbf{f(x) = 0}\), in other words:

\[\begin{split}f_1(x, y) &= y - x^2 = 0\\ f_2(x, y) &= y - x - 1 = 0\end{split}\]

Now write your Python function, as shown in the code, so that it accepts a vector of these inputs, \(x\) and \(y\), and return another vector of outputs which contains \(\mathbf{f(x)}\).

Entry inspired by http://scipy.org/Cookbook/Intersection

Read the documentation for fsolve.