0

I have the following code:

syms t x;
e=symfun(x-t,[x,t]);

In the problem I want to solve x is a function of t but I only know its value at the given t,so I modeled it here as a variable.I want to differentiate e with respect to time without "losing" x,so that I can then substitute it with x'(t) which is known to me. In another question of mine here,someone suggested that I write the following:

e=symfun(exp(t)-t,[t]);

and after the differentiation check if I can substitute exp(t) with the value of x'(t).

Is this possible?Is there any other neater way?

Controller
  • 489
  • 7
  • 27

1 Answers1

1

I'm really not sure I understand what you're asking (and I didn't understand your other question either), but here's an attempt.

Since, x is a function of time, let's make that explicit by making it what the help and documentation for symfun calls an "abstract" or "arbitrary" symbolic function, i.e., one without a definition. In Matlab R2014b:

syms t x(t);
e = symfun(x-t,t)

which returns

e(t) =

x(t) - t

Taking the derivative of the symfun function e with respect to time:

edot = diff(e,t)

returns

edot(t) =

D(x)(t) - 1

the expression for edot(t) is a function of the derivative of x with respect to time:

xdot = diff(x,t)

which is the abstract symfun:

xdot(t) =

D(x)(t)

Now, I think you want to be able to substitute a specific value for xdot (xdot_given) into e(t) for t at t_given. You should be able to do this just using subs, e.g., something like this:

sums t_given xdot_given;
edot_t_given = subs(edot,{t,xdot},{t_given, xdot_given});

You may not need to substitute t if the only parts of edot that are a function of time are the xdot parts.

Community
  • 1
  • 1
horchler
  • 18,384
  • 4
  • 37
  • 73
  • Just one more question... Trying to automate the process of computing derivatives of x,I tried doing the following: dx=[diff(x,t);diff(x,t,2)]; subs(de1,{dval,q},{[2;2],pi/6}) where de1 has also second order derivatives of x... But it doesn't work...How can I do that? To be more precise I don't want to have to write xdot = diff(x,t),xdoubledot=diff(x,t,2) because I might have high order derivatives and many states x1,x2,etc.. – Controller Aug 11 '14 at 08:41
  • @PavlosTriantafyllou: I don't know what you're asking. I don't see how your `dx` array relates to your `subs` call which doesn't use `dx`. This seems like a separate question. Also, for future reference, saying "But it doesn't work" is not helpful on StackOverflow (or elsewhere). Error messages, expected vs. actual outputs are helpful. – horchler Aug 11 '14 at 15:25
  • A million sorry... There was a typo there...I meant: `dx=[diff(x,t);diff(x,t,2)]; subs(de1,{dx,q},{[2;2],pi/6})` – Controller Aug 11 '14 at 15:34
  • 1
    Use cell arrays, not arrays. Something like this: `dx={diff(x,t);diff(x,t,2)};` `dxval={2,2};` `subs(de1,{dx{:},q},{dxval{:},pi/6})`. – horchler Aug 12 '14 at 01:49