%% eigenvalue method in Matlab

%% Example 1 
%% solve IVP
%% x1'=3*x1+4*x2
%% x2'=3*x1+2*x2
%% x1(0)=1
%% x2(0)=1
%% coefficient matrix
A=[3 4;3 2];
[P D]=eig(A)
%% columns of P contain eigenvectors
%% diagonal entries of D contain eigenvalues
%% so general solution is
%% x=c1*e^(6t)*[.8;.6]+c2*e^(-t)*[-.7071;.7071]
%% this is equivalent to
%% x=c1*e^(6t)*[4;3]+c2*e^(-t)*[-1;1]
%% plugging in t=0 gives
%% x(0)=[4 -1;3 1]*[c1;c2]
%% the IC is x(0)=[1;1]
%% so [c1;c2]=[4 -1;3 1]^(-1)*[1;1]
[4 -1;3 1]^(-1)*[1;1]
%% so the answer is
%% x=.2857*e^(6t)*[4;3]+.1429*e^(-t)*[1;1]
%% note .2857=2/7 and .1429=1/7
%% also, rescaling x (as in line 18) to make things look nicer is not necessary
%% sometimes it is easier to not rescale


%% Example 2
%% solve IVP
%% x1'=4*x1+1*x2+4*x3
%% x2'=4*x1+1*x2+4*x3
%% x3'=1*x1+7*x2+1*x3
%% x1(0)=1
%% x2(0)=2
%% x3(0)=3
%% coefficient matrix
B=[4 1 4;4 1 4;1 7 1];
[P2 D2]=eig(B)
%% dont't bother rescaling
%% general solution can be thought of as
%% x=c1*e^(9t)*(first col of P2)+c2*e^(0t)*(2nd col of P2)+c3*e^(-3t)*(3rd col of P2)
%%  =P2*[c1*e^(9t);c2*e^(0t);c3*e(-3t)]
%% so x(0)=P2*[c1;c2;c3]
%% want to solve x(0)=[1;2;3]
%% so [c1;c2;c3]=P2^(-1)*[1;2;3]
c=P2^(-1)*[1;2;3]
%% c1 times first column of P2 is
c(1)*P2(:,1)
%% c2 times second column of P2 is
c(2)*P2(:,2)
%% c3 times third column of P2 is
c(3)*P2(:,3)
%% solution is
%% x=e^(9t)*[2;2;2]+e^(0t)*[-1;0;1]+e^(-3t)*[0;0;0]


