matlabFunction option is very slow????
Show older comments
Hi Dear All, I have very long symbolic function with six symbolic variable (in the attachment). I need to convert this function to function handle before using it in newton-raphson equation.
A=matlabFunction(Eq); time is around 16 seconds. this is long when I have lots of grids in my equation.
Any solution to this??
Answers (1)
Walter Roberson
on 6 Jan 2016
0 votes
You either taylored a function to degree 42, or (more likely) you have an effectively junk formula triggered by trying to find a closed form solution to a equation involving a power which is a floating point number.
If you are attempting to solve() a formula that involves floating point numbers, then you are likely to get bogus formula. solve() is for exact solutions, and any floating point number you might have is unlikely to be exact unless it ends in .0, 0.5, 0.25 or 0.75. GIGO, Garbage In Garbage Out, and floating point numbers are more or less garbage when it comes to exact mathematics.
You should write known floating point constants in terms of rationals using sym(). Any input value that is a parameter to the formula you should use a symbolic placeholder for, solve the expression symbolically, and then substitute the actual value after the symbolic solution is found.
5 Comments
Sukru
on 6 Jan 2016
Walter Roberson
on 6 Jan 2016
If you are going to use junk formulae that are overly complicated then you need to expect it to take a long time.
Any particular reason you are not using vpasolve() instead of solve() followed by a numeric solver?
Sanjay Kumawat
on 21 Jun 2021
Similar problem is faced by me...any solution you came with? please share
Walter Roberson
on 21 Jun 2021
Sometimes it is faster to use a careful feval(symengine) to ask the internal symbolic programming language, MuPAD, to write out the function to a file as text, and then use a text editor to reformat the expression and rewrite MuPAD-specific portions into MATLAB format.
When I say "faster", I am talking about it might only take an hour and a half of editing with a good editor, instead of 5 hours of matlabFunction processing.
matlabFunction() is slower according to roughly the length of the expression to the power of 2.3. For longer expressions, that can be quite long.
This means that if you have a long symbolic expression, then if you can find a way to break it into sub-expressions, you might be able to save a lot of time. For example if you have something that you know is the sum() of a lot of terms, then use children() to extract the terms, and generate for them, and then use a function that sums the results of invoking the functions generated for the children.
Categories
Find more on Symbolic Math Toolbox 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!