CHAPTER 3Basic Programming with MATLAB®

3.1 Introduction

Examples in previous chapters demonstrated manual interactions with MATLAB, either interactively or by typing commands. Those methods are fine for basic or one-time calculations, but your work is likely to require running multiple commands sequentially. In MATLAB, you can automate those command sequences and save them for later use as scripts and functions.

This chapter introduces material and techniques that can enhance your productivity with MATLAB by creating scripts and functions. Topics include:

  • Understanding algorithms
  • Building your own functions versus using MATLAB financial functions
  • Writing scripts and functions to automate processes
  • Flow control
  • Modular programming
  • Basic interactions with users
  • Debugging your code

Software required for this chapter: MATLAB base program

3.1.1 Algorithms 101

The term algorithm shows up frequently in the news—there is even a documentary on algorithms on Netflix. At times, it seems like algorithms run our lives and some people would maintain that they do, probably much more so than we realize. But what are algorithms? Essentially, they are sequences of steps or processes followed to solve a problem. In the modular programming approach, users develop algorithms by separating required computations into separate, more managable action steps called modules.

Here's a simplified financial example. Imagine you want to implement a real-time pricing formula for a security type. The desired ...

Get Foundations of Computational Finance with MATLAB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.