%% Runge-Kutta (RK) Methods
% This module is intended to be read after the relevant lectures.
%% Introduction: Beyond quadrature 
% |FUNDAMENTAL POINT|: RK methods are inspired by quadrature.
%
% Suppose the equation of interest is 
% $\mathbf{y}^\prime = \mathbf{f}(t,\mathbf{y})$
%
%% RHS a function of time only
% If the right-hand side is a function of $t$ only (and NOT of
% $\mathbf{y}$) then the problem reduces to a simple integration, for which
% quadrature is appropriate. Consider $f(t)=a \exp(-a t)$ with
% $y(0)=0$; the exact solution is $y(t)=1-exp(-a t)$. We have the following
% numerical results using the common RK4 scheme detailed in [1] 
% (with $a=10$):
a=10;
rk_test(@(t,y)(a*exp(-a*t)),@(t)(1-exp(-a*t)),0,0,3,7);

%%
% The 'semi-exact' scheme is RK except that it cheats slightly by using
% the exact $\mathbf{y}$ whenever we evaluate $\mathbf{f}$. 
%
% Here however, 
% $\mathbf{f}$ is independent of $\mathbf{y}$ and so the 'semi-exact' 
% scheme agrees with the RK scheme exactly. Due to the error in the 
% quadrature scheme on which the RK scheme
% is based, both solutions deviate from the exact solution (we use a large
% step size for illustration).

%% The general case
% However, in the general case, $\mathbf{f}(t,\mathbf{y})$ varies with $\mathbf{y}$;
% now we cannot simply use a quadrature rule, because we don't know the
% values of $y$ to use in $\mathbf{f}(t,\mathbf{y})$ between the current 
% and next time-steps!
%
%% The basic idea of RK
% The *essence* of RK methods is generalising quadrature by devising 
% appropriate approximations to $\mathbf{y}(t)$. Of course, the devil is in
% the details, and the approximations are of a certain fixed form as
% explained in lectures.
%
%%
% The outcome is that there are two approximations in RK methods:
%
% # The underlying quadrature approximation.
% # The approximation to $\mathbf{y}$ used for $\mathbf{f}(t,\mathbf{y})$.

%% A comparison in the general case
% We consider now $f(y,t)=-b y^2$ with $y(0)=1$. The exact
% solution is $y(t)=\frac{1}{5t+1}$. With $b=5$:
b=5;
y0=1;
rk_test(@(t,y)(-b.*y^2),@(t)(1./(b.*t+1/y0)),y0,0,3,11);

%%
% In this case, we can see the error introduced by approximating
% $\mathbf{y}$. 
%
% Fortunately, the approximations are chosen such that the method has 
% order 4, so the error decays quite quickly with step size. 
% (Again, we have chosen a large step size for illustration - otherwise the
% various solutions become indistinguishable!)


%% REFERENCES
% [1]:
% <http://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods#Common_fourth-order_Runge.E2.80.93Kutta_method (Wikipedia) Runge-Kutta Methods>

%% Code
%
% * <rk_test.m> (Function used above)


