Find the points at which two given functions intersect

Submitted by
SciPy Central on 17 July 2011
Download
File
Update history
Revision 3 of 3: previous
Updated by
lissacoffey on 08 April 2015
Tags
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 http://scipy.org/Cookbook/Intersection

Read the documentation for fsolve.

When straight lines intersect on a 2-dimensional graph, they meet at only 1 point, which can be described by a single set of x- and y-coordinates. Because both lines pass through that point, you know that the x- and y- coordinates must satisfy both equations and, as long as you’re dealing with straight lines, you know those are the only coordinates that are valid in both equations. Instead of trying to calculate the point of intersection by squinting at a graph, you can use basic algebraic techniques to get an exact answer.

Intersect[ <Function>, <Function>, <Start x-Value>, <End x-Value> ]

Yields the intersection points numerically for the two functions in the given interval.

Example:

Let f(x) = x^3 + x^2 - x and g(x) = 4 / 5 + 3 / 5 x be two functions. Intersect[ f, g, -1, 2 ] yields the intersection points A = (-0.43, 0.54) and B = (1.1, 1.46) of the two functions in the interval [ -1, 2 ].

Intersect[ <Curve 1>, <Curve 2>, <Parameter 1>, <Parameter 2> ]

Find one intersection point using an iterative method starting at the given parameters.

Example:

Let a = Curve[cos(t), sin(t), t, 0, π] and b = Curve[cos(t) + 1, sin(t), t, 0, π].

Intersect[a, b, 0, 2] yields the intersection point A = (0.5, 0.87).

CAS Syntax

Intersect[ <Function>, <Function> ]

Yields a list containing the intersection points of two objects.

Example:

Let f(x):= x^3 + x^2 - x and g(x):= x be two functions. Intersect[ f(x), g(x) ] yields the intersection points list: {(1, 1), (0, 0), (-2, -2)} of the two functions.

Note: See also Tool Intersect Two Objects.gif Intersect tool. https://wiki.geogebra.org/en/Intersect_Tool

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