Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

substitute the derivative of a function in a symbolic expression

I have the same question as here.

In Matlab the derivative of a function can be represented symbolically as

>> syms t
>> syms x(t)
>> diff(x,t)

ans(t) =

D(x)(t)

But how can I substitute in an expression if, say, I know the derivative.

>> subs(ans,D(x)(t),3)
Error: ()-indexing must appear last in an index expression.
like image 664
Gus Avatar asked Mar 18 '23 20:03

Gus


2 Answers

Let's work through an example:

syms t x(t) y
f = x^2+y
dfdt = diff(f,t) % returns 2*D(x)(t)*x(t)
dxdt = diff(x,t) % returns D(x)(t)
subs(dfdt,dxdt,3)

which returns the 6*x(t). The key is that D(x)(t) is just the printed representation of the derivative with respect to time, not the actual value. You need to assign it to an actual variable. In your example you'd need to do what @rayryeng suggests, but it it's much more flexible and clearer if you assign names to your outputs.

Both x and dxdt are what the help and documentation for symfun call an "abstract" or "arbitrary" symbolic function, i.e., one without a definition. These behave a bit differently from regular symbolic variables of type sym. Type class(x) or whos in your command window to see your variable types.

like image 198
horchler Avatar answered Mar 20 '23 09:03

horchler


MATLAB interprets that you are doing a nested index call. It thinks that you are doing D(x), then whatever result of this you are indexing into the array with t. What you should do instead is nest a diff(x,t) call inside of the second parameter of subs, then do your substitution. Therefore:

subs(ans, diff(x,t), 3);
like image 36
rayryeng Avatar answered Mar 20 '23 10:03

rayryeng