1
$\begingroup$

I have some function (say, $f(x,y)=x^2+y$) I want to take the gradient of. I then want to make a table of $|\nabla f|$ evaluated at different values of $x$ and $y$. Here's the code I wrote:

Table[Norm[Grad[x^2+y,{x,y}]],{x,1,10},{y,1,10}]

I'm getting the following error message:

Grad::nocoord: {1,1} is not a non-empty list of valid variables. $\gg$

Grad::nocoord: {1,2} is not a non-empty list of valid variables. $\gg$

Grad::nocoord: {1,3} is not a non-empty list of valid variables. $\gg$

General::stop: Further output of Grad::nocoord will be suppressed during this calculation. $\gg$

What am I doing wrong?

$\endgroup$
  • 1
    $\begingroup$ Table[Evaluate[Norm[Grad[x^2 + y, {x, y}]]], {x, 1, 10}, {y, 1, 10}]? $\endgroup$ – kglr May 8 '18 at 1:18
  • $\begingroup$ That works! Can you please post it as a reply so I can accept it? $\endgroup$ – Rain May 8 '18 at 1:19
2
$\begingroup$
Table[Evaluate[Norm[Grad[x^2 + y, {x, y}]]], {x, 1, 10}, {y, 1, 10}]

or

Table[Norm[Grad[x^2 + y, {x, y}]] /. {x -> u, y -> v}, {u, 1, 10}, {v, 1, 10}]

both give (when wrapped with TeXForm):

$\left( \begin{array}{cccccccccc} \sqrt{5} & \sqrt{5} & \sqrt{5} & \sqrt{5} & \sqrt{5} & \sqrt{5} & \sqrt{5} & \sqrt{5} & \sqrt{5} & \sqrt{5} \\ \sqrt{17} & \sqrt{17} & \sqrt{17} & \sqrt{17} & \sqrt{17} & \sqrt{17} & \sqrt{17} & \sqrt{17} & \sqrt{17} & \sqrt{17} \\ \sqrt{37} & \sqrt{37} & \sqrt{37} & \sqrt{37} & \sqrt{37} & \sqrt{37} & \sqrt{37} & \sqrt{37} & \sqrt{37} & \sqrt{37} \\ \sqrt{65} & \sqrt{65} & \sqrt{65} & \sqrt{65} & \sqrt{65} & \sqrt{65} & \sqrt{65} & \sqrt{65} & \sqrt{65} & \sqrt{65} \\ \sqrt{101} & \sqrt{101} & \sqrt{101} & \sqrt{101} & \sqrt{101} & \sqrt{101} & \sqrt{101} & \sqrt{101} & \sqrt{101} & \sqrt{101} \\ \sqrt{145} & \sqrt{145} & \sqrt{145} & \sqrt{145} & \sqrt{145} & \sqrt{145} & \sqrt{145} & \sqrt{145} & \sqrt{145} & \sqrt{145} \\ \sqrt{197} & \sqrt{197} & \sqrt{197} & \sqrt{197} & \sqrt{197} & \sqrt{197} & \sqrt{197} & \sqrt{197} & \sqrt{197} & \sqrt{197} \\ \sqrt{257} & \sqrt{257} & \sqrt{257} & \sqrt{257} & \sqrt{257} & \sqrt{257} & \sqrt{257} & \sqrt{257} & \sqrt{257} & \sqrt{257} \\ 5 \sqrt{13} & 5 \sqrt{13} & 5 \sqrt{13} & 5 \sqrt{13} & 5 \sqrt{13} & 5 \sqrt{13} & 5 \sqrt{13} & 5 \sqrt{13} & 5 \sqrt{13} & 5 \sqrt{13} \\ \sqrt{401} & \sqrt{401} & \sqrt{401} & \sqrt{401} & \sqrt{401} & \sqrt{401} & \sqrt{401} & \sqrt{401} & \sqrt{401} & \sqrt{401} \\ \end{array} \right)$

$\endgroup$
  • $\begingroup$ Thanks! The first option is more concise, but I suppose the second one better illustrates what exactly you're doing to avoid the problem. $\endgroup$ – Rain May 8 '18 at 1:25
  • $\begingroup$ @Rain, my pleasure. Thank you for the accept. $\endgroup$ – kglr May 8 '18 at 1:35

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.