How to save a symbolic equation in a txt file?

52 views (last 30 days)
I have a complex system of equations for the dynamics of a rigid body.
I want to store the resultant matrix in a txt file (or any other format that works well) so that I do not have to compute the symbolic expression inside a numerical solver.
How do I store it in a proper format and then how do I read it?

Accepted Answer

Walter Roberson
Walter Roberson on 28 Mar 2020
save('test.mat', 'A', 'a', 'b', 'c');
When you use
syms a
that is equivalent to
a = sym('a');
That calls into the symbolic engine to create a variable named a inside the symbolic engine, and return a reference to the variable. The reference will look something like '_symans_[[32,0,43]]' . That reference will be recorded inside a symbolic object and that object will be assigned to the variable named a at the MATLAB level. The MATLAB level variable named a is not itself something that lives inside the symbolic engine; it is just that '_symans_[[32,0,43]]' that was returned by the symbolic engine.
When symbolic operations are done on a, such as a+1 then the symbolic toolbox pulls out that internal reference _symans_[[32,0,43]] and passes that to the symbolic engine along with the '+1', so it would look something like
evalin(symengine, '_symans_[[32,0,43]]+1')
The symbolic engine looks up _symans_[[32,0,43]] in its tables, find the appropriate expression, does the calculation, and returns a reference to a new symbolic variable, such as '_symans_[[32,0,89]]'
When you construct A from [a b c] then what would happen would be that the symbolic engine would get passed something like
evalin(symengine, '_DOM_LIST([_symans_[[32,0,43]], _symans_[[32,0,89], _symans_[[-7,5,18]]])')
and it would construct the list and return yet another internal reference. Notice that it is not getting passed the names of MATLAB variables, only references to expressions that live in the symbolic engine. A would become a symbolic reference to content that MATLAB does not know about until it asks the symbolic engine for the content.
When you save() A, MATLAB would ask the symbolic engine to return a parse-tree of symbolic expressions that can later be executed inside the symbolic engine to re-create the expression. It does not just ask for a printable version of the expression because in symbolic expressions, what appears to be the same variable is not always the same variable, especially when you get into nested structures.
Then you clear the variables. When you do so, the MATLAB level variables a, b, c stop existing, and information connecting them to (say) _symans_[[32,0,43]] is gone. You load symbolic variable A and it recreates references inside the symbolic engine to symbolic variables a, b, c that live in the symbolic engine, and A would get a new _symans reference. But that does not recreate the connection between the MATLAB variables a, b, c and the symbolic engine.
The situation is much like if you had done
a = 1; b = 2; c = 3;
A = [a, b, c];
and saved A and cleared the workspace and restored A, and if you then expected that a and b and c were also automatically restored, on the basis that A referred to them. No, A does not refer to them, MATLAB copied their values into A and throws away the information about where it got the values. Likewise when you have symbolic variables installed, MATLAB copies their values -- but the values of symbolic variables are the references into the symbolic engine, not the connection between the local variables of that name and whatever is inside the symbolic engine.
Consider for example if you had done
syms a b c
A = [a*2;b*0;c+5];
save('test.mat', 'A');
then upon the load() would you expect a and b and c to be restored? A doesn't even contain a reference to symbolic engine variable b -- the b*0 is 0 before you even get around to constructing A. MATLAB does not take a record of the variable names used to construct an expression and the expression that was used, only a copy of the evaluated values.
  6 Comments
Anirudh Chhabra
Anirudh Chhabra on 29 Mar 2020
Should I be saving all my variables with my expression to avoid this? (as below)
save('test.mat', 'A', 'a', 'b', 'c');
Ameer Hamza
Ameer Hamza on 29 Mar 2020
Anirudh, can you show a simple example to recreate the error with the matlabFunction? Also can you specify your MATLAB release?

Sign in to comment.

More Answers (2)

Ameer Hamza
Ameer Hamza on 28 Mar 2020
Edited: Ameer Hamza on 28 Mar 2020
You can save the variables in a .mat file which is the MATLAB's own file type to save data.
save('filename', 'variableName');
Then you can load it back to the workspace
load('filename')
  4 Comments
Anirudh Chhabra
Anirudh Chhabra on 28 Mar 2020
I can load it and find it the workspace as type sym. But in a new program, the variables/symbols have been cleared.
So now how do I substitute numeric values into those symbols which make up the original expression?
syms a b c
A = [a;b;c];
save('test.mat', 'A');
clear;
load('test.mat')
subs(A,a,2)
Error: Undefined function or variable 'a'.

Sign in to comment.


Walter Roberson
Walter Roberson on 28 Mar 2020
If you plan to use the expression inside a numeric solver, you should use matlabFunction(), which has the advantage of converting symbolic expressions into numeric ones.
If this involves differential equations that you are passing through one of the ode* functions, then you should see odeFunction(), and be sure to read the first example there, as it has valuable information on suggested workflow for converting symbolic ode into numeric ones.
  6 Comments
Walter Roberson
Walter Roberson on 19 Apr 2020
In particular, dsolve() tends to generate situations involving the sum of an expression with a variable being all of the possible roots of some expression; it uses RootOf to do that. But the RootOf that it generates are not necessarily roots of a polynomial, and can be nonlinear, and finding all of the roots (including the complex ones) of an arbitrary expression can be difficult.
Sagar Chirania
Sagar Chirania on 20 Apr 2020
Edited: Sagar Chirania on 20 Apr 2020
I am not dealing with differential equations. These are the results of solve(). The maximum degree is 8.
I am solving a set of 6 coordinate geometric equations. There is an unknown symbolic variable t in the solution that changes depending on position of particular object.The matlabFunction(), as I told throws the error and I cannot store the solution too, as when I load it again, there is no reference to t.
Do you have any method in particular or would I have to solve it for t always?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!