1
$\begingroup$

I have a function defined by

f[x_, y_] := 1/(x^2+1) + 1/(y^2+1);

I can plot this function perfectly.

Then I want to StreamPlot the gradient of that function so I do:

myGrad[x_, y_] = Grad[f[x, y], {x, y}]]

(I also tried some different approaches with := and Evaluate and Function here.)

Sadly

StreamPlot[myGrad[x, y], {x, -2, 5}, {y, -2, 5}]

produces a blank plot. On the other hand, using the output as %xyz from the myGrad definition as the 1st argument in the StreamPlot call, renders a plot.

So, how do I reuse a functions result as definition of a new function and use the new function in a plot?

$\endgroup$
1
  • $\begingroup$ On my machine "11.3.0 for Mac OS X x86 (64-bit) (March 7, 2018)" your code produces a plot, no problem. $\endgroup$
    – Roman
    Commented Feb 11, 2019 at 21:58

1 Answer 1

2
$\begingroup$
ClearAll[f, myGrad]
f[x_, y_] := 1/(x^2 + 1) + 1/(y^2 + 1);
myGrad[x_, y_] := Grad[f[x, y], {x, y}];

Use the option Evaluated -> True or wrap the first argument with Evaluate in StreamPlot:

StreamPlot[myGrad[x, y], {x, -2, 5}, {y, -2, 5}, Evaluated -> True]

enter image description here

StreamPlot[Evaluate @ myGrad[x, y], {x, -2, 5}, {y, -2, 5}]

same picture

$\endgroup$
1
  • $\begingroup$ Thanks, that does the trick! $\endgroup$ Commented Feb 12, 2019 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.