# How to plot the result of Grad function?

When I calculate the gradient of a two dimension function like $$r=\frac{1}{\sqrt{x^2+y^2}}$$ with the syntax of

h[x_,y_]:=Grad[1/r,{x,y}]


and when I checked the result with

h[x,y][[1]]
h[x,y][[2]]


I got a right result of $$-\frac{x}{(\sqrt{x^2+y^2})^{-3/2}}$$ and $$-\frac{y}{(\sqrt{x^2+y^2})^{-3/2}}$$, respectively.

However, when I tried to plot the function with syntax of

VectorPlot[{h[x,y][[1]],h[x,y][[2]]},{x,0.1,0.3},{y,-0.3,0.3}]


I found the value in y direction is 0, and I also confirmed with

Plot3D[h[x,y][[1]],{x,-0.3,0.3},{y,-0.3,0.3}]
Plot3D[h[x,y][[2]],{x,-0.3,0.3},{y,-0.3,0.3}]


and the results are that the first one has a proper plot while the second one shows the function value is 0.

Would anyone give me some clue that how this happened? Thank you and best regards!

• a typo: Grad[1/r,{x,y}] should be Grad[r,{x,y}]?
– kglr
Commented Jul 16, 2019 at 11:02
• try VectorPlot[Evaluate@{h[x,y][[1]],h[x,y][[2]]},{x,0.1,0.3},{y,-0.3,0.3}] ( better yet as in Ulrich's answer VectorPlot[h[x,y] ,{x,0.1,0.3},{y,-0.3,0.3}]) and Plot3D[Evaluate@h[x,y][[1]],{x,-0.3,0.3},{y,-0.3,0.3}], Plot3D[Evaluate@h[x,y][[2]],{x,-0.3,0.3},{y,-0.3,0.3}]
– kglr
Commented Jul 16, 2019 at 11:04
• Thank you for you reply, yes, it is a typo.
– Amon
Commented Jul 16, 2019 at 11:35
• With the VectorPlot[Evaluate@{h[x,y][[1]],h[x,y][[2]]},{x,0.1,0.3},{y,-0.3,0.3}], it worked, Thank you kglr!
– Amon
Commented Jul 16, 2019 at 11:42

ClearAll[h]
r = Sqrt[x^2 + y^2];
h[u_, v_] := Evaluate[Grad[1 / r, {x, y}]] /. {x -> u, y -> v}


Use Evaluate on the first arguments of VectorPlot and Plot3D:

VectorPlot[Evaluate @ h[x, y], {x, .1, 0.3},{y, -0.3, 0.3}]


Row[{Plot3D[Evaluate@h[x, y][[1]], {x, -0.3, 0.3}, {y, -0.3, 0.3}],
Plot3D[Evaluate@h[x, y][[2]], {x, -0.3, 0.3}, {y, -0.3, 0.3}]}]


• This helped a lot, Thank you! Another question is if I use 'If' expression, it doesn't work again. The function like h[x_, y_]:= If[-0.01<x<0.01&&-0.01<y<0.01, {0,0}, Grad[1/r,{x,y}]], does the Evaluate function not work in this situation? Thank you!
– Amon
Commented Jul 16, 2019 at 12:01
• @Amon, try ClearAll[h]; r=Sqrt[x^2+y^2];h[u_, v_]:= If[-0.01<u<0.01&&-0.01<v<0.01,{0,0},Evaluate[ Grad[1/r,{x,y}]/. {x->u,y->v}]] ; VectorPlot[Evaluate@h[x,y],{x,.1,0.3},{y,-0.3,0.3}]
– kglr
Commented Jul 16, 2019 at 12:37
• It worked! Thank you@kglr! I'm new to Mathematica, it needs time to learn.
– Amon
Commented Jul 17, 2019 at 2:44
h[x_, y_] := Grad[1/Sqrt[x^2 + y^2], {x, y}]
VectorPlot[h[x, y]//Evaluate, {x, 0.1, 0.3}, {y, -0.3, 0.3}]


• fyi, the above gives blank plot in V 13.2 ! from clean kernel. Screen shot !Mathematica graphics do you think I should post a question on this, or do you something I am not seeing? But adding Evaluate makes it work. !Mathematica graphics Strange that it worked for you without Evaluate. something seems to have changed in 13.2 Commented Dec 28, 2022 at 22:16
• @Nasser Thank you for your hint. In the year 2019 it seems to work on MMA version v12. I can't reproduce this result with MMA v12.2. I modified my answer! Commented Dec 29, 2022 at 9:35