% Script to generate values for runoff calculations
% A. Trani

% Given a vector of time values (t) in seconds
% Given four coefficients k1, k2, k3 and k4

% Calculate runoff (cu. meters/second) using the following equation:
% runoff =k2 * k1*sin(t/k3) .* exp(-t/k4) ;

clear all
close all
clc

t=0:1:4500;                                             % time vector (seconds) to evaluate the function

% Define constants for the problem (runoff function)
k1 = 50;          
k2 = 2;          
k3 = 1500;         
k4 = 800;  

% Define the bounds of time to perform the numerical integration using the
% Trapezoid function

lowerBound = 500;           % time units (seconds)
upperBound = 2000;

% Calculate the runoff generated by the storm (over the full range of
% values)

runoff =k1 *sin(t/k3) .* exp(-t/k4) + k2;   % runoff (cu.meters/second)          

plot(t,runoff,'o-r')
xlabel('Time (seconds)')
ylabel('Runoff (cu.feet/second)')
grid

% Practice using the area command in Matlab

x_vector = lowerBound:1:upperBound;                                   % Same units as t vector above
fx = k1 *sin(x_vector/k3) .* exp(-x_vector/k4) + k2;            % Same runoff equation

% Plot the area under the curve for value of x from lowerbound to an
% upperbound value of x_vector

hold on
area(x_vector,fx)

% Use the trapezoidal rule numerical integration function to evaluate the
% area under the curve of function fx

volumeOfWater = trapz(x_vector,fx);

disp(['Volume of water accumulated is  ', num2str(volumeOfWater)]);

    