7
$\begingroup$

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);
$\endgroup$
  • 2
    $\begingroup$ Do you have an actual example? $\endgroup$ – Feyre Nov 12 '16 at 10:50
  • 2
    $\begingroup$ One way is to plot it in 3D, extract the 3D curve (Cases), and drop one of the coordinates out of the three. $\endgroup$ – Szabolcs Nov 12 '16 at 10:58
  • 2
    $\begingroup$ Another way is to plug in a value for c, then solve the system for {x,y} numerically. There may be more than one solution. Do this for many values of c and connect the dots. These suggestions are of course starting points. What you are asking for is definitely doable, but definitely not trivial. Good question. $\endgroup$ – Szabolcs Nov 12 '16 at 11:05
  • 1
    $\begingroup$ As @Feyre suggests, an example would give people something concrete to work on. $\endgroup$ – Chris K Nov 12 '16 at 15:00
  • 1
    $\begingroup$ @MrDi can you make a minimal example that captures the essence of your real problem? $\endgroup$ – Chris K Nov 12 '16 at 15:40
4
$\begingroup$

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)$.

$\endgroup$
  • $\begingroup$ In retrospect, I've answered this kind of question before... $\endgroup$ – Rahul Nov 12 '16 at 17:51
  • $\begingroup$ Why does the solution take too long for some functions of $f$ and $g$? $\endgroup$ – MrDi Nov 12 '16 at 18:17
  • $\begingroup$ 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. $\endgroup$ – Rahul Nov 12 '16 at 18:22
  • $\begingroup$ Is there a different quicker method? $\endgroup$ – MrDi Nov 12 '16 at 18:25
  • $\begingroup$ You can try reducing PlotPoints. Though that's not "a different quicker method", just the same method with lower quality. $\endgroup$ – Rahul Nov 12 '16 at 18:30
1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ What is TrackRootPAL? I am not sure it is defined in my Mathematica? $\endgroup$ – MrDi Nov 12 '16 at 16:53
  • $\begingroup$ First, load the function TrackRootPAL defined here $\endgroup$ – Chris K Nov 12 '16 at 16:55
  • $\begingroup$ I am sorry if this is a stupid question but how do you load a function? Do you just copy and paste the code? $\endgroup$ – MrDi Nov 12 '16 at 17:00
  • $\begingroup$ Yes, that's all. $\endgroup$ – Chris K Nov 12 '16 at 17:01
  • $\begingroup$ I am getting this error ReplaceAll::reps: {init} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. $\endgroup$ – MrDi Nov 12 '16 at 17:01

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.