  function J = obj_func_DC_ftg(P,Prob)
% returns the objective function based on fatigue to SNOPT
% Note: this cost fucntion was derived based on the stress-endurance 
% relationship in Crowninshield and Brand (1981). For further information on
% the derivation of the cost fucntion refer to Marko's Notebook.
% J = sum over all muscles(sum over all nodes(aij^p))^n_p
% where N should be large to approximate a minmax problem and p was
% originally adoptet as 3
% Last modification: 10/08/2008 by Marko Ackermann

n_nod = Prob.user.n_nod;
c1_J = Prob.user.c1_J; % scaling of cost function
%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(1); % high power to simulate minmax
p = Prob.user.n_p(2); % power in the cost function

% 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:
J =  c1_J*(sum(sum(A.^p,2).^n_p))^(1/n_p);