How to save a symbolic equation in a txt file?
Ältere Kommentare anzeigen
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?
Akzeptierte Antwort
Weitere Antworten (2)
Ameer Hamza
am 28 Mär. 2020
Bearbeitet: Ameer Hamza
am 28 Mär. 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 Kommentare
Anirudh Chhabra
am 28 Mär. 2020
Ameer Hamza
am 28 Mär. 2020
Yes, it will also work for symbolic variables. You just need to load the file and it will appear in the workspace as a sym.
Anirudh Chhabra
am 28 Mär. 2020
Anirudh Chhabra
am 28 Mär. 2020
Walter Roberson
am 28 Mär. 2020
1 Stimme
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 Kommentare
Anirudh Chhabra
am 28 Mär. 2020
Anirudh Chhabra
am 28 Mär. 2020
Sagar Chirania
am 19 Apr. 2020
I have an issue where the results of the solver is in the form of rootsOf() and also have variables associated with it. matlabFunction() throws an error
"Code generation failed due to unexpected object of type 'RootOf'."
Do you any other function or method with which I can save the results?
Walter Roberson
am 19 Apr. 2020
What is the maximum degree of the variable being solved for in the RootOf? And are these the results of solve() or dsolve()? Also which MATLAB version are you using?
If they are from solve and maximum degree 4 then you can pass the MaxDegree option but the formula gets very long.
If they are from dsolve then you are not giving a chance to pass MaxDegree.
In r2019b new tools became available to post process symbolic expressions. For RootOf polynomial it becomes more plausible to create new code. But dsolve tends to use RootOf() in a different context that is more difficult to deal with.
Walter Roberson
am 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
am 20 Apr. 2020
Bearbeitet: Sagar Chirania
am 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?
Kategorien
Mehr zu Linear Algebra finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!