4
$\begingroup$

How to numerically find points of intersection between pair of curves (Here,a circle and a parabola) ? Finding it a bit messy as, for a point on one curve, slope of the other is involved.

ContourPlot[ (x^2 + y^2 - 4) *( y - x^2 + 2 x - 1) == 0, {x, -4, 4}, {y, -4, 4} , ContourStyle -> {Thick, Magenta}, 
     GridLines -> Automatic] 
$\endgroup$
3
$\begingroup$

FindInstance works as well:

fi = FindInstance[(x^2 + y^2 - 4) == 0 && (y - x^2 + 2 x - 1) == 
     0, {x, y}, Reals, 2] // N

{*
{x -> -0.399864, y -> 1.95962}, {x -> 1.85894, y -> 0.737785}
*}

ContourPlot[(x^2 + y^2 - 4)*(y - x^2 + 2 x - 1) == 0, {x, -4, 
  4}, {y, -4, 4}, ContourStyle -> {Thick, Blue}, 
 GridLines -> Automatic, 
 Epilog -> {Red, PointSize[Large], Point[{x, y}] /. fi}]

enter image description here

And we can compare the results of FindInstance with NSolve

nsol = NSolve[{(x^2 + y^2 - 4) == 0, (y - x^2 + 2 x - 1) == 0}, {x, 
   y}, Reals]

{*
{x -> -0.399864, y -> 1.95962}, {x -> 1.85894, y -> 0.737785}
*}

fi == nsol
(*
True
*)
$\endgroup$
4
$\begingroup$

Using this great post of J.M. which defines FindAllCrossings2D

f[x_, y_] := (x^2 + y^2 - 4)
g[x_, y_] := (y - x^2 + 2 x - 1)

pts = FindAllCrossings2D[{f[x, y], g[x, y]}, {x, -7/2, 4}, {y, -9/5, 
     21/5}, Method -> {"Newton", "StepControl" -> "LineSearch"}, 
    PlotPoints -> 85, WorkingPrecision -> 20] // Chop;

pl=ContourPlot[{f[x, y], g[x, y]}, {x, -7/2, 4}, {y, -9/5, 21/5}, 
 Contours -> {0}, ContourShading -> False, 
 Epilog -> {AbsolutePointSize[6], Red, Point /@ pts}]

Mathematica graphics

we can check it works

 NSolve[{(x^2 + y^2 - 4) == 0, (y - x^2 + 2 x - 1) == 0}, {x,y}, Reals]

(* {{x->-0.399864,y->1.95962},{x->1.85894,y->0.737785}} *)

Show[pl, 
Graphics[{AbsolutePointSize[12], Purple, 
Point[{x, y}] /. 
NSolve[{(x^2 + y^2 - 4) == 0, (y - x^2 + 2 x - 1) == 0}, {x, y},  Reals]}]]

Mathematica graphics

$\endgroup$
3
$\begingroup$

MeshFunctions

You can also use a combination of the options MeshFunctions and Mesh:

f[x_, y_] := (x^2 + y^2 - 4)
g[x_, y_] := (y - x^2 + 2 x - 1)

ContourPlot[{f[x, y], g[x, y]}, {x, -7/2, 4}, {y, -9/5, 21/5},
 Contours -> {0}, ContourShading -> False, BaseStyle -> Thick,
 MeshFunctions -> {g[#, #2] - f[#, #2] &},
 Mesh -> {{{0, Directive[Red, PointSize[Large]]}}}]

enter image description here

See also: Marking points of intersection between two curves

Graphics`Mesh`FindIntersections

Using the function Graphics`Mesh`FindIntersections to find the intersections:

Graphics`Mesh`MeshInit[];
cp = ContourPlot[{f[x, y], g[x, y]}, {x, -7/2, 4}, {y, -9/5, 21/5}, 
  Contours -> {0}, ContourShading -> False, BaseStyle -> Thick];

Show[cp, Epilog -> {Red, PointSize[.03], 
     Point[Graphics`Mesh`FindIntersections[Normal @ cp]]}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.