MatLabChapter 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 105

Chapter 2

22NovTZK 1
Get started with Matlab
Explore the Matlab windows
Solve some problems using Matlab
Learn how to save our work

22NovTZK
2
In Windows or Apple operating systems click on
the desktop icon

22NovTZK
3
22NovTZK
4
Matlab uses a standard
windows menu bar
Current To exit Matlab use the
Directory MATLAB Windows
close icon

22NovTZK
5
Matlab uses several different windows to
display data, commands and results.
They are not necessarily all open at once

22NovTZK
6
You can use the command window much
like you’d use a calculator
The standard order of operation rules
apply

22NovTZK
7
Similar to a scratch pad
Once you hit enter, you can’t edit any
commands
You can retype them or use the arrow keys to
retrieve commands and edit them before hitting
enter again

22NovTZK
8
Records the commands you issue in the
command window
When you exit the command window, or when
you issue the clc command, the command
window is cleared
But the command history remains

22NovTZK
9
You can transfer commands from the command
history to the command window
◦ Double click on a command
◦ Click and drag

22NovTZK
10
Keeps track of the variables you’ve defined
◦ Name
◦ Value
◦ Class
◦ Size
◦ Bytes
You may need to click on the name bar and
select size and bytes in order to see these
parameters

22NovTZK
11
22NovTZK 12
22NovTZK
13
Scalar

Vector

2-D
Matrix

22NovTZK
14
The current directory window is a list of files
When you try to load information from a file or
try to save information – Matlab uses the
current directory

22NovTZK
15
Scalar

Vector

2-D
Matrix

22NovTZK
16
If you double click on any variable in the
workspace window Matlab launches a
document window containing the array editor
You can edit variables in the array editor

22NovTZK
17
Documet Window
Document Window

22NovTZK
18
When Figures are created a new window opens
It’s extremely easy to create graphs in Matlab

22NovTZK
19
The semicolon suppresses
the output from each
command

22NovTZK
20
22NovTZK
21
This window allows you to type and save a
series of commands without executing them
There are several ways to open an editing
window
◦ From the file menu
◦ With the new file icon

22NovTZK
22
The semicolon suppresses
the output from each
command

22NovTZK
23
Save and Run

Write your code in the editing window,


then run it using the Save and Run
icon

22NovTZK
24
We’ve already solved some simple
problems
We need to understand how Matlab
works to solve more complicated
problems

22NovTZK
25
MATLAB allows you to assign a value to a
variable
A=3
Should be read as A is assigned a value of 3
Use the variables in subsequent calculations

22NovTZK
26
All names must start with a letter
They may contain letters, numbers and the
underscore ( _ )
Names are case sensitive
There are certain keywords you can’t use

22NovTZK
27
iskeyword
ans =

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

22NovTZK
28
Matlab will let you use built-in function names
as variables – but it’s a really bad idea

sin = 3 changes sin from a


function to a variable name

22NovTZK
29
test
Test
ifx
x
my-book
my_book
x
Thisisoneverylongnamebutisitstillallowed?
x
1stgroup
group_one
zzaAbc x x
z34wAwy?12#
sin bad
log idea

22NovTZK
30
Group of numbers arranged into rows and
columns
Single Value (Scalar)
◦ Matrix with one row and one column
Vector (One dimensional matrix)
◦ One row or one column
Matrix (Two dimensional)

22NovTZK
31
You can use MATLAB like you’d use a calculator

Comman
d Prompt
>> 9 + 10
ans=19
Result

22NovTZK
32
To define a variable a we might type
a=1+2
which should be read as:
a is assigned a value of 1+2

22NovTZK
33
In algebra the equation
x=3+5
means that both sides are the same
In computers when we say
x=3+5
we are telling the machine to store
the value on the right hand side of
the equation in a memory location,
and to name that location x

22NovTZK
34
Yes!!!
In algebra this is not a true statement
x=x+1
In computers (assignment statements) it
means replace the value in the memory
location named x, with a new value equal to
x+1

22NovTZK
35
Same as you’ve learned in math class
Same as your calculator
◦ Parentheses first
◦ Exponentiation
◦ Multiplication / division
◦ Addition / subtraction

22NovTZK
36
5*(3+6) = 45

5*3+ = 21
6
White space does not
matter!!!
5*3 + = 21
6

22NovTZK
37
Use only ( )
{ } and [ ] mean something different
MATLAB does not assume operators

5 * (3+4) not
5(3+4)

22NovTZK
38
5*6/6* = 25
5
5*6/(6*5) = 1

22NovTZK
39
r = radius h = height
r=5 h = 10

π r2
2π r * h

π r2

SA  2r  2rh  2r ( r  h )


2

22NovTZK
40
22NovTZK
41
22NovTZK 42
22NovTZK 43
22NovTZK 44
Using Matlab as a glorified calculator is OK, but
its real strength is in matrix manipulations

22NovTZK
45
To create a row vector, enclose a list of
values in brackets

22NovTZK
46
You may use either a space or a
comma as a “delimiter” in a row
vector

22NovTZK
47
Use a semicolon as a delimiter to create
a new row

22NovTZK
48
Use a semicolon as a delimiter to create
a new row

22NovTZK
49
Hint: It’s easier to keep track of how many
values you’ve entered into a matrix, if you
enter each row on a separate line. The
semicolons are optional

22NovTZK
50
While a complicated matrix might have to be
entered by hand, evenly spaced matrices can
be entered much more readily. The command
b= 1:5
or the command
b = [1:5]
both return a row matrix

22NovTZK
51
The default increment is 1, but if you
want to use a different increment
put it between the first and final
values

22NovTZK
52
linspace
logspace

22NovTZK
53
number of elements in
the array
Initial value in the Final value in the
array array

22NovTZK
54
number of elements in
the array
Initial
Initialvalue
valueininthe
the Final value in the
array
arrayexpressed
expressed array expressed
asasa apower
powerofof1010 as a power of 10

22NovTZK
55
You can include mathematical operations inside
a matrix definition statement.
For example

a = [0: pi/10: pi]

22NovTZK
56
Matrices can be used in many calculations
with scalars
There is no confusion when we perform
addition and subtraction
Multiplication and division are a little different
In matrix mathematics the multiplication
operator (*) has a very specific meaning

22NovTZK
57
22NovTZK
58
Addition between arrays is
performed on
corresponding elements

22NovTZK
59
Multiplication between
arrays is performed on
corresponding elements if
the .* operator is used

22NovTZK
60
Matlab interprets * to mean matrix
multiplication. The arrays a and b are
not the correct size for matrix
multiplication in this example
22NovTZK
61
Array multiplication .*
Array division ./
Array exponentiation .^

In each case the size of the arrays must match

22NovTZK
62
For example, assume you have a list of angles in
degrees that you would like to convert to
radians.
◦ First put the values into a matrix.
◦ Perform the calculation

22NovTZK
63
22NovTZK
64
Either the * or the .* operator can be
used for this problem, because it is
composed of scalars and a single
matrix

The value of pi is built into Matlab as a


floating point number, called pi

22NovTZK
65
Because pi is an irrational number, it can not be
expressed exactly with a floating point
representation
The Matlab constant , pi, is really an
approximation.
If you find sin(pi) Matlab returns a very small
number.

22NovTZK
66
The transpose operator changes rows to
columns or vice versa.

22NovTZK
67
The transpose operator
makes it easy to create
tables

22NovTZK
68
table =[degrees;radians]’ would have given
the same result

22NovTZK
69
The transpose
operator works on
both one dimensional
and two dimensional
arrays

22NovTZK
70
22NovTZK 71
Scientific Notation
◦ Although you can enter any number in decimal
notation, it isn’t always the best way to represent
very large or very small numbers
◦ In Matlab, values in scientific notation are
designated with an e between the decimal number
and exponent. (Your calculator probably uses similar
notation.)

22NovTZK
72
It is important to omit blanks between
the decimal number and the
exponent. For example, Matlab will
interpret
6.022 e23
as two values (6.022 and 1023 )

22NovTZK
73
Multiple display formats are available
No matter what display format you choose,
Matlab uses double precision floating point
numbers in its calculations
Matlab handles both integers and decimal
numbers as floating point numbers

22NovTZK
74
The default format is called short
If an integer is entered it is displayed without
trailing zeros
If a floating point number is entered four
decimal digits are displayed

22NovTZK
75
Changing the format affects all subsequent
displays
◦ format long results in 14 decimal digits
◦ format bank results in 2 decimal digits
◦ format short returns the display to the default 4
decimal digits

22NovTZK
76
When numbers become too large or too small
for Matlab to display using the default format,
it automatically expresses them in scientific
notation
You can force scientific notation with
◦ format short e
◦ format long e

22NovTZK
77
For long and short formats, a common scale
factor is applied to the entire matrix if some of
the elements become very large, or very small.
This scale factor is printed along with the scaled
values.

22NovTZK
78
Common
Scale Factor

22NovTZK
79
format +
format rat

22NovTZK
80
The format command also allows us to control
how tightly information is spaced in the
command window
◦ format compact
◦ format loose – (default)
Most of the examples in this presentation use
format compact
Notice that the value of A is still
being displayed using the rat
format, because we haven’t
changed it back to format short

22NovTZK
81
If you save a MATLAB session, all that is saved
are the values of the variables you have named

22NovTZK
82
If you want to save to another format, such a
s .dat, you need to explicitly tell the program

save <file_name> <variable_list> -ascii

22NovTZK
83
If you want to save your work, you need to
create an M-file
File->New->M-file
Type your commands in the edit window that
opens

22NovTZK
84
The file is saved into the current directory
It runs in the command window

22NovTZK
85
22NovTZK
86
Be sure to comment your code
◦ Add your name
◦ Date
◦ Section #
◦ Assignment #
◦ Descriptions of what you are doing and why

22NovTZK
87
The % sign identifies comments

You need one on each line

22NovTZK
88
Introduced the MATLAB Windows
Basic matrix definition
Save and retrieve MATLAB data
Create and use script M-files

22NovTZK
89
22NovTZK 90
22NovTZK 91
22NovTZK 92
22NovTZK 93
22NovTZK 94
22NovTZK 95
22NovTZK 96
22NovTZK 97
22NovTZK 98
22NovTZK 99
10
22NovTZK 0
22NovTZK 101
10
22NovTZK 2
10
22NovTZK 3
10
22NovTZK 4
10
22NovTZK 5

You might also like