% euler forward for y' = 1-t+4y, y0=1

%clc; clear all;

T = 2 %final time

N = 40
N = 80
% N = 100
 N = 1000
% N = 10000
 N = 100000

h = T/N %step size
t = 0;  %initial time
y = 1;  %initial value at t=0

ye = zeros(N+1,1); ye(1) =1; %vector, saved solution
yy = zeros(N+1,1); yy(1) =1; %vector, exact solution

for i=1:N 
    fi = 1-t+4*y;
    y = y+h*fi;
    t = t+h;
    
    yy(i+1) = y; 
    ye(i+1) = t/4-3/16+19/16*exp(4*t);  
end

% comparision at time T
disp('exact solution at time T')
ye(N+1)
disp('numerical solution at time T') 
yy(N+1)
erroratT = abs(ye(N+1)-yy(N+1))


% comparision of the exact and numerical solutions
figure()
plot(0:h:T, yy,'--rs')
hold on;
plot(0:h:T, ye,'b*')



