2
$\begingroup$

Consider the following code:

fun[x_] := x^2;
Plot[fun[x], {x, -4, 4}]

which simply plots a parabola for the range of -4 to 4. I don't know how many points between -4 and 4 Mathematica uses to plot the function? My question is how can I control this number? In fact I have a complicated function which its evaluation takes a long time. On the other hand I need to plot it in range of 0 to 4 to see its shape. When I try Plot command in the range of {0,4} it takes a very very long time to get the curve (it has never finished), so I want to reduce number of points. I took a look on the options of Plot command but I didn't find my desire one. Any idea?

$\endgroup$
5
  • $\begingroup$ see PlotPoints and MaxRecursion $\endgroup$
    – kglr
    Dec 3 '20 at 14:58
  • $\begingroup$ try also Plot[Evaluate @ yourcomplicatedfunction[x], {x, -4, 4}] $\endgroup$
    – kglr
    Dec 3 '20 at 14:59
  • $\begingroup$ @kglr what does Evaluate command do? $\endgroup$
    – Wisdom
    Dec 3 '20 at 15:14
  • $\begingroup$ Another thing you might try is to define the function as fun[x_] = x^2; (remove the colon before the equals). This may be why the plotting is so slow. $\endgroup$
    – bill s
    Dec 3 '20 at 15:41
  • $\begingroup$ I meant, just in case, you have a case like this $\endgroup$
    – kglr
    Dec 3 '20 at 15:44
3
$\begingroup$

You can use the options PlotPoints and MaxRecursion. For example

fun[x_] := x^2;
Plot[fun[x], {x, -4, 4}, PlotPoints -> 5, MaxRecursion -> 0]

gives

enter image description here

$\endgroup$
2
  • $\begingroup$ PlotPoints is the number of points? $\endgroup$
    – Wisdom
    Dec 3 '20 at 15:13
  • $\begingroup$ PlotPoints is the number of initial sampling points. MaxRecursion the number of times the sampling is iterated. As an example, the options PlotPoints -> 5, MaxRecursion -> 0 and PlotPoints -> 2, MaxRecursion -> 2 give the same answer. $\endgroup$
    – Roderic
    Dec 3 '20 at 15:16

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.