# Substitute Expression in for Derivative?

I am trying to create a notebook to evaluate a line integral Line Integral on SO(3). Everything is going fine up to this point:

g[t_] := {t, t^2, t^3}
limits = {t, 0.5, 1.5}
M1[{w_, x_, y_, z_}] := w*x*y*z
M2[{w_, x_, y_, z_}] := w*x*y*z
M3[{w_, x_, y_, z_}] := w*x*y*z
M4[{w_, x_, y_, z_}] := w*x*y*z
M[{w_, x_, y_, z_}] := {M1[{w, x, y, z}], M2[{w, x, y, z}], M3[{w, x, y, z}], M4[{w, x, y, z}]}
r[{psi_, th_, phi_}] := {Cos[(phi + psi)/2]*Cos[th/2], Cos[(phi - psi)/2]*Sin[th/2], Sin[(phi - psi)/2]*Sin[th/2], Sin[(phi + psi)/2]*Cos[th/2]}
r2[psi_, th_, phi_] := {Cos[(phi + psi)/2]*Cos[th/2], Cos[(phi - psi)/2]*Sin[th/2], Sin[(phi - psi)/2]*Sin[th/2], Sin[(phi + psi)/2]*Cos[th/2]}
J[psi_, th_, phi_] := D[r2[psi, th, phi], {{psi, th, phi}}]
Mst[psi_, th_, phi_] := M[r[psi, th, phi]].J[psi, th, phi]


This gives me the pullback of $$\mathbf{M}$$, $$\mathbf{M}^*$$, as a function of $$\psi, \theta$$, and $$\phi$$.

The problem occurs when I try to substitute $$g(t)$$ (really, $$\gamma(t)$$) in for $$(\psi, \theta, \phi)$$ in $$\mathbf{M}^*$$:

Mstg[t_] := Mst[g[t]]
Mstg[t]
General::ivar: t^2 is not a valid variable.


I would like to go on to do something like

R[t_] := r[g[t]]
Rp[t_] := D[R[t], t]
Integrate[Mstg[t].Rp[t], limits]


getting the path $$\mathbf{R}$$ as a function of t and computing its derivative and computing the line integral. How do I substitute $$\gamma(t)$$ into the expressions for $$\mathbf{M}^*(\psi, \theta, \phi)$$ (= Mst[psi, th, phi]) and $$\vec{r}(\psi, \theta, \phi)$$ (= r[psi, th, phi]) and differentiate $$\mathbf{R}(t) = \vec{r}[\gamma(t)]$$ with respect to $$t$$ and evaluate the line integral?

(To be clear, there are two problem lines, Mstg[t_] := Mst[g[t]] and R[t_] := r[g[t]] leading to Rp[t_] := D[R[t], t].)

Any assistance you provide is appreciated.

• You do not define r2. Anyway, try J[psi_, th_, phi_] = D[r2[psi, th, phi], {{psi, th, phi}}] – yarchik Jun 26 '20 at 6:38
• General comment: You should probably also try to choose either the [arg1, arg2, ...] or the [{arg1, arg2, ...}] conventions. The mixing is just asking for trouble. – Natas Jun 26 '20 at 7:26
• Related/duplicate: mathematica.stackexchange.com/questions/1301/… – Michael E2 Sep 23 '20 at 15:32

The problem arises because you want to take the derivative with respect to an expression which is not a valid variable for D[].

A toy example is the following:

f[x_] := x^2
g[x_] := D[f[x], x]

g[x^2]
(* During evaluation of In:= General::ivar: x^2 is not a valid variable.*)


What happens is that g[x^2] evaluates to

D[f[x^2], x^2]


and D does not know how to treat x^2.

As pointed out by yarchik in the comments, one strategy is to use Set (=) rather than SetDelayed (:=) for defining g, i.e.

g[x_] = D[f[x], x]


which has also the benefit of only running D once and not at every call of g. Of course this is not so good if f might change in the future. Then a more robust strategy would be.

g[x_] := Derivative[f][x]


which generalizes to functions with more than one argument.

Edit: For your list-based functions you could try something along these lines:

f[{x_, y_}] := x^2 + y^2
g[{x_, y_}] := Module[{xP, yP},D[f[{xP, yP}], {{xP, yP}}] /. Thread[{xP, yP} -> {x, y}]]

g[{a, b}]
(* {2 a, 2 b} *)

• Thanks so much for replying! Changing the := to = is exactly what I needed and indeed did the trick; here is the finished notebook bit.ly/2YAuYnO . Thanks again so much for replying! – Jeffrey Rolland Jun 27 '20 at 14:00