%% A
%Sets the tol to 10% error
tol = 10;

%Init error
err = 1000;

%Init timestep to be small
u = 0.01;

%Counter
i = 1 ;

%Loop for finding time steps
while err > tol
    
    %Calc val and error at this time step
    [a, b] = euler_explicit(@(x, y) 3*exp(-201*x) - 5*exp(-x), @(x, y) -201*y - 1000*exp(-x), -2, 0, 1, u);
    
    %Sets error for tol
    err = b;
    
    %Decreases timestep 
    u = 1/i;
    
    %Increases counter for decreasing time step
    i = i + 1;
    
end

%Time step disp
disp(['The number of steps is ', num2str(u)]);

%%
%The number of steps is 0.0097087

%% B
%Sets the tol to 10% error
tol = 10;

%Init error
err = 1;

%Init timestep to be small
u = 1;

%Counter
i = 1 ;

%Loop for finding time steps
while err > tol
    
    %Calc val and error at this time step
    [a, b] = euler_implicit(@(x, y) 3*exp(-201*x) - 5*exp(-x), 201, -2, 0, 1, u);
    
    %Sets error for tol
    err = b;
    
    %Decreases timestep 
    u = u - 0.01;    
   
end

%Output
disp(['The number of steps is ', num2str(u)]);

%%
%The number of steps is 1

%% C

%Solves for explicit and implicit for the points
[a, b, c, d] = euler_explicit(@(x, y) 3*exp(-201*x) - 5*exp(-x), @(x, y) -201*y - 1000*exp(-x), -2, 0, 1, 0.0097087);
[e, f, g, h] = euler_implicit(@(x, y) 3*exp(-201*x) - 5*exp(-x), 201, -2, 0, 1, 0.01);

%Solves using ODE15S
[T,Y] = ode15s( @(x, y) -201*y - 1000*exp(-x),[0:0.01:1],-2);

%Sets init and intervals
t = 0:0.01:1;
j = 3*exp(-201*t) - 5*exp(-t);

%Graphs it
hold all;

plot(t,j);
plot(c,d);
plot(g,h);
plot(T,Y);

legend('Analytical','Explicit','Implicit','ODE15S');

