% 1. Consider the differential equations dy/dt = 1/2 - t + 2y with y(0) = 1.
% a) (4 points) Find y(t) using Euler?s and Heun?s method by hand at t = 0.1, 0.2, 0.3,
% 0.4, and 0.5, using ?t = 0.1. Compare to the analytical solution y(t) = e2t + 0.5t.
% b) (4 points) In Matlab, Find y(t) at t=5 using Euler?s method for ?t = 0.4, 0.2,0.01,
% and 0.05. Find the error at each time step by comparison to the analytical solution.
% Put your code in the file H8Q1.m, and your Euler integration function in the M-function
% file euler_explicit.m

%% t = 0.4

[a, b, c, d] = euler_explicit(@(t, y) exp(2*t) + 0.5*t, @(t, y) 0.5 - t + 2*y, 1, 0, 10, 0.4);

disp(['The Explicit Euler estimation at ', num2str(10)]);
disp(['is: ', num2str(a)]);
disp(['The relative percent of error is (%) ', num2str(b)]);

%% t = 0.2

[a, b, c, d] = euler_explicit(@(t, y) exp(2*t) + 0.5*t, @(t, y) 0.5 - t + 2*y, 1, 0, 10, 0.2);

disp(['The Explicit Euler estimation at ', num2str(10)]);
disp(['is: ', num2str(a)]);
disp(['The relative percent of error is (%) ', num2str(b)]);

%% t = 0.01

[a, b, c, d] = euler_explicit(@(t, y) exp(2*t) + 0.5*t, @(t, y) 0.5 - t + 2*y, 1, 0, 10, 0.1);

disp(['The Explicit Euler estimation at ', num2str(10)]);
disp(['is: ', num2str(a)]);
disp(['The relative percent of error is (%) ', num2str(b)]);

%% t = 0.05

[a, b, c, d] = euler_explicit(@(t, y) exp(2*t) + 0.5*t, @(t, y) 0.5 - t + 2*y, 1, 0, 10, 0.05);

disp(['The Explicit Euler estimation at ', num2str(10)]);
disp(['is: ', num2str(a)]);
disp(['The relative percent of error is (%) ', num2str(b)]);

%%
% The Explicit Euler estimation at 10
% is: 2408870.9211
% The relative percent of error is (%) 98.895
% 
% The Explicit Euler estimation at 10
% is: 20248921.2398
% The relative percent of error is (%) 93.7737
% 
% The Explicit Euler estimation at 10
% is: 82817979.522
% The relative percent of error is (%) 79.1506
% 
% The Explicit Euler estimation at 10
% is: 189905281.4605
% The relative percent of error is (%) 56.741
