7

Suppose we have two equation

$f(x,y,c)=0$

and

$g(x,y,c)=0$

where $c$ is an unknown constant.

I am trying to plot a graph for $x,y$. One way to do it is to solve one of the equations for $c$ then substitute the value of $c$ in the second equation to obtain an expression for $x$ and $y$. Then we can use the ContourPlot to plot a graph for $x$ and $y$.

However, what if $f$, $g$ are too complicated and we can't solve for $c$ to substitute in the other equation to obtain the an expression for $x,y$. Is there another method or a special function one can use to plot without solving the equations?

Thanks.

Example:

$f(x,y,c)=c \left(c^3-c x^2+\log (c)+2 (x-1)^2\right)-(c-1)^2 y^2=0$

$g(x,y,c)=2 \left(-c \left(x^2+y^2-1\right)+2 \sin ^3(c)+(x-1)^2+y^2\right)=0$

In Mathematica terms:

f[x_, y_, c_] := c (c^3 - c x^2 + Log[c] + 2 (x - 1)^2) - (c - 1)^2 y^2;
g[x_, y_, c_] := 2 (-c (x^2 + y^2 - 1) + 2 Sin[c]^3 + (x - 1)^2 + y^2);
MrDi
  • 474
  • 4
  • 10

2 Answers2

4

As suggested by Szabolcs in the comments:

f[x_, y_, c_] := c (c^3 - c x^2 + Log[c] + 2 (x - 1)^2) - (c - 1)^2 y^2;
g[x_, y_, c_] := 2 (-c (x^2 + y^2 - 1) + 2 Sin[c]^3 + (x - 1)^2 + y^2);

cp3 = ContourPlot3D[{f[x, y, c] == 0, g[x, y, c] == 0}, 
  {x, 0, 4}, {y, -5, 5}, {c, 0, 2}, ContourStyle -> Opacity[0.6], 
  Mesh -> None, PlotPoints -> 50, MaxRecursion -> 0, 
  BoundaryStyle -> {1 -> None, 2 -> None, {1, 2} -> Directive[Thick, Red]}]

enter image description here

Graphics[{Thick, ColorData[1, 1], 
  Cases[Normal@cp3, Line[ps_] :> Line[Most /@ ps], Infinity]}, 
 PlotRange -> {{0, 4}, {-5, 5}}, Axes -> True]

enter image description here

This method does requires you to know the range of $c$ where the solutions may be found -- in this case, after some experimentation I took $c \in [0, 2]$. If you need to consider an unbounded range, you could define $\theta=\tan^{-1}c$ and plot $f(x,y,\tan\theta)=0$, $g(x,y,\tan\theta)=0$ over the range $\theta\in(-\pi/2,\pi/2)$.

  • In retrospect, I've answered this kind of question before... –  Nov 12 '16 at 17:51
  • Why does the solution take too long for some functions of $f$ and $g$? – MrDi Nov 12 '16 at 18:17
  • MrDi: Probably because some functions $f$ and $g$ are expensive to evaluate? It has to perform $O(n^3)$ function evaluations, where $n$ is the value of PlotPoints. –  Nov 12 '16 at 18:22
  • Is there a different quicker method? – MrDi Nov 12 '16 at 18:25
  • You can try reducing PlotPoints. Though that's not "a different quicker method", just the same method with lower quality. –  Nov 12 '16 at 18:30
1

Here's an approach using some functions for tracking roots I hacked together previously.

First, load the function TrackRootPAL defined here. Then, define your functions:

f[x_, y_, c_] := c (c^3 - c x^2 + Log[c] + 2 (x - 1)^2) - (c - 1)^2 y^2;
g[x_, y_, c_] := 2 (-c (x^2 + y^2 - 1) + 2 Sin[c]^3 + (x - 1)^2 + y^2);

Make a ContourPlot to get some idea where to start:

c = 0.5;
ContourPlot[{f[x, y, c] == 0, g[x, y, c] == 0}, {x, -2, 5}, {y, -5, 5}]

Mathematica graphics

Looks like there are two roots. Use FindRoot to find one of them:

init = FindRoot[{f[x, y, c] == 0, g[x, y, c] == 0}, {x, 2.5}, {y, 0.75}]
(* {x -> 2.3209, y -> 0.675456} *)

Now use TrackRootPAL to continue this root as a function of your parameter c:

Clear[c];
tr = TrackRootPAL[{f[x, y, c], g[x, y, c]}, {x, y}, {c, 0.4, 1}, 0.5, {x, y} /. init, NDSolveOpts -> {AccuracyGoal -> 6}]

This gives two solutions (one for each root) as pairs of InterpolatingFunctions, which you can ParametricPlot:

ParametricPlot[Evaluate[{x[c], y[c]} /. tr], {c, 0.4, 1}, PlotRange -> {{-2, 5}, {-5, 5}}]

Mathematica graphics

I used TrackRootPAL because it can go around corners to track both roots. Hope this helps for your real problem.

Chris K
  • 20,207
  • 3
  • 39
  • 74