  function J = obj_func_DC_a(P,Prob)
% returns the objective function to SNOPT
% J = Sum of the mean of the muscle activations to the power of n_p
% Last modification: 04/14/2008 by Marko Ackermann

n_nod = Prob.user.n_nod;
%model = Prob.user.model; % spedifies model used
n_f =  Prob.user.n_f; % number of DoFs
n_m = Prob.user.n_m; % number of muscles
n_p = Prob.user.n_p; % power in the cost function
c1_J = Prob.user.c1_J; % scaling of cost function
F_isomax = Prob.user.F_isomax; % vector of CE maximal isometric forces
L_ceopt = Prob.user.L_ceopt; % vector of optimal muscle CE lengths

% Extract only muscle activations and store them in a n_mx(n_nod-1) matrix
for k = 1:n_nod-1
    A(:,k) = P((k-1)*(2*n_f+3*n_m)+(2*n_f+n_m)+1:(k-1)*(2*n_f+3*n_m)+(2*n_f+2*n_m)); % muscle activations   
end

% Compute the objective function: sum of the mean squared activations
% multiplied by muscle volume and divided by total muscle volume, where
% muscle volume is estimated with Lceopti*Fisomaxi
VP_v = [L_ceopt L_ceopt].*[F_isomax F_isomax];
J =  c1_J*sum(VP_v*A.^n_p)/((n_nod-1)*sum(VP_v));