I have a question in Mathematica, I would appreciate it if you possibly answered me. What can I do for defining a special variable for derivative of arbitrary variable. For example, imagine I have a function as following

F[a_,b_]:= a  + b

Then I want to define

D[a,c] == R1

Define D[b,c] == R2

and at the end I get:

D[F[a,b],c] = R1 + R2 

can I do something like this in Mathematica? Thanks


One possibility: assign UpValues to symbols a and b and useDt instead:

Clear[a, b, f, r1, r2]
a /: Dt[a, c] := r1
b /: Dt[b, c] := r2
f[a_, b_] := a + b


Dt[f[a, b], c]
(* r1 + r2 *)

To see more clearly what's going on with Dt, consider:

Dt[g[a, b]]
(* Dt[b] D[g[a, b], b] + Dt[a] D[g[a, b], a] *)
  • $\begingroup$ Thanks alot, and one more thing, if the function f[c1_, c2_] and g[c1_,c2_], is there any possiblity to do the same thing for g and f for example f[c1, c2] /: Dt[f[c1, c2], g[c1,c2]] := r1 $\endgroup$ – amin bk Nov 24 '15 at 1:21
  • $\begingroup$ @aminbk: Once you start mixing the definitions like this, it's probably best to go Pillsy's route and use replacement rules. $\endgroup$ – march Nov 24 '15 at 3:15

I like @march's suggestion to use Dt (as well as the implicit suggestion to use lower-case symbol names), but one alternative approach is to use replacement rules. If they work for your use case, I find them much clearer than setting UpValues:

Clear[a, b, f, r1, r2];

f[a_, b_] := a + b;

Dt[f[a, b]] 

(* Dt[a, c] + Dt[b, c] *)

% /. {Dt[a, c] -> r1, Dt[b, c] -> r2}

(* r1 + r2 *)
  • $\begingroup$ Thanks, but just one more thing, Now if I have something like this f[a_,b_] and g[a_,b_] how can I define Dt[F[a,b],g[a,b]] ->r1 ?? t $\endgroup$ – amin bk Nov 24 '15 at 16:19

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.