You are on page 1of 22

MATLAB - Lecture # 1

Starting with MATLAB / Chapter 1

Topics Covered:
1. Introduction.
2. MATLAB windows.
3. Arithmetic operations with scalars.
4. Working in the command window.
5. Defining variables (scalars).
1-4

MATLAB is a software for numerical computations,


visualization and programming.
The name MATLAB stands for MATrix LABoratory.
Its basic data element is an array (explained later).

With MATLAB you can:


 Use it as a calculator.
 Define variables and use them in calculations.
 Use built-in functions (sin, cos, max, min, etc.).
 Plot graphs.
 Write and run computer programs.
 Do symbolic calculations.
6
MATLAB windows:

Command Window: Main window that opens when


MATLAB is started. It has the command prompt ’ » ‘.
All commands can be typed in this window. Used for
running programs written by user.
 Figure Window: Contains graphs created by graphics

commands. This window opens automatically.


 Editor Window: Used for writing and editing
programs. This window is opened from the File menu
in the Command Window.
 Help Window: Contains help information. This
window is opened from the Help menu in any of the
previous windows.
6
(The Command Window opens
Command Window
when MATLAB is started)

Command prompt
7
Figure Window (The Figure Window opens
automatically by the plot command)
7
Editor Window (The Editor Window is opened from the
file menu in the Command Window)
8
Help Window (The Help Window can be opened from the
Help menu of any of MATLAB windows)
8-9
WORKING IN THE COMMAND WINDOW

• To type a command the cursor must be placed


after the command prompt (>>).
• Once a command is typed, and the “Enter” key is
pressed, the command is executed. (Only the last
command is executed. Everything executed before
is unchanged)
• It is not possible to go back to a previous line and
make a correction.
• A previously typed command can be recalled to
the command prompt with the up-arrow key ( ↑) .
10
ARITHMETIC OPERATIONS WITH SCALARS

Operation Symbol Example


Addition + 5+3
Subtraction - 5-3
Multiplication * 5*3
Right Division / 5/3
Left Division \ 5\3=3/5
Exponentiation ^ 5^3

NOTE: For scalars the arithmetic operations are the usual


ones. For vectors and matrices the arithmetic operations can
either follows matrix algebra rules, or can be performed on
element-by-element basis (discussed in the next lectures).
10
ORDER Of PRECEDENCE
(The order in which operations are executed by the computer)

Higher-precedence operations are executed before lower-


precedence operations.
If two operations have the same precedence, then the expression is
executed from left to right.
PRECEDENCE OPERATION

First Parentheses, starting with the innermost pair.

Second Exponentiation.

Third Multiplication and division (equal precedence).

Fourth Addition and subtraction (equal precedence).


11
USING MATLAB AS A CALCULATOR

Using numbers:
>> 7+8/2 Type and press Enter 8/2 is executed first
ans =
Computer response
11

>> (7+8)/2 Type and press Enter 7+8 is executed first


ans =
Computer response
7.5000

>> 4+5/3+2 Type and press Enter 5/3 is executed first


ans =
Computer response
7.6667
11
5^3/2 Type and press Enter 5^3 is executed first,
ans = /2 is executed next.
Computer response
62.5000>>

>> 27^(1/3)+32^0.2 Type and press Enter 1/3 is executed first,


ans = ^ is executed next,
Computer response + is executed last.
5

>> 27^1/3+32^0.2 Type and press Enter 27^1 and 32^0.2 is


executed first,
ans =
Computer response /3 is executed next,
11 + is executed last.
12-
DISPLAY FORMATS 13
The format command controls how output numbers appear on the
screen. Input numbers can be written in any format.

format short (the default) 41.4286 Fixed-point with 4 decimal


digits.
format long 41.42857142857143 Fixed-point with 14 decimal
digits.
format short e 4.1429e+001 Scientific notation with 4
decimal digits.
format long e 4.142857142857143e+001 Scientific notation with
15 decimal digits.
Format bank 41.43 Two decimal digits.

MATLAB has several other formats in which numbers can be displayed.


13-
15
MATLAB BUILT-IN MATH FUNCTIONS
In addition to arithmetic operations, MATLAB can be used to
calculate elementary math functions. The most common ones are:

sin(x) x in radians exp(x) exponential


cos(x) x in radians log(x) natural logarithm
tan(x) x in radians log10(x) base 10 logarithm
cot(x) x in radians sqrt(x) square root
The inverse is: asin(x), abs(x) absolute value
acos(x), etc.
Examples:
>> >> sqrt(169) >> log10(10000)
sin(0.78539)
ans = ans =
ans =
13 4
MATLAB 0.7071
has hundreds of built-in functions (this will be discussed in
future lectures).
16-
18
THE ASSIGNMENT OPERATOR

In MATLAB, the = sign is called the ASSIGNMENT


OPERATOR.

The ASSIGNMENT OPERATOR assigns a value to a variable.

Variable = A value, or a computable value

The left The right hand side can be a


hand side specific value, or a computable
can only be expression (an expression that
one includes values and/or previously
variable. defined variables).
16-
THE ASSIGNMENT OPERATOR 18
For example, if you type:
>> x = 3
MATLAB assigns the value of 3 to x.
x=
3
If then you type:
>> x = x + MATLAB assigns a new value to x, which
5 is the old value 3 plus 5.

x= (In mathematics this expression has no


8 meaning since it implies: 0 = 5.)

For example, the statement:


x + 4 = 30 is not valid. MATLAB does not solve for x,
but the statement:
x = 30 – 4 is valid (the number 26 is assigned to x.)
16-
DEFINING VARIABLES 18
A variable is defined by typing a variable name followed by the
assignment operator (equal sign) and then a value, or a
mathematical expression.
>> a=8 Type and press Enter
a=
Computer response
8

>> B=12 Type and press Enter


B=
Computer response
12
Once a variable is defined, the computer remembers and stores its
value. The variable can then be used in further calculations.
>> a+B >> a/B >> B/a >> B^a
ans = ans = ans = ans =
20 0.6667 1.5000 429981696
16-
18
Variables can also be used to define new variables
>> d=a*B
d=
96
Once in existence, variables can be used in functions
>> sqrt(d)
ans =
9.7980
A previously defined variable can be redefined and reassigned
a new value.
18
RULES ABOUT VARIABLES NAMES

 Variable names can be up to 63 characters long.


 Variable name can contain letters, digits, and
the underscore character.
 Variable name must begin with a letter.
 MATLAB is case sensitive; it distinguishes
between uppercase and lowercase letters. For
example, A and a are not the same variable.
18
AVOID USING NAMES OF FUNCTIONS
FOR VARIABLES.

Once a function name is used to define a variable, the function


can not be used.

This means that variables should not be named


sin, cos, exp, tan, sqrt, ……., etc.
OR:
max, min, sum, det, …., etc.
18-
19
PREDEFINED VARIABLES
MATLAB has several variables that are predefined.
These variables can be redefined to have any other value.
It is probably better not to use the predefined variables as
variable names.
Some of the predefined variables are:

pi (π ), eps (the smallest difference between two numbers)


inf (infinity)
i (square root of –1) j (square root of –1)
ans (the value of the most recent calculation)
Typing these variables gives:
>> pi >> sin(pi/4) >> eps >> inf >> i
ans = ans = ans = ans = ans =
3.1416 0.7071 2.2204e-016 Inf 0 + 1.0000i
9,
SOME USEFUL COMMANDS 19
When these commands are typed in the Command Window they
either provide information, or perform a task.
; When a semicolon is typed at the end of a line, the
computer does not displays the output.
>> abc=37 >> def=23;
abc = >>
37

clc Clears the command window.


clear Removes all variables from memory.
clear x y z Clears only variables x, y and z.
who Lists the variables currently in memory.
clf Clears the Figure Window.
% typing % at the beginning of a line designates the line
as a comment, which is not executed.

You might also like