function retval = finqu5(u)
% usage:  y = finqu5(u)
% description: Returns value of function
% of final exam question #5

% no bulletproofing
n = length(u);
dt = 1/(n-1);
t = linspace(0,1,n)';
y = zeros(n,1);
% calculate y first
% calculate y first
for ii = 2:n
  y(ii) = y(ii-1) + dt*(u(ii-1)*y(ii-1)+t(ii-1)^2);
end
% next the functional
Lfnal = (y-3).^2+0.5*u.^2;
retval = dt/2*sum(Lfnal(1:n-1)+Lfnal(2:n));
%retval = dt*sum(Lfnal(2:n));