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.)


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?

  • $\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 Feb 11 '19 at 21:58
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

  • $\begingroup$ Thanks, that does the trick! $\endgroup$ – Julian Strecker Feb 12 '19 at 16:03

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.