MATLAB implementation of BP algorithm