Find the points at which two given functions intersect

Submitted by
SciPy Central on 17 July 2011
Update history
Revision 2 of 3: previous next
Updated by
kevindunn on 21 July 2011
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])

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\\\end{split}\]\[\begin{split}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

Read the documentation for fsolve.

Please Sign in or Register to leave a comment

  • Creative Commons Zero. No rights reserved.
    Users have permission to do anything with the code and other material on this page. (More details)
  • Trademarks are property of their respective owners. Code and comments are owned by their respective posters. © 2013 All Rights Reserved