'Substitute a derivative in symbolic function with "chained" dependencies

This answer explains how to substitute a derivative in a symbolic expression. However, this approach fails when why function is "chained twice", e.g. sin(psi(phi(t))). See the example

clear;
syms t phi(t) psi(phi)
psi = psi(phi)
dphidt = diff(phi, t)
dpsidphi = diff(psi, phi)

x = sin(psi)
dxdt = diff(x, t)

syms phid psip % Substitution variables for derivatives

test1 = subs(dxdt,dphidt,phid) % This works
test2 = subs(dxdt,dpsidphi,psip) % This doesn't work

% Ideal result, without any dependencies
res = psip*phid*cos(psi)

Substituting phid for diff(phi(t), t) works, but substituting psip for D(psi)(phi(t)) does not work. What am I doing wrong? Do I have to specify the chained dependecies in a different way? I also don't quite understand the difference between D and diff.


Second question:
My actual terms are a lot more complicated and I have to take derivatives with respect to phid for example. Therefore, I have to get rid of all the dependencies because I want to generate a function using matlabFunction. I can remove the t dependency using formula, however I can't get rid of the phi(psi) dependency. Is there a command for this or do I have to make another substitution?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source