MATLAB algorithm for calculating middleman power