% Midpoint method
% Solving the ODE: y'=b y + c t; y(0)=1
clear;

%  Coefficients of equation y'=b y + c t
b=-2.;
c=1.;

% Initial and final times
tinit= 0.;
tmax=5.; 

% Number of time steps
Nt = 300;
dt = (tmax-tinit)/Nt;

% Initial condition
y(2)=1.;
t(2)=tinit;
y(1)=1.0-dt*(b*y(2)+c*t(2));
t(1)=tinit-dt;

% Time loop
for j=2:(Nt+1)
y(j+1)=y(j-1)+2.*dt*((b*y(j)+c*t(j)));
t(j+1)=tinit+(j-1)*dt;
end

plot(t,y)
title('Midpoint Method')
xlabel('T')
ylabel('y(t)')