How to do manipulation of derivatives for symbolic variables using the symbolic toolbox?

2 views (last 30 days)
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.

Answers (1)

Star Strider
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
Eric McCormick
Eric McCormick on 24 Feb 2019
Edited: Eric McCormick on 24 Feb 2019
Hi, thanks for your response. I'll try to give a better explaination here.
%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));
Basically this first chunk of code is just setting up an equation in the following form:
So, in order to construct D, I first create an elemental equation:
and a continuity equation:
The continuity equation is then subbed into the elemental equation to get D:
%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
M = equationsToMatrix(rhs(D),d_x);
N = equationsToMatrix(rhs(D),d_u);
The second large chunk of code is setting up and , where:
and
Then, extracts Mand N from D using the equationsToMatrix function. The expected outcome of this are:
I have tried to use the diff function before as well, but it did not seem to work how I expected either. I will retry this though and see if it can fix my issue.
I hope this explaination clears up what I am trying to do. If you have any other advice it would be greatly appreciated!
Thanks!
Star Strider
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).

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!