title: Substitute a derivative in symbolic function with "chained" dependencies
body: <p><a href="https://stackoverflow.com/a/26998716/18462484">This</a> answer explains how to substitute a derivative in a symbolic expression. However, this approach fails when why function is &quot;chained twice&quot;, e.g. <code>sin(psi(phi(t)))</code>. See the example</p>
  <pre><code>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)
  </code></pre>
  <p>Substituting <code>phid</code> for <code>diff(phi(t), t)</code> works, but substituting <code>psip</code> for <code>D(psi)(phi(t))</code> 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 <code>D</code> and <code>diff</code>.</p>
  <hr />
  <p>Second question:<br />
  My actual terms are a lot more complicated and I have to take derivatives with respect to <code>phid</code> for example. Therefore, I have to get rid of all the dependencies because I want to generate a function using <code>matlabFunction</code>. I can remove the <code>t</code> dependency using <code>formula</code>, however I can't get rid of the <code>phi(psi)</code> dependency. Is there a command for this or do I have to make another substitution?</p>