Question:

I want to differentiate the following function wrt in MATLAB: T(<sup>e(x(t),t)</sup>⁄<sub>p(t)</sub>)

My problem is that I know the derivatives of x numerically (I am inside a kind of odefun). I want to use diff to make my code generalizeable for high order derivatives,but the derivatives of x are now constant. I would also like all this to be in an anonymous function where I can make the differentiation and substitute accordingly the time and the derivative of x needed,so that I don't have to write multiple functions for every state of my system.

My code is as follows:

```
syms q x star;
qd=symfun(90*pi/180+30*pi/180*cos(q),[q]);
p=symfun(79*pi/180*exp(-1.25*q)+pi/180,[q]);
T=log(-(1+star)/star);
e=symfun(x-qd,[x,q]);
```

and I want to write for example a function in the form

@(t,y)(<sup>d^2</sup>⁄<sub>dt^2</sub> T(<sup>e(x(t),t)</sup>⁄<sub>p(t)</sub>)+<sup>d</sup>⁄<sub>dt</sub> T(<sup>e(x(t),t)</sup>⁄<sub>p(t)</sub>)+T(<sup>e(x(t),t)</sup>⁄<sub>p(t)</sub>))

Answer1:I am not sure of the implementation details but in general this is one approach that you could take. It involves two steps.

<ol><li>in the`T(.)`

function replace `x`

with `exp(t)`

this way when you do the differentiation `exp(t)`

always stays there for the higher order derivatives to be taken and the outer functions will be differentiated with respect to `x`

at the same time. After you do `diff`

you should receive an expression that contains `exp(t)`

(not tested so hopefully it is the case). At this point `exp(t)`

is your time derivative of `x`

. Now you only need to evaluate this expression in `t`

. When doing so you need to replace `exp(t)`

by the derivative of `x`

. I do not know if this can be done, if not then perhaps using `y`

instead of `exp(t)`

with the constraint `y=exp(t)`

would do it but you need to figure the correct implementation out yourself. </li>
<li>Here you need to substitute the derivative of `x`

at the right `t`

. If you do not have the value at the particular point `t`

then do what I suggested in the comment. Pre-calculate it beforehand in many points and interpolate in this step. </li>
</ol>This approach relies on swapping `x(t)`

with `exp(t)`

if that does not work then I would do what I suggested in the comment. Approximate `x(t)`

by a known function and use that instead of `x`

in your code.