3
$\begingroup$

I would like to create a bifurcation diagram with ContourPlot. Suppose I want to plot the contour 1-2*a*Q+3*Q^3==0 with a the x-axis, Q the y-axis. I also want linestyle/thickness of the contour plot to change depending on the expression expr = 9*Q^2-2*a. If expr>0, I want it to be dashed; if expr<0, I want it to be a thick line. How to accomplish this?

Some previous posts about similar topic suggested putting an If statement inside the plot, but when I try something like

ContourPlot[If[expr>0, 1-2*a*Q+3*Q^3 == 0], {a,-2,2}, {Q,-2,2}]

it gives error.

$\endgroup$
5
$\begingroup$

You can create two contour plots, and show them together:

Show[
    ContourPlot[
        1-2*a*Q+3*Q^3==0, {a,-2,2}, {Q,-2,2},
        RegionFunction->Function[{a,Q},9Q^2-2a<0],
        ContourStyle->Thick
    ],
    ContourPlot[
        1-2*a*Q+3*Q^3==0, {a,-2,2}, {Q,-2,2},
        RegionFunction->Function[{a,Q},9Q^2-2a>0],
        ContourStyle->Dashed
    ]
]

enter image description here

Another possibility using a single contour plot:

ContourPlot[
    {
    ConditionalExpression[
        1-2 a Q+3 Q^3,
        9Q^2-2a<0
    ]==0,
    ConditionalExpression[
        1-2 a Q+3 Q^3,
        9Q^2-2a>0
    ]==0
    },
    {a,-2,2},
    {Q,-2,2},
    ContourStyle->{Thick,Dashed}
]
$\endgroup$
  • $\begingroup$ That solves my problem. But when I handle more complicated equations I want to define bifurEq=1-2*a*Q+3*Q^3. So in the RegionFunction I need to have something like Function[{a,Q},D[bifurEq,Q]>0], but then it gives me error saying that blah blah blah must be a Boolean function. How to fix this (have Mathematica do the algebra instead of me typing the equation myself)? $\endgroup$ – Physicist Nov 17 at 18:09
  • $\begingroup$ Try Function[{a, Q}, Evaluate @ D[bifurEq, Q] > 0] instead. $\endgroup$ – Carl Woll Nov 17 at 18:11
  • $\begingroup$ It works. Thank you. $\endgroup$ – Physicist Nov 17 at 18:14

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.