function [dZ, dZdT] = internal_mseBackward(dZ, X, T, observationDim)
% This function is undocumented and will change in a future release.

%   Copyright 2019-2021 The MathWorks, Inc.

if ~isreal(dZ)
    error(message('deep:dlarray:ComplexGradientOutput', 'mse'));
end

N = size(X, observationDim);
dZ = dZ.*(X - T)./N;

if nargout > 1
    dZdT = -dZ;
end

end