Speed up MatlabFunction or use alternatives
Show older comments
Hello,
I am currently processing a big equation (in fact a 36 - Vector with ~15.000 characters each) and am trying to get a function using MatlabFunction. The goal is to receive a function which can be evaluated as quickly as possible, as it's part of a system solved by an ODE solver. The equation itself only contains polynomials to the degree of 3 and square-roots (^3/2 e.g.) but no sin, cos, etc. . Calling MatlabFunction took longer than 18 Hours, and now I had to change the equation and I really don't want to wait for the full 18 Hours. Is setting optimize to false advisable, or will this make my evaluation time worse? Are there other options beside MatlabFunction? I am not very familiar with this kind of stuff as I never had to deal with calculations taking so long.
Any help is greatly appreciated!
23 Comments
darova
on 17 Apr 2020
Can you replace your equations? Did you try fitting (fit function)?
Finn Busch
on 19 Apr 2020
Finn Busch
on 19 Apr 2020
Finn Busch
on 19 Apr 2020
darova
on 19 Apr 2020
Can show your formulas or code?
John D'Errico
on 19 Apr 2020
Far too often, these huge things with endless equations result in something mathematically and numerically useless - not to be trusted. It looks impressive, but so what? There may be numerical problems just evaluating that mess. Massive internal subtractive cancellation is not uncommon, so that even while a given computation is mathematically correct, it is numerical garbage.
Can you usea GPU or parallel computations to do something? Sadly not. MatlabFunction is a symbolic tool, it does not work well in parallel. GPUs & parallel tools are GREAT when you need to do the same thing over and over again. Not here.
As I said, it looks impressive. It sounds impressive - taking 18 hours just to process. Computers are great, right?
Good luck. Get some coffee while you wait, and read a good, LONG book.
Finn Busch
on 19 Apr 2020
Edited: Finn Busch
on 19 Apr 2020
darova
on 19 Apr 2020
I don't want it if it's too big (more 1mb)
Finn Busch
on 19 Apr 2020
darova
on 19 Apr 2020
Please attach them here!
Finn Busch
on 19 Apr 2020
darova
on 19 Apr 2020
How long does it take to evaluate? 18h?
darova
on 19 Apr 2020
I could'n handle that. I'm sorry
And what is wrong with numerical calculations? Can i help you with them?
Finn Busch
on 19 Apr 2020
darova
on 19 Apr 2020
I think there are no significant errors or mistakes. I think those calculations should be numerical. It's madness for symbolic evaluations. I can't even open dude variable!
Finn Busch
on 19 Apr 2020
Edited: Finn Busch
on 19 Apr 2020
John D'Errico
on 19 Apr 2020
Finn - you misunderstand. Nobody ever said you were bragging. My point is it is too easy to get hung up on the idea that a computer produced these equations, therefore they are correct, and anything that comes from them is meaningful. Remember that any model is merely an approximation of some process. A simpler model may be far more useful, because it is possible to make vaild predictions using it. The art then comes down to which terms are safely excluded.
Anyway, when your code eventually runs, it will produce a result, SOME prediction. You need to heavily validate what you get. Its that result meaningful in any way? Is it consistent with reality? All code like this is a model of some process. But did your model predict something that seems reasonable? The first filter is your eye, as you should know what is realistic. That is not a proof of validity, but just an aid to intuition that you may have done something reasonable.
If you just accept the result as "truth" becuase a computer produced it, then you are asking for trouble. Computers are not infallible. They just do what you ask them to do. As far as you can, then make more conclusive tests to verify the result. It is often true that you can find ways to drive even a complex system to produce an expected simple result. Does your model verify that result?
As I said, don't trust the results from such a complex computation, not until you can verify the computations.
darova
on 19 Apr 2020
What about subs? How much does matlabFunction take for calculations?
vars1 = [e(:)', [a, b, h], [x, y, z],lam,mu];
tic
A = subs(dude, vars1, 2*rand(size(vars1)));
double(A)
toc
% Elapsed time is 30.214898 seconds.
Finn Busch
on 19 Apr 2020
darova
on 19 Apr 2020
Im out of ideas. I give up
Finn Busch
on 19 Apr 2020
Franz Enkelmann
on 28 Nov 2022
Have you just found a solution?
Answers (1)
cd(tempdir)
syms a b c d positive
syms x
s = solve(a*x^3+b*x^2+c*x+d == 0, x, MaxDegree=3);
s is a pretty complicated expression, but the final answer can be simplified through the use of temporary sub-expressions. But that simplification can result in longer files (albeit with simpler expressions and shorter lines) and longer time creating the files.
tic
matlabFunction(s, File='cubicSolver1.m', Optimize=true);
toc
dbtype cubicSolver1.m
tic
matlabFunction(s, File='cubicSolver2.m', Optimize=false);
toc
dbtype cubicSolver2.m
Categories
Find more on Numeric Solvers in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!