0

I want to define a function of $x$, $y$ and $z$, and then compute the gradient of that function. I want the result to be a triple: $(df/dx, df/dy, df/dz)$. I have successfully defined a function, but when I ask for Grad, Mathematica substitutes zeroes for $x$, $y$ and $z$ and then evaluates the gradient. I want $x$, $y$ and $z$ to be retained as symbols.

kglr
  • 394,356
  • 18
  • 477
  • 896
Gene Naden
  • 327
  • 2
  • 9
  • 1
    To come up with an answer, most of us would want to run tests with your code. But you give us no code. Do you expect us to envision your code by ESP and then type into a Mathematica notebook? I think you are unlikely to get an answer unless you edit your question to include a all the code needed to reproduce your problem. – m_goldberg Jul 11 '18 at 20:42
  • I hear your frustration about my question. However, to suggest I want ESP was a little sarcastic. Anyway, I gave enough information; I was directed to another post that had an answer. – Gene Naden Jul 11 '18 at 21:54

2 Answers2

1
ClearAll[f, x, y, z]
f[x_, y_, z_] := Sin[x] Cos[y + z] z^2;

Grad[f[x, y, z], {x, y, z}]

{z^2 Cos[x] Cos[y + z],
-z^2 Sin[x] Sin[y + z],
2 z Cos[y + z] Sin[x] - z^2 Sin[x] Sin[y + z]}

Compare with (which is what you probably tried)

Grad[f, {x, y, z}]

{0, 0, 0}

kglr
  • 394,356
  • 18
  • 477
  • 896
0
f[x_,y_,z_]:= Sin[x] Cos[y + z] z^2;

D[f[x, y, z], {{x, y, z}}]

(*

{z^2 Cos[x] Cos[y + z], -z^2 Sin[x] Sin[y + z], 2 z Cos[y + z] Sin[x] - z^2 Sin[x] Sin[y + z]}

*)

David G. Stork
  • 41,180
  • 3
  • 34
  • 96