# Finding intersections of NDSolve solution with curves

This is an extension to a previous question I've posted:

How to extract specific points from NDSolve solution

I would like to find the intersection of the solution given by NDSolve (code below) with curves on the phase space.

f[x_, y_] := -x + 0.1 y + x^2 y;
g[x_, y_] := 0.5 - 0.1 y - x^2 y;
F1 = 0.5;
F2 = x - x^3;
F3 = f[x, y] == 0
sol = NDSolve[{x'[t] == f[x[t], y[t]], y'[t] == g[x[t], y[t]],
x == 0.6, y == 1.4}, {x, y}, {t, 0, 300}];
sp = StreamPlot[{f[x, y], g[x, y]}, {x, 0, 1}, {y, 0.5, 2},
StreamStyle -> Black];
p = ParametricPlot[{Evaluate[x[t]], Evaluate[y[t]]} /. sol, {t, 0,
100}, PlotStyle -> Red];
pp1 = ParametricPlot[{F1, y}, {y, 0.5, 2}, PlotStyle -> Blue];
pp2 = ParametricPlot[{x, 1 + x - x^3}, {x, 0, 1}, PlotStyle -> Blue]
pp3 = ContourPlot[-x + 0.1 y + x^2 y == 0, {x, 0, 1}, {y, 0.5, 2},
ContourStyle -> Blue];
Show[sp, p, pp1, pp2, pp3]


For example: In the figure below I have three curves, the first is a straight line ( was solved in the prevoius answer), the second is a given function, and the third is the nullcline that is obtained numerically. For each curve, how can I obtain:

• A list of the points at which the solution intersect with the curve.
• A list with the time at which these intersections have occurred.

If you're willing to find these points within the NDSolve, you can use WhenEvent.

{sol, pts} = Reap@NDSolve[{x'[t] == f[x[t], y[t]], y'[t] == g[x[t], y[t]],
WhenEvent[x[t] == 0.5, Sow[{t, x[t], y[t]}, 1]],
WhenEvent[y[t] == 1 + x[t] - x[t]^3, Sow[{t, x[t], y[t]}, 2]],
WhenEvent[x'[t] == 0, Sow[{t, x[t], y[t]}, 3]],
x == 0.6, y == 1.4}, {x, y}, {t, 0, 300}];

Show[sp, p, pp1, pp2, pp3,
ListPlot[pts[[1, All, 2 ;; 3]], PlotStyle -> Green],
ListPlot[pts[[2, All, 2 ;; 3]], PlotStyle -> Cyan],
ListPlot[pts[[3, All, 2 ;; 3]], PlotStyle -> Yellow]
]
` Note: the intersection times are stored as the first element of each triple.

• Thank you. Nice and elegant. – jarhead Apr 29 '18 at 18:10