How to convert an expression to a function with lossless precision?

3 visualizaciones (últimos 30 días)
I use matlabFunction to convert an expression to an anonymous function, but the convertion of matlabFunction has a loss of precision. Is there a lossless conversion method? If not, is there any conversion method to improve accuracy?
  4 comentarios
Chenguang Yan
Chenguang Yan el 27 de Sept. de 2020
I read the answer to the following question. The conversion of matlabFucntion did lose accuracy, but I did not find a solution to the problem
Ameer Hamza
Ameer Hamza el 27 de Sept. de 2020
As Walter mentioned in his answer, you will lose precision once you convert from symbolic maths to finite-precision. To get exact results, you must stick to variable precision or symbolic mathematics.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 27 de Sept. de 2020
Is there a lossless conversion method?
NO, there is no lossless conversion method.
A few years ago I encountered a paper that showed an explicit function that had the property that for any given finite numeric precision, there were inputs to the function for which the error due to rounding could be made arbitrarily large compared to the actual value of the function at the location. The paper showed that no matter how many guard digits you used for rounding purposes, the rounding error could be arbitrarily large relative to the correct value.
I do not recall now what the demonstration function used was, but it was not a complicated function. Something along the lines of x+(1/x) but a bit more developed.
I have myself encountered nonlinear equalities that when I evaluated to a million digits, I still could not decide if the equality was true. The symbolic manipulations said that the equalities held, but because the expressions were so different I was trying to verify whether the symbolic package had made a conversion mistake... and I couldn't decide based on numeric techniques. The rounding issues even at a million digits were substantial.
So, NO. There cannot be any finite numeric technique that can give enough precision to solve arbitrary nonlinear least squares.
  6 comentarios
Chenguang Yan
Chenguang Yan el 28 de Sept. de 2020
Thank you very much for your reply, which has benefited me a lot. I will reconsider my question based on your suggestions.
When using the solution algorithms provided by matlab, does the following statement hold true?
Statement: When functionTolerance == 0 is used as the only iteration termination condition and the solution is obtained, the solution obtained is the same as the exact solution under double precision.
Walter Roberson
Walter Roberson el 28 de Sept. de 2020
The truth of that statement would depend on which solver you were using. Most of the Mathworks ones do not permit you to turn off everything else and use exact 0 as the termination. Solvers would, in many cases, narrow down to the double precision numbers that are on either side of the exact solution, and if exact 0 residue was required and everything else was turned off for termination, the solver would then cycle infinitely between the two adjacent numbers.

Iniciar sesión para comentar.

Más respuestas (2)

madhan ravi
madhan ravi el 27 de Sept. de 2020
vpa(f_func(sym(sol1), sym(sol2)))
  3 comentarios
madhan ravi
madhan ravi el 27 de Sept. de 2020
When you use matlabFunction() , it converts it into double precision, why not use subs()?
Chenguang Yan
Chenguang Yan el 27 de Sept. de 2020
This expression represents a nonlinear least squares problem, and I need to convert the expression into an objective function (the global minimum value is zero, and I don’t know the values of sol1 and sol2 in the actual problem). However, the precision loss of matlabFunction() means that even if I find the global minimum value, the corresponding value will not be the same as sol1 and sol2.

Iniciar sesión para comentar.


Chenguang Yan
Chenguang Yan el 27 de Sept. de 2020
Editada: Chenguang Yan el 27 de Sept. de 2020
I uploaded the compressed package, which includes a .mat file and a .m file. After decompression, the problem should be reproduced by executing the .m file.

Community Treasure Hunt

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

Start Hunting!

Translated by