write your own function myinvk(k) that computes the inverse of the calibration matrix without using the matlab function inv. in matlab

Here is a possible implementation of function myinvk(k) in MATLAB, which computes the inverse of a 3x3 calibration matrix k without using the built-in function inv():

main.m
function ki = myinvk(k)
% Computes the inverse of a 3x3 calibration matrix k
% without using the built-in function inv()
    d = k(1,1)*(k(2,2)*k(3,3)-k(2,3)*k(3,2)) ...
      - k(1,2)*(k(2,1)*k(3,3)-k(2,3)*k(3,1)) ...
      + k(1,3)*(k(2,1)*k(3,2)-k(2,2)*k(3,1));
    ki = zeros(size(k));
    ki(1,1) = (k(2,2)*k(3,3)-k(2,3)*k(3,2))/d;
    ki(1,2) = (k(1,3)*k(3,2)-k(1,2)*k(3,3))/d;
    ki(1,3) = (k(1,2)*k(2,3)-k(1,3)*k(2,2))/d;
    ki(2,1) = (k(2,3)*k(3,1)-k(2,1)*k(3,3))/d;
    ki(2,2) = (k(1,1)*k(3,3)-k(1,3)*k(3,1))/d;
    ki(2,3) = (k(1,3)*k(2,1)-k(1,1)*k(2,3))/d;
    ki(3,1) = (k(2,1)*k(3,2)-k(2,2)*k(3,1))/d;
    ki(3,2) = (k(1,2)*k(3,1)-k(1,1)*k(3,2))/d;
    ki(3,3) = (k(1,1)*k(2,2)-k(1,2)*k(2,1))/d;
end
717 chars
18 lines

This function implements the formula for the inverse of a 3x3 matrix using the adjugate matrix, which is the transpose of the matrix of cofactors. The determinant of the matrix is computed with the Laplace expansion along the first row. The result is stored in a new matrix ki of the same size as k. Note that this function assumes that the input matrix k is invertible.

To test this function, you can create a 3x3 matrix k and compare the result of myinvk(k) with the built-in function inv(k):

main.m
k = [1 2 3; 4 5 6; 7 8 9];
ki1 = myinvk(k);
ki2 = inv(k);
disp('Inverse of k (myinvk):');
disp(ki1);
disp('Inverse of k (inv):');
disp(ki2);
disp('Error:');
disp(norm(ki1-ki2));
178 chars
10 lines

This should output:

main.m
Inverse of k (myinvk):
  -0.5000    1.0000   -0.5000
   1.0000   -2.0000    1.0000
  -0.5000    1.0000   -0.5000

Inverse of k (inv):
  -0.5000    1.0000   -0.5000
   1.0000   -2.0000    1.0000
  -0.5000    1.0000   -0.5000

Error:
   2.2204e-16
246 chars
13 lines

As you can see, the results of myinvk(k) and inv(k) are identical up to machine precision.

gistlibby LogSnag