matlabFunction option is very slow????

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??

Respuestas (1)

Walter Roberson
Walter Roberson el 6 de En. de 2016

0 votos

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 comentarios

Sukru
Sukru el 6 de En. de 2016
Actually I have different m files. such as waterprop.m gasprop.m etc Eq is a mass balance equation and which should equal to zero. I have 6 equations and 6 variables. When I convert to this to funciton handle with matlabFunction, I can easily solve parameter by using the newton raphson code I wrote. However converting from syms to matlab function takes a lot of time.
Walter Roberson
Walter Roberson el 6 de En. de 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?
Sukru
Sukru el 6 de En. de 2016
Editada: Sukru el 6 de En. de 2016
actually i need to check iteration times tolerance etc as it is in newton raphson and jacobian iteration.
Sanjay Kumawat
Sanjay Kumawat el 21 de Jun. de 2021
Similar problem is faced by me...any solution you came with? please share
Walter Roberson
Walter Roberson el 21 de Jun. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Symbolic Math Toolbox en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 6 de En. de 2016

Comentada:

el 21 de Jun. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by