I am trying to make a code that matches the eigenvalues to eigenvectors for one of my projects and I am new to MatLab. I get a 3x3 matrix output when it comes to eigenvalues and a 3x2 when it comes to eigenvectors. I know the issues lies within the fact that one of my eigenvalues is a repeat, but I cannot find any resources telling me how to fix this. My V should definitely be a 3x3 matrix unless I have completely done this wrong when I did it by hand.
When n=2, my answer is how I want and my code works.
% Case n=2.
clear
syms k1 c1 c2 lambda t
A=[-k1 0; k1 0];
[V,D]=eig(A) %V is the eigenvectors, D is the eigenvalues.
p0=[1 0];
eqn11 = c1*V(1,1)+c2*V(1,2) == p0(1,1);
eqn12 = c1*V(2,1)+c2*V(2,2) == p0(1,2);
solcon = solve([eqn11,eqn12], [c1, c2]);
c1Sol=solcon.c1;
c2Sol=solcon.c2;
exp(D(1,1)*t)*c1Sol*V(:,1) + exp(D(2,2)*t)*c2Sol*V(:,2)
However, when n=3, I get an error.
% Case n=3.
clear
syms k1 c1 c2 c3 lambda t
B=[-k1 0 0; k1 -k1 0; 0 k1 0]
[V,D]=eig(B) %V is the eigenvectors, D is the eigenvalues.
p0=[1 0 0];
eqn21 = c1*V(1,1)+c2*V(1,2)+c3*V(1,3) == p0(1,1);
eqn22 = c1*V(2,1)+c2*V(2,2)+c3*V(2,3) == p0(1,2);
eqn23 = c1*V(3,1)+c2*V(3,2)+c3*V(3,3) == p0(1,3);
solcon = solve([eqn21, eqn22, eqn23], [c1, c2,c3]);
c1Sol=solcon.c1;
c2Sol=solcon.c2;
c3Sol=solcon.c3;
exp(D(1,1)*t)*c1Sol*V(:,1) + exp(D(2,2)*t)*c2Sol*V(:,2)+exp(D(3,3)*t)*c3Sol*V(:,3)
V should be a 3x3, right? Please help. Perhaps my code for n=2 is wrong as well and I just lucked out.
The "secret" is to use the kernel of $B-\lambda_k I$ for the different eigenvalues $\lambda_k$.
Let me explain it on a numerical example :