2
$\begingroup$

I have this function:

f[k_, θ_, ϕ_] := Cos[k*Cos[θ]*x + k*Sin[θ]*y - ϕ]

I want to change the parameters $\theta, \phi,$ and $k$ to see the effects on the function. I use the command below to achieve that:

Manipulate[
  ContourPlot[f[k, θ, ϕ], {x, 0, 50}, {y, 0, 50}, PlotLegends -> Automatic],
{k, 0, 10}, {θ, 0, 2*Pi}, {ϕ,0, 2*Pi}]

The problem is that when I change one of the parameters, say $\theta$, continuously the plot is evaluated slowly so that during the running you cannot see the actual graph. How can I change those parameters and see the real evaluated result immediately?

$\endgroup$
2
$\begingroup$

What you are seeing is not due to ContourPlot being slow per se. It is because when controls are active, ContourPlot produces a poorer quality plot by default. The plot commands are designed to interact with the dynamic system. But ultimately, I think ContourPlot will be too slow to do what you seek.

The way to get nice detailed plot is to use the option setting PerformanceGoal -> "Quality":

  ContourPlot[f[k, θ, ϕ], {x, 0, 50}, {y, 0, 50},
   PerformanceGoal -> "Quality", PlotLegends -> Automatic]

However you will notice a considerable lag in response, especially as k gets bigger than 0.5 or 1.0 depending on the speed of your computer. As far as I can tell, any plot with k bigger than 1. will have to compute so many points and contour lines that the performance is bound to be unacceptable.

If you can be satisfied with k between 0. and 0.5, perhaps this will be satisfactory:

Manipulate[
 ContourPlot[f[k, θ, ϕ], {x, 0, 50}, {y, 0, 50}, 
  PlotPoints -> ControlActive[15, Automatic], 
  MaxRecursion -> ControlActive[0, Automatic], 
  PlotLegends -> Automatic],
  {k, 0, 0.5}, {θ, 0, 2*Pi}, {ϕ, 0, 2*Pi}]

If your computer is moderately fast, you might try a higher number of plot points, such as PlotPoints -> ControlActive[25, Automatic], for a slightly better plot. (ControlActive switches the settings depending on whether a control is being moved.)

Note: The contours are lines and can be computed directly, with a little bit of math and programming, instead via sampling the plane. A faster way is possible, even if it is less elegant.

$\endgroup$
  • $\begingroup$ Would it be possible to make a movie of the changing graph? I mean it would be good if I could change the parameter k at each point and save the resulting graph and finally make a movie out of those many produced graphs. $\endgroup$ – MOON Aug 1 '14 at 11:21
  • 1
    $\begingroup$ @yashar Yes. See Is it possible to prerender animation in Wolfram Mathematica? $\endgroup$ – Michael E2 Aug 1 '14 at 13:00
0
$\begingroup$

You should set the step for you parameters for Manipulate. In this case it will work much faster. So instead of

Manipulate[
  ContourPlot[f[k, θ, ϕ], {x, 0, 50}, {y, 0, 50}, PlotLegends -> Automatic],
{k, 0, 10}, {θ, 0, 2*Pi}, {ϕ,0, 2*Pi}]

use for example:

Manipulate[
  ContourPlot[f[k, θ, ϕ], {x, 0, 50}, {y, 0, 50}, PlotLegends -> Automatic],
{k, 0, 10, 1}, {θ, 0, 2*Pi, Pi/20}, {ϕ,0, 2*Pi, Pi/20}]
$\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.