0
$\begingroup$

I’m coding my first physics simulation in Mathematica, and have a problem. I want to do the following inside a single cell input.

Clear[x, y]
F[x_, y_] := Cos[x] Cos[y];
\!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]\(F[x, y]\)\)

Then I click shift enter and copy output -Cos[y] Sin[x] to the next cell as the deffinition of G[x,y].

G[x_, y_] := -Cos[y] Sin[x];
Print[{G[a, b], G[c, d], G[e, f]}]

Note, that above was only an example. The code should work for any differentiable function F[x,y]. Also, I want the code to do differentiation only once, because G[x,y] will then be evaluated tens of millions of times.

Below is an image of the code. enter image description here

$\endgroup$

2 Answers 2

4
$\begingroup$

We can also use Derivative act on the function F to get a pure function and then Apply to another variables.

Clear["`*"];
F[x_, y_] := Cos[x] Cos[y];
G=Derivative[1, 0][F]

(*  -Cos[#2] Sin[#1]&  *)

G@@@ {{a, b}, {c, d}, {e, f}, {s, t}, {u, v}, {x, y}}

(*  {-Cos[b] Sin[a],-Cos[d] Sin[c],-Cos[f] Sin[e],-Cos[t] Sin[s],-Cos[v] Sin[u],-Cos[y] Sin[x]}  *)
$\endgroup$
1
$\begingroup$

This works for me on V 12.1 on windows

Clear[x, y, F, G]
F[x_, y_] := Cos[x] Cos[y];
G[x_, y_] := D[F[x, y], x];

{G[x, y], G[c, f], G[e, f]}

Mathematica graphics

If I were you, I'd avoid UpperCase single letters. I would also avoid using the math input palettes to enter derivatives and so on and get used to using plain text Mathematica commands, so you get used to them instead of just clicking on a symbol. But this is just me.

So instead of

Mathematica graphics

I would write

Mathematica graphics

Because I want to see the command itself in plain text and also I might want to later save the code as plain text file (.m).

$\endgroup$
1
  • $\begingroup$ Try G[1, y]. (Use = instead of := here.) $\endgroup$
    – Michael E2
    Commented Sep 8, 2020 at 23:24

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.