MATH/CMSC 206 - Introduction to Matlab

Announcements Syllabus Tutorial Projects Submitting

A Few Other Basic Commands

Contents

Precision for Display

Whenever you are performing mathematical computations on a computer, you must realize that some rounding of values is necessarily involved. A computer stores each value in a relatively small finite region of memory. How could you possibly store a value like pi, which has an infinite non-repeating decimal expansion, in a small space? You can't. So computers will necessarily round messy numbers. Some programming languages round more than others, but calculations in Matlab are handled fairly precisely. What we mean is that each numerical value is stored using a lot of digits. For example, calculations involving the constant pi use the value 3.141592653589793 instead of 3.14 or 3.1416.

However, the internal precision of values in Matlab will not always get displayed when Matlab shows you an answer. Try the following:

pi
ans =

    3.1416

This answer might seem like a disappointing level of precision. The good news is that the internal value really is stored very precisely, but Matlab assumes that most people just don't want to see all of those ugly digits, so the value is rounded before it is displayed. If you really want to see all of the digits in your answers to computations, try the following:

format long
pi
ans =

   3.141592653589793

Aha! There are those digits. If you want to go back to showing fewer digits in Matlab's answers, just use:

format short

Don't forget that values used in computations are always represented internally using full precision, regardless of whether you specify the long or short format; The format command only affects the way the answer is displayed on the screen.

Keeping a Diary

Sometimes you may want to log or record your Matlab session. Who knows, maybe one day one of your instructors will ask to see a transcript of your session with Matlab. Or maybe you're just obsessed with Matlab.

You can start recording a Matlab session at any time. In the example below, we'll pretend that we are going to record a Matlab session in which we do a few arithemetic operations. Try this:

diary on
10 + 7 * 9
ans =

    73

8 / 4 + 11
ans =

    13

diary off

The command diary on begins the recording. diary off stops it. The session above has been recorded in a file called diary. You should now be able to see the name of this file in the "Current Directory" pane on the left of the workspace. If you'd like to see the contents of the diary file, type:

edit diary

Later, if you record another session to the diary, it will be appended to the end of the existing diary. If you'd prefer to erase the existing diary before beginning a new recording, use this:

delete diary

By the way, you can store a record of your Matlab session using any filename you want - diary is just the default filename. Suppose you wanted to store your session in a file called MySession.txt. In this case, you would start and stop the session using the commands:

diary MySession.txt
diary off

The commands edit and delete can be used as we used them before, substituting the filename MySession.txt in place of diary.

Self-Test

  1. Write the Matlab command that will cause all of your results to be displayed in full precision.
  2. Write the Matlab command that will cause your results to be rounded before they are displayed.
  3. Write the Matlab command that will start recording your session to a diary called today.txt.
  4. Write the Matlab command that will stop recording a diary.

Answers to Self-Test

Next Topic: Variables