0

I want to differentiate the following function wrt in MATLAB: T(e(x(t),t)p(t))

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)(d^2dt^2 T(e(x(t),t)p(t))+ddt T(e(x(t),t)p(t))+T(e(x(t),t)p(t)))

Controller
  • 489
  • 7
  • 27
  • I don't understand your code, in particular I struggle to find the above formula in it. From the description however I gather that your problem is that the time derivative of `x` is not a known function. So one thing you could do is to calculate the derivative for many values of `x` and then approximate by a known function (essentially interpolating it). If you approximate the derivative by a low order polynomial then you will be able to calculate its higher order derivatives as well although you probably do not need that as you intend to use diff. – PetrH Aug 09 '14 at 15:01
  • The formula occurs if you substitute the variable "star" with "e(x(t),t)⁄p(t)".x is not really a function! I only know the values x,x',x'' at a given moment t. – Controller Aug 09 '14 at 15:27
  • OK, now I understand, I think. Initially I thought that `e` was Euler number (ie `exp(.)`) while it is a function `e(.)`. I also missed that you know the higher order derivatives of `x` as well. – PetrH Aug 09 '14 at 15:59

1 Answers1

1

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

  1. 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.
  2. 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.

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.

PetrH
  • 700
  • 1
  • 4
  • 12