function J = costfun(u,t,init,d,tintp,par) 

%qhf = u(1:length(u)/2,1);
%qhr = u(length(u)/2+1:end,1);

[t x] = ode15s(@(t,x)equations(t,x,u,d,tintp,par),t,init);

J = x(:,3);
J = J(end);

%J = trapz(tintp,P.*(qhf+qhr));
%J = trapz(tintp,P.*(qhf+qhr).^2);
%J = trapz(tintp,(P.*(0.4*(qhf+qhr)).^2+(qhf+qhr)));
