%% Question 2
% We consider the linear ODE $x'=Ax$ where
%
% $$
% A = \left(\begin{array}{ccc}
% -3 & 0 & 0 \\ 0 & -3 & 4 \\ 0 & -2 & 1
% \end{array}\right)
% $$
%
% We can determine whether the stable state $(0,0)$ is by analysing the eigenvalues.
%
% $$0 =\det(A-\lambda I) = (-\lambda-3)((-3-\lambda)(1-\lambda)+8) = (-\lambda-3)(\lambda^2+2\lambda+8)$$
%
% We get that, $\sigma(A) = \{-3,-1+2i,-1-2i\}$, and hence the stable state is A-stable as
%
% $$ \max_{\lambda\in\sigma(A)} Re(\lambda)<0.$$
%
% We now try to determine valid values of $\tau$ to ensure the fixed point $(0,0)$
% of first, second, third, and fourth order Runge-Kutta methods are A-stable.
%
A = [ -3  0 0; 0 -3 4; 0 -2 1];

figure;
hold on;
lambda=1;
plot(lambda*eig(A), 'ko', 'MarkerSize', 14);
lambda=0.9;
plot(lambda*eig(A), 'go', 'MarkerSize', 14);
lambda=0.8;
plot(lambda*eig(A), 'mo', 'MarkerSize', 14);
lambda=0.65;
plot(lambda*eig(A), 'bo', 'MarkerSize', 14);
lambda=0.4;
plot(lambda*eig(A), 'ro', 'MarkerSize', 14);

rk_stab(1, 'r.');
rk_stab(2, 'b.');
rk_stab(3, 'm.');
rk_stab(4, 'g.');
xlim([-4 4]);
ylim([-4 4]);
axis('square');
xlabel('Re');
ylabel('Im');
legend('1\lambda','0.9\lambda','0.8\lambda','0.65\lambda', '0.4\lambda', ...
    'First Order RK', 'Second Order RK', 'Third Order RK', 'Fourth Order RK');

%%
% In order for the various Runge-Kutta methods to have an A-stable fixed point
% then $\tau$ needs be less than roughly the following values for each method:
%
%   Order (s) | tau
%  -----------+-----
%       1     | 0.4
%       2     | 0.65
%       3     | 0.8
%       4     | 0.9
