My 6x6 symbolic Jacobian matrix is massive, and matlabFunction( ) is having trouble writing the corresponding numerical function file.

16 次查看(过去 30 天)
Hi there!
I currently have a 6x6 symoblic Jacobian using the Symbolic Math Toolbox. It is massive, when I take a peek at it using the Command Window. I then used matlabFunction( ) to convert this symbolic Jacobian to a numerical function. If I use matlabFunction's default optimize = true, the numerical function file is about 8,000 lines of code, and I get a message there that says the code is too complex to analyze, and that I should reduce the number of operations in my code. On the other hand, if I use matlabFunction's optimize = false option, the numerical function file is 59,506 lines of code, and I get a message there that says the file is too large. I then tried passing to matlabFunction( ), "Sparse", true, to try to get a sparse numerical Jacobian function file, but I still get the message in the numerical file that the code is too complex to analyze.
What can I do from here?
My goal is to successfully get a numerical Jacobian function file, then evaluate it to get a numerical Jacobian, and then find its eigenvalues using eig( ).
Thanks in advance,
  6 个评论
Torsten
Torsten 2025-4-28
If your ODE system is that complicated, you should completely avoid symbolic computations. Set up everything numerically right from the start - then you won't run into these problems you describe.
Maybe someone can help how to do this, but without at least knowing the mathematical model you try to simulate, it's almost impossible.
Noob
Noob 2025-4-28
编辑:Noob 2025-4-28
Hi Torsten!
Yes, working completely numerically is something I should consider in the near future perhaps.
What led me to work symbolically was because my equations are difficult in this sense:
Variables show up both on the LHS and RHS.
Symbolic math can handle this, such as x = x + 5 + y.
A numerical code couldn't handle this, and would say x is undefined.
The equal sign in a numerical code assigns numerical values to a variable.
If you want to share more of your thoughts, please feel free to do so.
Currently, I am separating the Jacobian and storing them into multiple variables, and I will then matlabFunction each of these smaller expressions.
I might also try using expand( ) followed by simplify( ), on all of the split-up Jacobian variables.
Thanks!

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2025-4-27
编辑:Matt J 2025-4-27
Maybe just compute the Jacobian numerically, by finite differences? If the analytical expressions for the Jacobian are that massive, I don't see how you can be any more confident in the numerical accuracy of the analytical derivative than a finite difference derivative.
  3 个评论
Noob
Noob 2025-4-29
Hi Matt!
After a few days of trying a lot of different things, I now think your advice is best to go with. I don't want to compromise my good workflow and make drastic changes to accommodate a massive symbolic Jacobian -- that seems to be the wrong direction to head in. So I will both look at the file you mentioned (it seems extremely popular) as well as try to learn to write a central-difference code myself.
Thanks again for your help!

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2025-4-28
Use children() to get a 6 x 6 cell array (or simply loop over the contents of the array); matlabFunction() each one seperately .
The time taken to use matlabFunction 'optimize' is roughly exponential in the size of the expression, so separating the expressions can help a lot.
If necessary, you can use children() on the individual expressions, if you are careful about how you put the pieces back together again.
  13 个评论
Noob
Noob 2025-4-28
编辑:Noob 2025-4-28
Hi Walter!
Different from my Simulation workflow, for my Stability workflow, I should immediately specify all parameter values (likely, using double-precision numbers) in order to reduce the number of symbolic variables. This seems to have resolved my issues.
Thanks!
Noob
Noob 2025-4-29
Hi Walter!
Just wanted to say thanks again! Today, I think I am deciding to compute the Jacobian function numerically, using a code from the File Exchange and / or write a central difference code myself. I'm afraid of making drastic changes to my workflow (which I've worked hard to debug over a long period of time) in order to accommodate a massive symbolic Jacobian.
Thanks again for your help!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by