Basics of optimization theory with applications in MATLAB and R