2. MATLAB Programming

Download this MathWorks provided quick reference on MATLAB: matlab-basic-functions-reference.pdf

MATLAB (MATrix LABoratory) is a data analysis, prototyping, and visualization tool with built-in support for matrices and matrix operations, excellent graphics capabilities, and a high-level programming language and development environment. MATLAB has become very popular with engineers, scientists, and researchers in both industry and academia. Three features of MATLAB make it stand out:

  1. Matrix and vector support
  2. Data visualization tools
  3. Huge library of functions covering nearly every area of math, science, and engineering.

MATLAB has extensive built-in documentation. It contains descriptions of the main functions, sample code, relevant demos, and general help pages. MATLAB documentation can be accessed in a number of different ways – command-line text-only help, a graphical documentation browser, and web pages.

MATLAB code that users write is interpreted, rather than compiled, so it is not necessary to declare and allocate variables in memory prior to using them. Although MATLAB programs are interpreted, MATLAB has features that make it still execute fairly fast.

  1. When the code is vectorized (explained later), instead of using loops, the performance is better than expected for interpreted programs.
  2. MATLAB has an extensive library of functions to do most of the work in your program. When MATLAB functions are run, then the computer is executing highly optimized compiled code.

Note

This chapter covers the basics of MATLAB programming. The intent is to start at the very beginning to get students started programming some simple programs. However, we do NOT wish to dwell on introductory material for very long. We will only cover the basics here. More features of MATLAB will be discussed and demonstrated by examples in following material.

Beginning with the Variables and Values section through the end of the chapter, the material here tracks the first five chapters and chapter seven of the free book, Physical Modeling in MATLAB, by Allen B. Downey [DOWNEY11]. Chapter 6 and parts of the remaining chapters are covered under the Computational Numerical Methods chapter of the study guide. Please download the PDF file of the book from the author’s web page. Dr. Downey was kind enough to share his work under a Creative Commons License, which allows us to use the material.

As is noted in the Preface of the book, his approach is slightly different than that taken by most books. Since MATLAB works so well with vectors and matrices, most books start using matrices right from the beginning to show off MATLAB’s capability. However, that is not the best starting point for students with limited prior knowledge of both programming and numerical computation. So Physical Modeling in MATLAB only uses scalar variables in first few chapters. Vectors are introduced later in the book. We will jump into vectors and matrices with both feet when we get to the Introduction to Linear Algebra chapter of the study guide.

Resources For Beginners

MathWorks MATLAB product page

MATLAB Onramp free training from MathWorks.

MATLAB for New Users video is sort of a teaser video of some cool things that users can do fairly soon after starting to use MATLAB. We will get to some of the cool things in the video later in the course. Don’t worry, we’ll start with some simpler things first.

Contents: