%% Example 4.6
% Runge-Kutta

%% Setup
close all;
clear h w Tmax g T y_true f yex;

h = 0.15;
w = 4;
Tmax = 6;

%% Analyic solution
g = @(x) cos(w*x);
T = linspace(0,6,100);
y_true = g(T);

%% Function
f = @(t,y) [y(2); -16*y(1)];

%% Use Runge-Kutta code
[t y2] = rk2(f,[0 6],[1 0]',40);
[t y4] = rk4(f,[0 6],[1 0]',40);
yex = exp(4*i*t);

%% Plot
figure(1);
plot(t,y2(1,:),'--')
hold on
plot(t,y4(1,:),'^:')
plot(T,y_true)
grid on
axis([0 6 -2 3])
xlabel('t')
ylabel('y(t)')
legend('2nd O Runge-Kutta','4th O Runge-Kutta','Exact','Location','NorthWest')
