问题
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.
回答1:
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 @rayeng 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.
回答2:
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);
来源:https://stackoverflow.com/questions/26997954/substitute-the-derivative-of-a-function-in-a-symbolic-expression