Search code examples
matlabsparse-matrixmatrix-inverse

Matrix inversion is difficult in matlab when deal with sparse matrix


I implement a algorithm which is related to sparse matrix inversion.

The code:

kapa_t=phi_t*F_x'*(inv(inv(R_t)+F_x*phi_t*F_x'))*F_x*phi_t;

I write down the code in matlab. It give me a warning Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 4.419037e-18.. But as per my algorithm matrix inversion is important part. So, I am trying to search some efficient way for matrix inversion.So I find out this link how to compute inverse of a matrix accurately?
So I changed my code as suggest.

kapa_t=phi_t*F_x'*(inv(inv(R_t)+F_x*phi_t*F_x'))\F_x*phi_t;

After that I get an error Error using \ Matrix dimensions must agree.

Error in EKF_SLAM_known (line 105) kapa_t=phi_tF_x'(inv(inv(R_t)+F_xphi_tF_x'))\F_x*phi_t;

The algorithm I am using is enter image description here

Here line no: 8 of the algorithm is equivalent to code kapa_t=phi_tF_x'(inv(inv(R_t)+F_xphi_tF_x'))F_xphi_t;

What should I do with my code to get rid of this warning.


Solution

  • kapa_t=phi_t*F_x'*(inv(inv(R_t)+F_x*phi_t*F_x'))\F_x*phi_t;
    

    should be

    kapa_t=phi_t*F_x'*((inv(R_t)+F_x*phi_t*F_x')\F_x)*phi_t;
    

    The A \ B operator is roughly equivalent to inv(A) * B when A is square, so you don't need the outer inv.