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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With