I'm having a little trouble with substituting functions into an expression. A minimal example is as follows:
diffy := D[U[z], z];
Unevaluated[diffy]/. U[z] :> Exp[-I*k*z]
Derivative[1][U][z]
Instead I want to do this:
Unevaluated[D[U[z], z]] /. U[z] :> Exp[-I*k*z]
-I E^(-I k z) k
but obviously I can't do the latter because my practical "diffy" expression is a result of many steps and consists of many lines of functions and derivatives of U[z].
Is there a way Mathematica can take a function, substitute it into an expression such as "diffy" and evaluate it? I tried enclosing my expression above in "Evaluate[]" but that did not work.
Update: The solution provided works for the above prototype, but when I add another level, it all breaks down. This is a minimum example:
U0 := U[z];
diffy1 := Hold[D[U0, z]];
diffy1 /. U[z] :> Exp[-I*k*z] // ReleaseHold
Derivative[1][U][z]
I have tried holding the first expression, releasing the hold in the definition of the second one and then holding again - and many other combinations of holding/releasing but nothing has worked so far. Is there a fundamental reason why this is not working? Maybe I'm not thinking of Mathematica correctly but I don't understand why this does not work immediately.
Block[{U}, U[z_] := Exp[-I*k*z]; diffy]
$\endgroup$diffy := Hold[D[U[z], z]];
then usediffy /. U[z] :> Exp[-I k z] // ReleaseHold
. $\endgroup$rnd := Random[]; {rnd, rnd, rnd}
. Hold keeps the actual pattern unevaluated until you release it. $\endgroup$