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

% no bulletproofing
n = length(u);
dt = 1/(n-1);
t = linspace(0,1,n)';
y = zeros(n,1);
p = y;
% 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 adjoint variable
for ii = n:-1:2
  p(ii-1) = p(ii) + dt*(p(ii)*u(ii)+2*(y(ii)-3));
end
% next the gradient
retval = p.*y + u;