1
$\begingroup$

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!

$\endgroup$
4
  • $\begingroup$ a typo: Grad[1/r,{x,y}] should be Grad[r,{x,y}]? $\endgroup$
    – kglr
    Commented Jul 16, 2019 at 11:02
  • $\begingroup$ 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}] $\endgroup$
    – kglr
    Commented Jul 16, 2019 at 11:04
  • $\begingroup$ Thank you for you reply, yes, it is a typo. $\endgroup$
    – Amon
    Commented Jul 16, 2019 at 11:35
  • $\begingroup$ 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! $\endgroup$
    – Amon
    Commented Jul 16, 2019 at 11:42

2 Answers 2

2
$\begingroup$
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}]

enter image description here

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}]}]

enter image description here

$\endgroup$
3
  • $\begingroup$ 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! $\endgroup$
    – Amon
    Commented Jul 16, 2019 at 12:01
  • $\begingroup$ @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}] $\endgroup$
    – kglr
    Commented Jul 16, 2019 at 12:37
  • 1
    $\begingroup$ It worked! Thank you@kglr! I'm new to Mathematica, it needs time to learn. $\endgroup$
    – Amon
    Commented Jul 17, 2019 at 2:44
2
$\begingroup$
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}]

enter image description here

$\endgroup$
2
  • $\begingroup$ 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 $\endgroup$
    – Nasser
    Commented Dec 28, 2022 at 22:16
  • $\begingroup$ @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! $\endgroup$ Commented Dec 29, 2022 at 9:35

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.