Skip to main content

calcMuscleExcitationPenaltyCost.m


% This function is part of the NMSM Pipeline, see file for full license.
%
% (Array of number, struct) -> (Array of number)
% returns the cost for all rounds of the Muscle Tendon optimization


function cost = calcMuscleExcitationPenaltyCost(modeledValues, ...
experimentalData, costTerm)
errorCenter = valueOrAlternate(costTerm, "errorCenter", 0.5);
maximumAllowableError = valueOrAlternate(costTerm, "maxAllowableError", 0.25);
muscleExcitationsConstraint = modeledValues.muscleExcitationsNoTDelay(: , ...
setdiff(1 : size(modeledValues.muscleExcitationsNoTDelay, 2), ...
[experimentalData.synergyExtrapolation.missingEmgChannelGroups{:}]), ...
experimentalData.numPaddingFrames + 1 : ...
size(modeledValues.muscleExcitationsNoTDelay, 3) - ...
experimentalData.numPaddingFrames);
cost = 30 / maximumAllowableError * (muscleExcitationsConstraint - errorCenter) .^ 8;
cost(isnan(cost))=0;
cost = sum((sqrt(0.1) .* cost).^ 2, 'all');
end