Table Of Contents

Introduction

_images/matlab_logo81.png

What is MATLAB ?

  • a mathematical and graphical software package for solving problems and for graphical illustrations
  • can solve a wide range of numerical problems, from the very basic to complex problems
  • has built-in functions and toolboxes to do many operations
  • is supported in Windows, Mac OS X, and Linux platforms
  • is now available in version R2011a

Other scientific softwares

There are many softwares that have similar features to MATLAB. Examples of commercial softwares are Mathematica and Maple. Free softwares include Scilab, Octave and Sage. The latter is developed in Python-based language. All of these software are available in Windows, Mac OS X, and Linux platforms.

Command window

three commonly used panels: command window, current folder, and command history

_images/command_window59.png

Window Purpose
Command window main window, enter variables, run programs
Figure window contains output from graphic commands
Editor window creates and debugs script and function files
Help window provides help information
Command history window & logs commands entered in the command window
Workspace window provides information about the variables that are used
Current Folder shows the files in the current directory

Figure window

_images/figure40.png

A figure window contains graphs created by putting commands in the command window. This graph is generated by the following command:

>> t=linspace(0,10,500);
>> y = exp(-t).*sin(10*t);
>> plot(t,y)





Working in the command window

  • To type a command the cursor must be placed next to the command prompt (>>).
  • Press Enter key to execute the command.
  • Several commands can be typed in the same line. This is done by typing a comma (,) between the commands
  • If a semicolon (;) is typed at the end of a command, the output of the command is not displayed. It is useful when the result is obvious or the output is very large.
  • A previously typed command can be recalled to the command prompt with the upper arrow key. The down-arrow key can be used to move down the previously typed commands. This can also be obtained by clicking at the desired command on the command history window.
  • If a command is too long to fit in one line, it can be continuted to the next line by typing three periods (...) and press the Enter key
  • The symbol %, when it is typed in the beginning of a line, the line is designated as a comment.

Start using MATLAB

First we can try simple arithmetic operations with scalars. MATLAB executes the calculations according the order of precedence just like what we have in most calculators.

  1. Parantheses. For nested parantheses, the innermost are executed first.
  2. Exponentiation (^)
  3. Multiplication (*), division (/) (equal precedence)
  4. Addition (+) and subtraction (-)

Let’s compare the command:

>> 2^6/3+4
        ans =
        25.3333

and the following command:

>> 2^(6/3)+4
        ans =
        8

The first example computed 2^6 first and then the result is divided by 3, and then was added by 4. The latter computed 2^2 (from 6 divided by 3) which gives us 4 and then it was added by 4.

Use a comma (,) to seperate several commands:

>> 3+2, 5*6
        ans =
                5
        ans =
         30

How about using the semicolon (;)?

>> 3+2; 5*6
        ans =
        30

The first command is executed but the output is not displayed.

When a command is too long, we can use ... to break the expression:

>> 0.56699999-0.222477777411/0.147778522699^4.3555555 ...
        +1.428877/8.667774

                ans =
                        -919.9098

Defining a variable

MATLAB allows you to assign a value to a variable. For example,

>> x = 3

We should read as ‘x is assigned a value of 3’. Then we can use this variable is subsequent calculations:

>> x=4*x-10
x =
     2

Now a new value is assigned to x. The new value is 4 times the previous value of x minus 10.

To define a variable:

  • all names must start with a letter
  • they may contain letters, numbers, and the underscore (_)
  • no spaces are allowed between characters
  • names are case sensitive
  • there are certain keywords we cannot use such as / * # $ % @ !
  • avoid using the names of built-in function for a variable (i.e., avoid using cos, sin, exp, etc.)

There are 17 words, called keywords that are reserved by MATLAB for various purposes, and cannot be used as variable names. These words are:

break  case  catch  continue  else  elseif  end  for  function
global  if  otherwise  persistent  return  switch  try  while

Let’s see one more example:

>> x=13;
>> y=5;
>> z=(x-y)+20-x*y;
>> z
        z =
        -37

First the variables x,y,z are defined but they are not displayed since a semicolon is typed at the end of each statement. So when we typed the name of the variable (here z), the value of z is displayed.

Useful commands for managing variables


Command Outcome
clear removes all variables from the memory
clear x y removes only variables x,y from the memory
who displays a list of variables currently in the memory
whos displays a list of variables and their size together with information about their bytes and class

There are frequently used variables that are already defined when MATLAB is started.

Variables Description
ans a variable that has the value of the last expression
pi the number of \pi
eps the smallest difference between two numbers and is equal to 2^(-52)
inf used for infinity
i defined as \sqrt{-1}
j same as i
NaN stands for Not-a-Number

Controlling output format

The command format controls how the result of computations is displayed in MATLAB. The default is format short which shows at most four decimals, with long format we can view all 16 figures:

>> format long
>> 2*3^5/7

ans =
  69.428571428571431

The result can be rounded to a fixed number of decimals with the function round:

>> format short
>> round(ans*1000)/1000

ans =
   69.4290