matlab code implemented, shows matching results to R code. closes #4