Search code examples
matlabmatrixcomputationsimplification

Simplify matrix matlab


I'm making some matrix computation in matlab. What looks strange (to me) is that I get results like

(8700286382685973*cos(q5)*sin(q4))/9007199254740992 + sin(q5)*((43220913799951902644522757965203*cos(q4))/730750818665451459101842416358141509827966271488 - 291404338770025/1125899906842624)

but matlab does not simplify the result. I already tried to use functions like simplify, simple,fix but none of them gave the desired result.

Any suggestion on what function should I use?


Solution

  • As @Lucas suggested, you can use vpa and digits in matlab, for example if the expression above is A (sym) then:

     vpa(A,3) % digits is set to 3
    
     ans = 
    
      0.966*cos(q5)*sin(q4) + sin(q5)*(5.91e-17*cos(q4) - 0.259)
    

    And then you can either see the numbers for themselves and chop them, or use something like:

     function result = significant(x, n)
     % significant(x, n) rounds number x to n number of significant figures
    
     s = floor(log10(abs(x)));
     shift = 10^(n-1);
     mant = round(x*shift/(10^s)) / shift;
     result = mant * 10^s;