MATLAB code for Part II