Search code examples
matlabsymbolic-math

Maximal numerical simplification of a symbolic expression


When I enter this code in Mathematica

t^3 + 6898033663572324079/6917529027641081856 // N

it simplifies expression into 0.997182 + t^3.

How do I achieve the same behavior in MATLAB? Note that the t is a symbolic variable.


Solution

  • Use vpa:

    syms t
    outExact = t^3 + 6898033663572324079/6917529027641081856
    outFloat = vpa(outExact)
    

    outExact =
    
    t^3 + 8981814666109797/9007199254740992
    
    
    outFloat =
    
    t^3 + 0.99718174452310093247575650821091
    

    optional you can set the number of digits with e.g. digits(5) to get:

    outFloat =
    
    t^3 + 0.99718
    

    or outFloat = vpa(outExact,5) which has the same effect.


    The scientific notation is not trivial, here is a non-generic solution as a starting point:

    f(t) = t^3 + 6898033663572324079/6917529027641081856
    remain = f(0)
    f(t) = f(t) - remain
    sprintf('%s + %1.3e',char(f(t)),double(remain))