Question 1(b) - Pendulum
Comparison of Euler, Runge, and Runge-Kutta for to ode23
Contents
Initial Setup
k=1; pendulum = @(t,x)[x(2); -k*sin(x(1))]; t0 = 0; T = 6*pi;
x0 = [-1.5 0]; [t,x]=ode23(pendulum, [t0, T], x0); fig = figure; plot(t,x(:,1),'-k','DisplayName','ode23'); hold on; xlabel('time'); ylabel('x'); [t,x]=eul(pendulum, t0, T, x0, 1/2); plot(t,x(:,1),'-ro','DisplayName','Euler (\tau=1/2)'); [t,x]=eul(pendulum, t0, T, x0, 1/4); plot(t,x(:,1),'-rx','DisplayName','Euler (\tau=1/4)'); [t,x]=eul(pendulum, t0, T, x0, 1/8); plot(t,x(:,1),'-r+','DisplayName','Euler (\tau=1/8)'); [t,x]=runge(pendulum, t0, T, x0, 1/2); plot(t,x(:,1),'-bo','DisplayName','Runge (\tau=1/2)'); [t,x]=runge(pendulum, t0, T, x0, 1/4); plot(t,x(:,1),'-bx','DisplayName','Runge (\tau=1/4)'); [t,x]=runge(pendulum, t0, T, x0, 1/8); plot(t,x(:,1),'-b+','DisplayName','Runge (\tau=1/8)'); [t,x]=rk_classical(pendulum, t0, T, x0, 1/2); plot(t,x(:,1),'-mo','DisplayName','Runge-Kutta (\tau=1/2)'); [t,x]=rk_classical(pendulum, t0, T, x0, 1/4); plot(t,x(:,1),'-mx','DisplayName','Runge-Kutta (\tau=1/4)'); [t,x]=rk_classical(pendulum, t0, T, x0, 1/8); plot(t,x(:,1),'-m+','DisplayName','Runge-Kutta (\tau=1/8)'); legend('Location','NorthEastOutside'); ylim([-5, 5]);

Due to the oscillations in the solution Euler (especially for low ) quickly diverges from the solution. Runge is better, but towards the end it is still slightly divergent; whereas, Runge-Kutta seems good for all
.
x0 = [-3 0]; [t,x]=ode23(pendulum, [t0, T], x0); fig = figure; plot(t,x(:,1),'-k','DisplayName','ode23'); hold on; xlabel('time'); ylabel('x'); [t,x]=eul(pendulum, t0, T, x0, 1/2); plot(t,x(:,1),'-ro','DisplayName','Euler (\tau=1/2)'); [t,x]=eul(pendulum, t0, T, x0, 1/4); plot(t,x(:,1),'-rx','DisplayName','Euler (\tau=1/4)'); [t,x]=eul(pendulum, t0, T, x0, 1/8); plot(t,x(:,1),'-r+','DisplayName','Euler (\tau=1/8)'); [t,x]=runge(pendulum, t0, T, x0, 1/2); plot(t,x(:,1),'-bo','DisplayName','Runge (\tau=1/2)'); [t,x]=runge(pendulum, t0, T, x0, 1/4); plot(t,x(:,1),'-bx','DisplayName','Runge (\tau=1/4)'); [t,x]=runge(pendulum, t0, T, x0, 1/8); plot(t,x(:,1),'-b+','DisplayName','Runge (\tau=1/8)'); [t,x]=rk_classical(pendulum, t0, T, x0, 1/2); plot(t,x(:,1),'-mo','DisplayName','Runge-Kutta (\tau=1/2)'); [t,x]=rk_classical(pendulum, t0, T, x0, 1/4); plot(t,x(:,1),'-mx','DisplayName','Runge-Kutta (\tau=1/4)'); [t,x]=rk_classical(pendulum, t0, T, x0, 1/8); plot(t,x(:,1),'-m+','DisplayName','Runge-Kutta (\tau=1/8)'); legend('Location','NorthEastOutside'); ylim([-5, 5]);

Runge is now also diverging from the solution, and even Runge-Kutta has some error.
x0 = [-pi 1]; [t,x]=ode23(pendulum, [t0, T], x0); fig = figure; plot(t,x(:,1),'-k','DisplayName','ode23'); hold on; xlabel('time'); ylabel('x'); [t,x]=eul(pendulum, t0, T, x0, 1/2); plot(t,x(:,1),'-ro','DisplayName','Euler (\tau=1/2)'); [t,x]=eul(pendulum, t0, T, x0, 1/4); plot(t,x(:,1),'-rx','DisplayName','Euler (\tau=1/4)'); [t,x]=eul(pendulum, t0, T, x0, 1/8); plot(t,x(:,1),'-r+','DisplayName','Euler (\tau=1/8)'); [t,x]=runge(pendulum, t0, T, x0, 1/2); plot(t,x(:,1),'-bo','DisplayName','Runge (\tau=1/2)'); [t,x]=runge(pendulum, t0, T, x0, 1/4); plot(t,x(:,1),'-bx','DisplayName','Runge (\tau=1/4)'); [t,x]=runge(pendulum, t0, T, x0, 1/8); plot(t,x(:,1),'-b+','DisplayName','Runge (\tau=1/8)'); [t,x]=rk_classical(pendulum, t0, T, x0, 1/2); plot(t,x(:,1),'-mo','DisplayName','Runge-Kutta (\tau=1/2)'); [t,x]=rk_classical(pendulum, t0, T, x0, 1/4); plot(t,x(:,1),'-mx','DisplayName','Runge-Kutta (\tau=1/4)'); [t,x]=rk_classical(pendulum, t0, T, x0, 1/8); plot(t,x(:,1),'-m+','DisplayName','Runge-Kutta (\tau=1/8)'); legend('Location','NorthEastOutside'); ylim([-5, 5]);

All solutions are fairly good.