function [ Kfm ] = Local_Yielding( Kf, Salt, Smean, Kfs, Talt, Tmean, Sy )
%
%    This routine returns the stress concentration factor to use for
%    mean stresses.  The stress concentration factor is corrected if local
%    yielding is occuring.
%
%    Kf = the fatigue stress concentration factor
%    Salt = the alternating bending stress
%    Smean = the mean or midrange bending stress
%    Kfs = the fatigue shear stress concentration factor
%    Talt = The alternating shear stress
%    Tmean = the mean shear stress
%    Sy = the yield strength of the material

S = Kf * (Salt + Smean);
T = Kfs * (Talt + Tmean);

Svm_max = sqrt(S^2 + 3*T^2);
Svm_alt = sqrt((Kf*Salt)^2 + 3*(Kfs*Talt)^2);
Svm_mean = sqrt((Kf*Smean)^2 + 3*(Kfs*Tmean)^2);

if (Svm_max < Sy)
    Kfm = Kf;
elseif (2*Svm_alt > 2*Sy);
    Kfm = 0;
    error('MATLAB:Local_Yielding_Correction', 'Stresses too great');
else
    Kfm = (Sy - Svm_alt) / Svm_mean; 
end

end

