How to get the coordinates of the intersection of two lines from a ListLinePlot?
The internal function Graphics`Mesh`FindIntersections
has limitations that are not well understood, but it can be applied directly to plots. For normal plots, it has always worked for me. It will find all the intersections, too, if there are more than one.
lst1 = Table[{x, x^2}, {x, 0, 5, 0.5}];
lst2 = Table[{x, x + 3}, {x, 0, 5, 0.5}];
plot = ListLinePlot[{lst1, lst2}]
Graphics`Mesh`FindIntersections@plot
(* {{2.28571, 5.28571}} *)
To compare with the OP's answer using Interpolation
, this method is equivalent to using InterpolationOrder -> 1
.
f = Interpolation[lst1, InterpolationOrder -> 1];
g = Interpolation[lst2, InterpolationOrder -> 1];
{x, f[x]} /. FindRoot[f[x] == g[x], {x, 2.1}]
(* {2.28571, 5.28571} *)
The default interpolation order, which is cubic, gives a slightly different answer:
f = Interpolation[lst1];
g = Interpolation[lst2];
{x, f[x]} /. FindRoot[f[x] == g[x], {x, 2.1}]
(* {2.30278, 5.30278} *)
This agrees exactly with the roots of the functions used to construct the lists because those functions, x^2
and x + 3
, have degrees that do not exceed the interpolation order (and there are a sufficient number of data points).
Yet another way is to use the interpolation of one set of points as an appropriate mesh function in the plot of the other.
Here
mf = Interpolation[lst2];
and
plt = ListLinePlot[lst1, MeshFunctions -> (#2 - mf[#1] &), Mesh -> {{0}}, MeshStyle -> Red]
You can then extract the point with
plt// Cases[Normal@#, Point[a_] :> a, Infinity] &
(*{{2.28571, 5.28571}}*)
Following the comment by @J.M., here is a solution:
lst1 = Table[{x, x^2}, {x, 0, 5, 0.5}];
lst2 = Table[{x, x + 3}, {x, 0, 5, 0.5}];
GraphicsRow[{ListLinePlot[{lst1, lst2}], ListPlot[{lst1, lst2}]}]
f = Interpolation[lst1];
g = Interpolation[lst2];
cords = {x = x /. FindRoot[f[x] == g[x], {x, 2.1}], f[x]}
cordsExact = {x /. FindRoot[x^2 == x + 3, {x, 2.1}], x^2}
cheers!