%  Main routine for the implementation of explicit Euler, explicit Heun 
%  and Runge-Kutta methods for the solution of the evolution of water 
%  draining through a rectangular opening.
%  

clear;
format long;

% Requests key input to user
Cd = input(' Indicate value of drawdown coefficient - Cd: ');
A = input(' Indicate Area of footprint in metres - A: ');
b = input(' Indicate width of the rectangular openingn in metres - b: ');
y0 = input(' Indicate initial height of water within the opening in metres- y0: ');
t_end = input(' Indicate final time in seconds: ');
n_steps = input(' Indicate number of discretization step: ');
%Cd=0.65;A=50;b=0.2;y0=0.75;t_end=120;n_steps=300;

% Approximation methods
y_Euler = Euler(Cd,b,y0,A,t_end,n_steps);
y_Heun = Heun(Cd,b,y0,A,t_end,n_steps);
y_RungeKutta = RungeKutta(Cd,b,y0,A,t_end,n_steps);

%Analytical solution
y_analytical = Analytical_solution(Cd,b,y0,A,t_end,n_steps);

% Absolute error
err_Euler = abs(y_Euler-y_analytical);
err_Heun = abs(y_Heun-y_analytical);
err_RungeKutta = abs(y_RungeKutta-y_analytical);

% Maximum error
max_err_Euler = max(err_Euler);
max_err_Heun = max(err_Heun);
max_err_RungeKutta = max(err_RungeKutta);

% Plot comparing all three numerical solutions against the analytical
% solution
% Figure 1 plots evolution of the water height over time
figure(1);
clf;
% Ensures that the visualization includes all four curves
y_max = max(max([y_Euler;y_Heun;y_RungeKutta;y_analytical]));
y_min = min(min([y_Euler;y_Heun;y_RungeKutta;y_analytical]));
% sets up x-coordinate 
h = t_end/n_steps;
x = 0:h:t_end;
% plots solution curves 
xlabel('Time (s) - t'), ylabel('Height of Water (m) - y');
axis square, axis([0, t_end, y_min, y_max]);
title('Comparison of solution vs time for different methods');
plot (x,y_Euler,'m--');
hold on;
plot (x,y_Heun,'b--');
hold on
plot (x,y_analytical,'r-');
hold on;
plot (x,y_RungeKutta,'ko');
legend('Euler','Heun','Analytical','Runge-Kutta')

% Figure 2 plots absolute error versus time
figure(2);
clf;
% Ensures that the visualization includes all four curves
y_max = max(max([err_Euler;err_Heun;err_RungeKutta]));
y_min = min(min([err_Euler;err_Heun;err_RungeKutta]));
% sets up x-coordinate 
h = t_end/n_steps;
x = 0:h:t_end;
% plots error curves
xlabel('Time (s) - t'), ylabel('Error');
axis square, axis([0, t_end, y_min, y_max]);
title('Comparison of error vs time for different methods');
plot (x,err_Euler,'k--');
hold on;
plot (x,err_Heun,'b--');
hold on;
plot (x,err_RungeKutta,'r--');
legend('Euler','Heun','Runge-Kutta')

% Figure 3 plots absolute error versus time for Runge-Kutta only
figure(3);
clf;
% Ensures that the visualization includes all four curves
y_max = max(max([err_RungeKutta]));
y_min = min(min([err_RungeKutta]));
% sets up x-coordinate 
h = t_end/n_steps;
x = 0:h:t_end;
%Plots Runge-Kutta error curve
xlabel('Time (s) - t'), ylabel('Error');
axis square, axis([0, t_end, y_min, y_max]);
title('Comparison of error vs time for RK Methods');
hold on;
plot (x,err_RungeKutta,'r--');
legend('Runge-Kutta')