How to do manipulation of derivatives for symbolic variables using the symbolic toolbox?
2 views (last 30 days)
Show older comments
I am having an issue using the 'equationsToMatrix' function in matlab on an equation which I believe to be linear, yet I am recieving an error saying that it is not.
The program I am working on is rather large, but I created the following code which replicates the issue I am having exactly. Further explainations of the issue I am having can be found below:
%MatLab_Ask
clc;clear;
syms s m K1 K2 F_s v_m v_K1 F_K1 F_K2
T = char(v_K1);
Var = char(1/K2);
A = char(F_K2);
%Create elemental equation
elem_eqn = str2sym([T ' = ' Var '* d(' A ')/dt']);
%Create continuity equation
cont_eqn = str2sym([char(F_K2) ' = ' char(F_s - F_K1)]);
%Sub continuity equation into elemental equation
D = subs(elem_eqn,lhs(cont_eqn),rhs(cont_eqn));
I think this may be the root of the issue. This gives:
UU = v_K1 == d(F_s - F_K1)/(K2*dt)
but I want matlab to see this as:
UU = v_K1 == (1/K2)*d(F_s)/(dt)-(1/K2)*d(F_K1)/(dt)
Matlab does not seem to see these as being the same.
%Create array of symbolic variables for derivatives of v_m and F_K1
d_x = sym(zeros(2,1));
d_x(1) = str2sym(['d(' char(v_m) ')/dt']);
d_x(2) = str2sym(['d(' char(F_K1) ')/dt']);
%Create symbolic variable for for derivative of F_s
d_u = str2sym(['d(' char(F_s) ')/dt']);
% Convert linear(?) equation UU to matrix form in terms of d_u and d_x
N = equationsToMatrix(rhs(D),d_u);
M = equationsToMatrix(rhs(D),d_x);
The expected outputs of these two lines should be something like:
N = [1/K2]
M = [0, -1/K2]
But, these lines both produce the following errors for N and M:
% Error using mupadengine/feval (line 187)
% Unable to convert to matrix form because the system does not seem to be linear.
%
% Error in sym/equationsToMatrix (line 61)
% T = eng.feval('symobj::equationsToMatrix',eqns,vars);
%
% Error in MatLab_Ask (line 32)
% N = equationsToMatrix(rhs(UU),d_u);
Sorry if my question is not very clear, but I am not exactly sure what the issue I am having is exactly.
Thank you for any help that you can provide.
0 Comments
Answers (1)
Star Strider
on 24 Feb 2019
I cannot follow what you apparently want to do with your code.
See the documentation on the symbolic diff (link) function. Rewrite your code using it, instead of attempting to define your derivatives as (for example):
elem_eqn =
v_K1 == d(F_K2)/(K2*dt)
That is never going to work.
2 Comments
Star Strider
on 24 Feb 2019
If you’re solving a linear state space system, see if using the symboilic expm (link) function does what you want. You can avoid specifying the derivatives (as derivatives) altogether.
It is in part the solution to:


(if I remember correctly).
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!