Search code examples
matlabsymbolic-math

How to shorten this symbolic expression?


The symbolic expression below is the answer of some problem:

syms x y;
F = (6006059164170857*x^4)/36028797018963968 ...
    - (3741993627723215*x^3*y)/144115188075855872 ...
    - (3786059161694655*x^3)/576460752303423488 ...
    + (2057823154876729*x^2*y^2)/9007199254740992 ...
    + (7804706423002791*x^2*y)/36028797018963968 ...
    - (1579656551431947*x^2)/4503599627370496 ...
    - (5176864966130107*x*y^3)/576460752303423488 ...
    - (3350671128443929*x*y^2)/288230376151711744 ...
    - (2340405747630269*x*y)/72057594037927936 ...
    - (3122104315900301*x)/1152921504606846976 ...
    + (1757149312773205*y^4)/36028797018963968 ...
    - (5692299995057083*y^3)/576460752303423488 ...
    + (4054023049400589*y^2)/144115188075855872 ...
    - (434917661837037*y)/2251799813685248 ...
    - 2254148116991025/18014398509481984;

As you can see, it's too long to read, how could I shorten it to read easily?


Solution

  • vpa will do the numeric calculations as far as possible and returns the result with the precision defined by digits.

    See also latex for latex representation of you symbolic expression,

    digits(2) % Two digits precision
    latex(vpa(F))
    
    0.17\, x^4 - 0.026\, x^3\, y - \left(6.6\cdot 10^{-3}\right)\, x^3 + 0.23\, x^2\, y^2 + 0.22\, x^2\, y - 0.35\, x^2 - \left(9.0\cdot 10^{-3}\right)\, x\, y^3 - 0.012\, x\, y^2 - 0.032\, x\, y - \left(2.7\cdot 10^{-3}\right)\, x + 0.049\, y^4 - \left(9.9\cdot 10^{-3}\right)\, y^3 + 0.028\, y^2 - 0.19\, y - 0.13
    

    and pretty for nicer presentation in command window.

    pretty(vpa(F))
                                  3                                             3                                                    3 
            4          3     6.6 x          2  2         2           2   9.0 x y             2               2.7 x          4   9.9 y           2 
      0.17 x  - 0.026 x  y - ------ + 0.23 x  y  + 0.22 x  y - 0.35 x  - -------- - 0.012 x y  - 0.032 x y - ----- + 0.049 y  - ------ + 0.028 y  - 0.19 y - 0.13 
                                 3                                            3                                 3                   3 
                               10                                           10                                10                  10