Final version of MATLAB code