0
$\begingroup$

Suppose I want to plot a function $f(x,y)$ but first need to calculate its symbolic expression. An easy solution is to copy the output of my calculation by hand and then paste into Plot function. If I naively do this

f[x_,y_]:= 100*x^3+Tanh[y^2];
Plot3D[f[x,y],{x,-10,10},{y, -20, 20}]

then it runs very slowly (for a much more complicated function I'm plotting). I think what's happenning is that instead of symbolically calcualte f[x,y], Mathematica numerically calculates it.

Question: is there a way to delay Plot first and evaluate the function inside Plot? Maybe using Hold? But I don't see how I can evaluate the function first after holding.

$\endgroup$
  • 2
    $\begingroup$ Use f[x_,y_] = 100*x^3+Tanh[y^2] instead of f[x_,y_] := 100*x^3+Tanh[y^2]? Either that, or make a Table of values instead and use ListPlot3D. $\endgroup$ – march Jun 9 at 14:27
  • 1
    $\begingroup$ Expanding on what @march said: please read up on the distinction between immediate and delayed definitions. If you don't want to delay the definition, then just don't use a delayed definition (i.e., use an immediate definition instead). $\endgroup$ – Roman Jun 9 at 15:59
  • $\begingroup$ Add the option Evaluated->True inside Plot3D $\endgroup$ – Ulrich Neumann Jun 9 at 18:52
3
$\begingroup$

Plot3D has attribute HoldAll, so its arguments are not evaluated before variable substitution. You can override this with:

Plot3D[Evaluate[f[x, y]], {x, -10, 10}, {y, -20, 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.