An Introduction to MATLAB Programming


TABLE OF CONTENTS

Introduction

This is a practical introduction to the MATLAB programming language. The best way to learn programming is to jump right in. We will skip the details and the oddities, and concentrate on concrete examples that do useful things. Some topics will be used before they have been properly defined, but you should be able to grasp the sense of what is going on, and you may expect the explanation to come shortly thereafter.

Your First Run of MATLAB

Before you can do anything with MATLAB, you need to have access to a computer, have an account, know how to start up the MATLAB program, and so on. We'll assume those hurdles have been overcome, and that you've started MATLAB, and now on your computer screen you have a big blank work area, called the MATLAB Command Window.

Back to TABLE OF CONTENTS.

Hello, Canned World!

The interactive approach to MATLAB is great if you're just doing simple calculations. But if you tend to make mistakes, are typing in long formulas, or have to turn in your homework, you need to put your commands in a file that you can correct, print, or reuse.

The second approach, called an M file, allows you to put your commands in a file, either inside the MATLAB editor, or using any text editor you like. The file is a permanent copy of a set of MATLAB commands, which can be named, and then referred to by name. This is an extremely useful feature; you can correct your mistakes; save your work and run it again later; modify your working example to solve a new problem. MATLAB expects your M-files to be in matlab subdirectory of your home directory.

Exercise: Create a "hello" M file. Use your favorite editor, or issue these instructions:

cd ~/matlab
edit hello.m
a
fprintf ( 'Hello yourself!\n' )
.
w
q
matlab
Now run MATLAB and type hello.

Sometimes it makes sense to use MATLAB interactively, rather than go through the effort of making an M file. As soon as your programs become more than 3 or 4 lines long, though, you'll find it much easier to use M-files, or better yet, to have both an edit window and a MATLAB window open, so you can edit, run, find a mistake and fix it, run again, and so on.

Back to TABLE OF CONTENTS.

The Square Root Algorithm

Programming is the implementation of algorithms. Our first algorithm is a method for approximating the square root X of a nonnegative number A. The algorithm can be regarded as a special case of Newton's method for nonlinear equations. The shorthand version is:

x := 0.5 * ( x + a / x ).

Exercise: Using paper, pencil, and a calculator, estimate the square root of 4. With a starting guess of 1, compute the next three estimates. Your third estimate should be about 2.00061.

Back to TABLE OF CONTENTS.

Implementation

Now we need to think about implementing the algorithm. This means nailing down details that we ordinarily skip over, but which are crucial to making the algorithm actually work. After all, a hammer may be essentially a piece of metal you hit stuff with, but it's useless without a hammer. We need especially to consider the issues of

None of this showed up in our one line version of the algorithm!

We won't take these issues too seriously yet, but let's specify a few details. Our sketch of an implementation will be:

Back to TABLE OF CONTENTS.

Example Program: The Square Root Algorithm

Based on our sketchy implementation, there's enough information to write down a MATLAB program:

    a = 1.96
    x = a

    for i = 1 : 10
      x = ( x + ( a / x ) ) / 2.0
    end

    fprintf ( 'The square root is %f\n', x )
  
(A copy of this file is available in root.m.)

The algorithm that we discussed earlier is clearly part of this program, and there is very little else going on. This is a big advantage of using MATLAB compared to C or FORTRAN: MATLAB programs are almost as short and simple as the algorithms they implement.

Notice how the one line algorithm is embedded in the for loop. The iteration, or repeated execution of the one line algorithm, is controlled by a simple count: i starts at 1, every time the body of the loop is executed i is increased by 1, and the last time the loop is executed, i will be 10.

Three lines of the program are assignment statements, and each time they are executed, we see a printout of the assigned value. Sometimes this is helpful, and sometimes not. You can turn off printout by appending a semicolon to the end of an assignment statement.

Exercise: Add a variable called residual which measures the residual error. In this case, the residual error is the difference between x^2 and a. Compute the value of residual every time that X is initialized or updated. Print the value of residual after the iteration has terminated.

Exercise: The for statement repeats the iteration a fixed number of times. We want to use a better iteration control, using the value of residual that we computed in the previous exercise. Replace the for statement by a while statement. Now, the iteration should only be carried out if the residual error is greater than one millionth. Can you follow these instructions EXACTLY?

Exercise: We'd like to be able to compute the square roots of many different values. Rather than editing the M file each time, we can pass in the value as an argument. Replace the line a = 1.96 by the following:

    function x = root ( a )
  
and then, inside of MATLAB, try typing
root ( 9 )

Back to TABLE OF CONTENTS.


Back to the home page.

Last revised on 18 January 2001.