My 6x6 symbolic Jacobian matrix is massive, and matlabFunction( ) is having trouble writing the corresponding numerical function file.
Show older comments
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 Comments
I currently have a 6x6 symoblic Jacobian using the Symbolic Math Toolbox.
If you already have the (6x6) Jacobian matrix in symbolic form, why don't you just use "subs" to substitute numerical values (your stationary points) for the y-vector and apply "eig" to the resulting numerical matrix ?
Noob
on 28 Apr 2025
Matt J
on 28 Apr 2025
The problem with evaluating symbolically subs() is that it will be very slow. If the calculation needs to be repeated, this could be a major bottleneck.
I read something about garbage in garbage out.
If you have the Jacobian in symbolic form, you don't have garbage.
Since you only have a small number of points (or even a single point) where to evaluate the Jacobian, time is not a factor. So just proceed as I told you to proceed.
If J is your symbolic Jacobian, try
Jnum = double(subs(J,y,ynum))
eig(Jnum)
where ynum is the numerical point where you want to evaluate it and y is the name of your symbolic (6x1) solution vector.
That's all.
Torsten
on 28 Apr 2025
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.
Accepted Answer
More Answers (1)
Walter Roberson
on 28 Apr 2025
1 vote
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 Comments
Noob
on 28 Apr 2025
Walter Roberson
on 28 Apr 2025
J = jacobian(EXPRESSION);
ch = children(J);
numch = numel(ch);
jh = cell(size(J));
for K = 1 : numch
outfile = "jacob" + K + ".m";
jh{K} = matlabFunction( ch{K}, 'file', outfile, 'optimize', true, 'vars', LIST_OF_VARIABLES);
end
Now jh will be a cell array of function handles that you can call with numeric values.
nJ = zeros(size(J));
for K = 1 : numch
nJ(K) = jh{K}(var1, var2, var3, ...);
end
Walter Roberson
on 28 Apr 2025
"That makes 21 numerical files."
You have six equations, each differentiated by six variables, so you should end up with either 6 or 36 files, not 21.
Walter Roberson
on 28 Apr 2025
You should always specify the 'vars' parameter, as some of the expressions might happen to not involve all of the variables.
Your J41 and so on are scalar expressions; using 'Sparse', true is not going to help at all.
Noob
on 28 Apr 2025
Noob
on 28 Apr 2025
Noob
on 29 Apr 2025
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!