Introduction

  • What is MATLAB?
    • MATLAB stands for MATrix LABoratory
    • Developed by Cleve Moler in late 1970s
    • Easy access to LINPACK and EISPACK
    • Written in FORTRAN and later rewritten in C
  • Why MATLAB?
    • Excels at numerical calculations and graphics, especially matrix calculations
    • Has many toolboxes for different disciplines
    • Has many visualization tools
    • However
      • works slower than C and Fortran
      • for large applications such as operating systems or software design (C++, JAVA or FORTRAN), would be better.

Basic Commands

In [1]:
clc

Clears the command window, but does not delete any variable

In [2]:
clear

Delete all variables

In [3]:
home

Shifts the commands up so that the cursor is at the top left of the command window (You can still access previous commands by scrolling upwards).

In [ ]:
help *topic*
*topic* not found.

Use the Help browser search field to search the documentation, or
type "help help" for help command options, such as help for methods.


helps on a particular topic

In [ ]:
lookfor *keyword*

Searches for keyword in all help entries

In [ ]:
who

Lists the names of the variables defined by the user

In [ ]:
whos

Lists detailed information about the variables defined by the user

In [ ]:
date

Shoes the current system date

Variables

Variable and function names are case sensitive They cannot start with a number They cannot include language specific characters such as [], (), % They cannot include punctuation characters They cannot include spaces

In [ ]:
iskeyword

Lists keywords which you cannot assign as a variable name

In [ ]:
isvarname *variable*

Checks whether a variable name is allowed (ans=1) or not (ans=0)

We can reassign built-in function names as variable names, but be careful! (for example "sin")

Display Format for Numeric Values

In [ ]:
format short

4 decimal places (3.1416) (default display)

In [ ]:
format short e

4 decimal places with exponent (3.1416e+00)

In [ ]:
format long

many decimal places (3.141592653589793)

In [ ]:
format long e

many decimal places with exponent (3.141592653589793e+00)

In [ ]:
format bank

2 decimal places (3.14)

In [ ]:
format rational

small integer ratio output (355/113)

In [ ]:
format +

gives +, - or blank

Saving Your Work, m-files

In [ ]:
cd *path*

Changes working directory

In [ ]:
diary

Creates a copy of all the commands issued in the workspace window, and most of the result

In [ ]:
save *filename* *variablelist*

Saves variables in a file

In [ ]:
load *filename*

Loads matrices from a file

In [ ]:
quit or exit

Terminates MATLAB

In [ ]:
what

Lists MATLAB-specific files in directory

In [ ]:
%%

Cell mode

m-files: Files whose names end with the extension .m (scripts and functions)

Arithmetic Operations

Operator Operation
+ plus
- minus
* matrix multiplication
.* array multiplication (element wise)
ˆ matrix power
array power (element wise)
/ slash or right division
\ backslash or left division
./ right array division (element wise)
.\ left array division (element wise)
: colon
' transpose

Relational Operations

Operator Operation
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
~= not equal to

Logical Operators

Operator Operation
& and
~ not
$\mid$ or
xor exclusive or

Arrays and Matrices (more on this later)

  • A scalar is a matrix with only one row and one column. It is simply a single value.
  • A vector is a special form of matrix which contains only one row or one column.
    • A matrix with only one row is called a row vector.
    • A matrix with only one column is called a column vector.
    • They can be named as one-dimensional arrays.
  • A matrix is a rectangular multidimensional array of numbers.
In [ ]:
a:b:c

This method creates an array with values starting from a and ending at c with an increment of b.

In [ ]:
linspace(a,b,N)

Linearly spaced vector function. This command creates an array with N values starting from a and ending at b.

In [ ]:
logspace(a,b,N)

Logarithmically spaced vector function.

Common Math Functions

abs(x) : Finds the absolute value of x

In [1]:
abs(-2)
ans =

     2


sqrt(x) : Find the square root of x

In [2]:
sqrt(16)
ans =

     4


nthroot(x,n) : Finds the real nth root of x. This function will not return complex results.

In [3]:
nthroot(16,4)
ans =

     2


sign(x) : Returns a value of -1 if x is less than zero, and a value of +1 if x is greater than zero

In [4]:
sign(-2)
ans =

    -1


In [5]:
sign(2)
ans =

     1


rem(x,y) : Computes the remainder of x/y

In [6]:
rem(34,5)
ans =

     4


exp(x) : Computes the value of ex, where e is the base for natural logarithms (approximately 2.7183)

In [7]:
exp(15)
ans =

   3.2690e+06


log(x) : Computes ln(x), the natural logarithm of x (to the base e)

In [8]:
log(15)
ans =

    2.7081


log2(x) : Computes the logarithm of x to the base 2

In [9]:
log2(15)
ans =

    3.9069


log10(x) : Computes the logarithm of x to the base 10

In [10]:
log10(15)
ans =

    1.1761


reallog(x) : Computes the natural logarithm of each element in array X. Array X must contain only nonnegative real numbers.

realpow(x,y) : Raises each element of array X to the power of its corresponding element in array Y. Array X and Y must be the same size. All elements of the output array Z must be real.

realsqrt(x) : Computes the square root of each element of array X. Array X must contain only nonnegative real numbers.

Rounding Functions

round(x) : Rounds x to the nearest integer

In [11]:
round(15.748)
ans =

    16


fix(x) : Rounds (or truncates) x to the nearest integer toward zero

In [12]:
fix(15.748)
ans =

    15


In [13]:
fix(-15.748)
ans =

   -15


floor(x) : Rounds x to the nearest integer toward negative infinity

In [14]:
floor(15.748)
ans =

    15


In [15]:
floor(-15.748)
ans =

   -16


ceil(x) : Rounds x to the nearest integer toward positive infinity

In [16]:
ceil(15.748)
ans =

    16


In [17]:
ceil(-15.748)
ans =

   -15


Functions Used in Discrete Math

factor(x) : Finds the prime factors of x

In [18]:
factor(18)
ans =

     2     3     3


gcd(x,y) : Finds the greatest common divisor of x and y

In [19]:
gcd(12,18)
ans =

     6


lcm(x,y) : Finds the least common multiple of x and y

In [20]:
lcm(12,18)
ans =

    36


rats(x) : Represents x as a fraction

In [21]:
rats(3.8)
ans =

    '     19/5     '


factorial(x) : Finds the value of x factorial (x!)

In [22]:
factorial(5)
ans =

   120


nchoosek(x,y) : Finds the number of possible combinations of k items from a group of n items

In [23]:
nchoosek(15,3)
ans =

   455


primes(x) : Finds all the prime numbers less than x

In [24]:
primes(8)
ans =

     2     3     5     7


isprime(x) : Checks to see if x is a prime number If it is, the function returns 1; If not, it returns 0.

In [25]:
isprime(11)
ans =

  logical

   1


In [26]:
isprime(12)
ans =

  logical

   0


perms(v) : Returns a matrix containing all permutations of the elements of vector v. It has n! rows and n columns. If it is, the function returns 1; If not, it returns 0.

In [27]:
v=[2 3 7];
perms(v)
ans =

     7     3     2
     7     2     3
     3     7     2
     3     2     7
     2     7     3
     2     3     7


Trigonometric Functions

sin(x) : Finds the sine of x (expressed in radians)

In [28]:
sin(0)
ans =

     0


cos(x) : Finds the cosine of x (expressed in radians)

In [29]:
cos(pi)
ans =

    -1


tan(x) : Finds the tangent of x (expressed in radians)

In [30]:
tan(pi/4)
ans =

    1.0000


cot(x) : Finds the cotangent of x (expressed in radians)

In [31]:
cot(pi/6)
ans =

    1.7321


sind(x) : Finds the sine of x (expressed in degrees)

In [32]:
sind(90)
ans =

     1


cosd(x), tand(x), and cotd(x) function the same way as sind(x) do

asin(x) : Finds the arcsine, or inverse sine of x, where x must be between -1 and 1. The function returns an angle in radians between $\pi/2$ and $-\pi/2$

In [33]:
asin(-0.5)
ans =

   -0.5236


acos(x), atan(x), and acot(x) function the same way as asin(x) do

asind(x) : Finds the inverse sine of x (expressed in degrees)

In [34]:
asind(1)
ans =

    90


acosd(x), atand(x), and acotd(x) function the same way as asind(x) do

sinh(x) : Finds the hyperbolic sine of x (expressed in radians)

In [35]:
sinh(pi)
ans =

   11.5487


cosh(x), tanh(x), and coth(x) function the same way as sinh(x) do

asinh(x) : Finds the inverse hyperbolic sine of x (expressed in radians)

In [36]:
asinh(1)
ans =

    0.8814


acosh(x), atanh(x), and acoth(x) function the same way as asinh(x) do

deg2rad(x) : Converts angle units from degrees to radians

In [37]:
deg2rad(90)
ans =

    1.5708


rad2deg(x) : Converts angle units from radians to degrees

In [38]:
rad2deg(pi/3)
ans =

   60.0000