2. MATLAB ProgrammingΒΆ

See also

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, but don’t worry, we’ll start with some simpler things first.

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 MATLAB’s 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. Interpreted programs run slower than compiled code; however, MATLAB has two 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 harder worker in your program. When MATLAB functions are run, then the computer is executing highly optimized compiled code.

Note

This chapter of the study guide covers the basics of MATLAB programming. The intent is to start at the very beginning to get students started programming some simple programs. However, I do NOT wish to dwell on introductory material for very long. I just want to cover the basics here. More features of MATLAB will be taught 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 Numeric 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, begins with just using scalar variables. 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.

Contents: