Source: https://www.iro.umontreal.ca/~mignotte/IFT2425/Matlab.pdf