Compute the change of basis matrix in Matlab

7.5k views Asked by At

I've an assignment where I basically need to create a function which, given two basis (which I'm representing as a matrix of vectors), it should return the change of basis matrix from one basis to the other.

So far this is the function I came up with, based on the algorithm that I will explain next:

function C = cob(A, B)
% Returns C, which is the change of basis matrix from A to B,
% that is, given basis A and B, we represent B in terms of A.
% Assumes that A and B are square matrices

n = size(A, 1);

% Creates a square matrix full of zeros 
% of the same size as the number of rows of A.
C = zeros(n);

for i=1:n
    C(i, :) = (A\B(:, i))';
end

end

And here are my tests:

clc
clear out

S = eye(3);
B = [1 0 0; 0 1 0; 2 1 1];
D = B;

disp(cob(S, B));  %  Returns cob matrix from S to B.
disp(cob(B, D));
disp(cob(S, D));

Here's the algorithm that I used based on some notes. Basically, if I have two basis B = {b1, ... , bn} and D = {d1, ... , dn} for a certain vector space, and I want to represent basis D in terms of basis B, I need to find a change of basis matrix S. The vectors of these bases are related in the following form:

(d1 ... dn)^T = S * (b1, ... , bn)^T

Or, by splitting up all the rows:

  d1 = s11 * b1 + s12 * b2 + ... + s1n * bn
  d2 = s21 * b1 + s22 * b2 + ... + s2n * bn
  ...
  dn = sn1 * b1 + sn2 * b2 + ... + snn * bn

Note that d1, b1, d2, b2, etc, are all column vectors. This can be further represented as

  d1 = [b1 b2 ... bn] * [s11; s12; ... s1n];
  d2 = [b1 b2 ... bn] * [s21; s22; ... s2n];
  ...
  dn = [b1 b2 ... bn] * [sn1; sn2; ... s1n];

Lets call the matrix [b1 b2 ... bn], whose columns are the columns vectors of B, A, so we have:

  d1 = A * [s11; s12; ... s1n];
  d2 = A * [s21; s22; ... s2n];
  ...
  dn = A * [sn1; sn2; ... s1n];

Note that what we need now to find are all the entries sij for i=1...n and j=1...n. We can do that by left-multiplying both sides by the inverse of A, i.e. by A^(-1).

So, S might look something like this

S = [s11 s12 ... s1n;  
     s21 s22 ... s2n; 
     ...
     sn1 sn2 ... snn;]

If this idea is correct, to find the change of basis matrix S from B to D is really what I'm doing in the code.

Is my idea correct? If not, what's wrong? If yes, can I improve it?

1

There are 1 answers

4
Leandro Caniglia On

Things become much easier when one has an intuitive understanding of the algorithm.

There are two key points to understand here:

  1. C(B,B) is the identity matrix (i.e., do nothing to change from B to B)
  2. C(E,D)C(B,E) = C(B,D) , think of this as B -> E -> D = B -> D

A direct corollary of 1 and 2 is

  1. C(E,D)C(D,E) = C(D,D), the identity matrix

in other words

  • C(E,D) = C(D,E)-1

Summarizing. Algorithm to calculate the matrix C(B,D) to change from B to D:

  1. Define C(B,E) = [b1, ..., bn] (column vectors)
  2. Define C(D,E) = [d1, ..., dn] (column vectors)
  3. Compute C(E,D) as the inverse of C(D,E).
  4. Compute C(B,D) as the product C(E,D)C(B,E).

Example

B = {(1,2), (3,4)}
D = {(1,1), (1,-1)}

C(B,E) = | 1  3 |
         | 2  4 |

C(D,E) = | 1  1 |
         | 1 -1 |

C(E,D) = | .5  .5 |
         | .5 -.5 |

C(B,D) = | .5  .5 | | 1 3 | = | 1.5  3.5 |
         | .5 -.5 | | 2 4 |   | -.5  -.5 |

Verification

1.5 d1 + -.5 d2 = 1.5(1,1) + -.5(1,-1) = (1,2) = b1
3.5 d1 + -.5 d2 = 3.5(1,1) + -.5(1,-1) = (3,4) = b2

which shows that the columns of C(B,D) are in fact the coordinates of b1 and b2 in the base D.