Finite Horizon Dynamic Programming Matlab Code