Uploaded by richien1215

Lecture1 Introduction to Matlab

advertisement
Chapter 1: Introduction to MATLAB
Part I – COMPUTING SYSTEM
ENIAC
Computing System
• Computing system: a system of one or
more computers and associated
software with common storage.
• Components: hardware, software,
data and user.
Hardware
Hardware refers to the physical parts of the computing
system that have mass (i.e. the parts you can see and
touch):
– Computer
– Display
– Mouse
– Printer
–…
Hardware
Von Neumann architecture
-- Input device(s)
-- Output device(s)
-- CPU (Central Processing
Unit) consisting of:
• Control Unit
• ALU (Arithmetic Logic Unit)
From Wiki
Hardware
Harvard architecture
a computer architecture with
physically
signal pathways
for instructions and data
From Wiki
Moore’s Law
Gordon Moore (1965): The
observation that the number
of transistors in a dense
integrated circuit doubles
approximately every two
years.
Software Interface to
Computer Hardware
Copyright © 2012
Pearson Education,
Inc.
Software
Computer software refers to programs that reside and execute
electronically on the hardware.
– Compilers
System Software
– Translate source code
– Operating systems
– Provide the HCI (Human Computer Interface)
– Application programs
– Provide problem solutions
Key Terms
• Source Program
– printable/Readable Program file
• Object Program
– nonprintable machine readable file
• Executable Program
– nonprintable executable code
Chapter 1: Introduction to MATLAB
Part II – DATA
Base: number of unique identifiers to represent distinct digits.
Humans
Computers
(Decimal System)
Base 10: { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }
(Binary System)
Base 2: { 0, 1 }
(Octal System)
Base 8: { 0, 1, 2, 3, 4, 5, 6, 7 }
(Hexadecimal System) Base 16: { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
A, B, C, D, E, F }
Each of these base systems is just another way of writing down the same
number.
When you convert a number between different bases, it should still have
the same value.
Base ten number system
It's generally easiest to understand the concept of different bases by
looking at base 10.
When we have a number in base 10, each digit can be referred to as the
ones digit, the tens digit, the hundreds digit, the thousands digit, or so
forth.
For instance, in the number 432,
- 4 is the hundreds digit,
- 3 is the tens digit,
- 2 is the ones digit.
Base ten number system
Another way to think about this is to rewrite 43210 as:
4 x 102
+ 3 x 101
+ 2 x 100
To evaluate a number in a base system, multiply successive values by
increasing powers of the base.
Base two (binary) number system
• Two binary digits (0,1)
• Each digit multiplies a power of two
– Example:
101102 = 1*24 +
=
=
=
0*23 + 1*22 + 1*21 + 0*20
1*16 + 0*8 + 1*4 + 1*2 + 0*1
16 + 0 + 4 + 2 + 0
2210
Base eight number system
• Eight octal digits (0,1,2,3,4,5,6,7)
• Each digit multiplies a power of eight
– Example:
2458 = 2*82 + 4*81 + 5*80
= 2*64 + 4*8 + 5*1
= 128 + 32 + 5
= 16510
Base sixteen number system
• Sixteen hex digits (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F)
• Each digit multiplies a power of sixteen
– Example:
2FB16 = 2*162 + F*161 + B*160
= 2*256 + F*16 + B*1
= 512 + 240 + 11
= 76310
Base systems are different ways to represent a quantity.
Base:
B = 10
Number of digits: N = 3
0123456789
100 101 102
12310 = 1*100 + 2*10 + 3*1
= 1*102 + 2*101 + 3*100
Base systems can be expressed as:
BN-1 … B3 B2 B1 B0
Exercises:
Convert 10112 , 10118 and 101116 to Base 10 systems.
To go other way, e.g. 1317810, how to convert it to base 16?
1. Find the next highest power of base:
160 = 1, 161 = 16, 162 = 256, 163 = 4096, 164 = 65536
164
163
13178
=
4096
3.2
2. Since 13178 <
= 65536, use
= 4096,
There are “3” units of 4096 in this number, so record this and remove that
component: 13178 – 3*(4096) = 890 .
890
3. Since 162 = 256, 256
= 3.47
There are “3” units of 256 in this number, so record this and remove that
component: 890– 3*(256) = 122 .
4. Since 161 = 16, 122
= 7.625, 122 – 7*(16) = 10
16
10
5. Since 160 = 1, 1 = 10, Symbol of 10 in base 16 is A
The number 1317810 is written as 337A16 in basis 16.
Convert a number from Base 10 to Base N:
1. Find largest value of Bj that is less than the number
32110
Base 5 4
3
2
1
0
625 125 25
5
1
2. Remove as many as that value that you can
−
321
250 (= 2 ∗ 125)
71
3. Repeat to next lower value.
71
− 50 (= 2 ∗ 25)
21
− 20 (= 4 ∗ 5)
1 (= 1 ∗ 1)
32110 = 22415
Question?
Exercises:
Convert 33810 to Base 8 systems.
Memory Storage
The most fundamental unit of computer memory is the bit.
1 bit (Binary digit) = 0 or 1
0
1
1 bit
1 bit
0110
01101011
4 bits
8 bits
A collection of 8 bits is called a byte.
A collection of 4 bytes, or 32 bits, is called a word.
1 byte
8 bits
2 bytes
16 bits
4 bytes
32 bits (word)
8 bytes
64 bits
The number of bytes and words used for an individual data value depends on
the storage format, the operating system, and even the computer hardware.
• A single letter or character of text takes up one byte;
- The text “hello” would take up 5 bytes of storage, one per character.
- The text “12345” would also require 5 bytes.
• An integer, or whole number, takes up one word.
- The integer 12,345 would take up 4 bytes (1 word),
• A real or decimal number takes up one or two words depending on how it
is stored.
- The real number 123.45 would take up 4 or 8 bytes.
Unsigned Integer: either positive or zero.
An unsigned integer containing n bits can have a value between 0 and
(which is 2n different values).
An 8 bit unsigned integer: 0~28–1, or 255.
(Short) integers (16 bits): 0~216-1, or 65,535.
Long integers (32 bits): 0~232-1, or 4,294,967,295.
Double integers (64 bits): 0~264–1, or 18,446,744,073,709,551,615.
Signed Integer: Negative values allowed.
In computers, the left most bit of a signed integer is called the "sign bit".
If a signed integer has n bits, it can contain a number between -2n-1 and +(2n-11).
An 8 bit signed integer: -128 (-28-1) to 127 (28-1–1)
Signed short integer: -32,768 to +32,767
Signed long integer: -2,147,483,648 to +2,147,483,647
Signed double integer: -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
How to represent a negative integer?
One sign bit is allocated to represent number's sign:
- Sign bit = 0 : a positive number or zero,
- Sign bit = 1 : a negative number.
- The remaining bits in the number indicate the magnitude (or absolute
value).
Negative integers are often represented in their two’s complement form.
The two’s complement of an integer is formed by negating all of the bits
and adding one.
Two’s complement
• The two's complement of an N-bit number is defined as
its complement with respect to 2N.
• Form the two’s complement representation for the value -12710
12710 = 011111112
Negate bits: 10000000
Add 1: 10000001
• two’s complement is 1000 00012
Two’s complement
Add 12710 to -12710
011111112
+ 100000012
= 000000002
12710

+ -12710
= 010
Eight-bit two's complement
Binary value Two's complement interpretation
Unsigned interpretation
00000000
0
0
00000001
1
1
…
…
…
01111110
126
126
01111111
127
127
10000000
−128
128
10000001
−127
129
10000010
−126
130
⋮
⋮
⋮
11111110
−2
25
11111111
−1
255
Exercise:
Form the 2’s complement representation for the value -210
Signed types:
- Enable you to work with negative integers as well as positive;
- But cannot represent as wide a range of numbers as the unsigned
types (one bit is used to designate a positive or negative sign for
number).
Unsigned types:
- Give you a wider range of numbers;
- But these numbers can only be zero or positive.
Here are the eight integer classes, the range of values, and the
MATLAB conversion function required to create that type:
Class
Range of Values
Conversion Function
Signed 8-bit integer
-27 to 27-1
int8
Signed 16-bit integer
-215 to 215-1
int16
Signed 32-bit integer
-231 to 231-1
int32
Signed 64-bit integer
-263 to 263-1
int64
Unsigned 8-bit integer
0 to 28-1
uint8
Unsigned 16-bit integer
0 to 216-1
uint16
Unsigned 32-bit integer
0 to 232-1
uint32
Unsigned 64-bit integer
0 to 264-1
uint64
>> A = uint8(3)
A =
3
B = uint16(3)
>> B =
3
C = uint32(3)
>> C =
3
>> whos
Name
Size
A
B
C
1x1
1x1
1x1
Bytes
1
2
4
Class
uint8
uint16
uint32
Floating Point Numbers
Floating point number: a real number (that is, a number that can
contain a fractional part). i.e. 3.14.
In decimal system, very large/small floating point numbers can be
shown with a mantissa and an exponent. i.e. 0.12*108, 0.45*10-6
Here the 0.12/0.45 is the mantissa and the 108/10-6 is the exponent.
The mantissa holds the main digits, and the exponent defines where
the decimal point should be placed.
Matlab represents floating-point numbers in either single-precision format
or double-precision format (default).
Single precision: 4 bytes (32 bits)
Double precision: 8 bytes (64 bits)
single
double
sign
1 bit
1 bit
exponent
8 bits
11 bits
mantissa
23 bits
52 bits
Total
32 bits (4 bytes)
64 bits (8 bytes)
Single precision:
Floating points numbers have finite precision.
single
double
Mantissa
23 bits
52 bits
Number of decimal precision
7
15
Finite precision is the fundamental limit of digital computers.
>> A = single(101239.216);
>> B = single(101239.215);
>> A - B
ans =
0
Other Pitfalls
1. Integer arithmetic:
>> i = int8(2);
>> j = int8(3);
>> k = int8(5);
>> i/j
ans =
1
>> i/k
ans =
0
>> x = 2 / 3
x =
0.6667
>> y = 2 / 5
y =
0.4000
>> class(x)
ans =
double
>> class(y)
ans =
double
>> 2./3.
ans =
0.6667
>> 2./5.
ans =
0.4000
Other Pitfalls
2. Division by difference of two small numbers:
>> A = single(101239.216)
A =
1.0124e+05
>> B = single(101239.215)
B =
1.0124e+05
>> 1/(A - B)
ans =
Inf
>> C = double(101239.216)
C =
1.0124e+05
>> D = double(101239.215)
D =
1.0124e+05
>> 1/(C -D)
ans =
1.0000e+03
Other Pitfalls
3. Round off error
The decimal number 4/3 is not exactly representable as a binary
fraction. For this reason, the following calculation does not give zero.
>> e = 1 - 3*(4/3 - 1)
e =
2.2204e-16
Part II: Data
•
•
•
•
•
Base System;
Memory Storage;
Data Type;
Negative Integer;
Finite Precision.
Chapter 1: Introduction to MATLAB
Part III -- MATLAB
What is MATLAB?
• MATLAB stands for Matrix Laboratory. As the name suggests, MATLAB
is especially designed for matrix computations: solving systems of linear
equations, computing eigenvalues and eigenvectors, factoring matrices, and
so forth.
• It is an interactive software system for numerical computations,
programming and graphics.
What is MATLAB?
• Matlab is a computer language/application designed for scientific
computation. It can be used in two ways, interactively (like a calculator) or
non-interactively (as a programming language).
As a command-line calculator, Matlab can do:
– Simple arithmetic operators
• +-*/^
– Basic mathematic functions
• sin(), log(), log10(), exp(), rem()
What is MATLAB?
For example,
>> x = 1.5+2*3^2/4
x=
6
will result in x being given the value 6.
>> sin(pi/6)+cos(pi/3)
ans =
1
Exercises:
1.
12−3
5+1
2. 32 + 5
3. 32+5
The MATLAB System
MATLAB system consists of five main parts:
1. Desktop Tools and Development Environment
2. Mathematical Function Library
3. The Language
4. External Interfaces
5. Graphics
The MATLAB System
1. Desktop Tools and Development Environment
– Includes the Command Window, an editor and debugger, a code
analyzer, browsers for viewing help, the workspace, and other
2. Mathematical Function Library
– Vast collection of computational algorithms ranging from
elementary functions, like sine, cosine, and complex arithmetic,
more sophisticated functions like matrix inverse, matrix
eigenvalues, and fast Fourier transforms.
– https://www.mathworks.com/help/matlab/functionlist.html#r
onsive_offcanvas
The MATLAB System
3. The Language
The MATLAB language is a high-level matrix/array language with
control flow statements, functions, data structures, input/output,
and object-oriented programming features.
4. External Interfaces
– The external interfaces library allows you to write C and
programs that interact with MATLAB.
– http://www.unc.edu/depts/case/BMELIB/apiextMATLAB6p1.
pdf
The MATLAB System
5. Graphics
– MATLAB has extensive facilities for displaying data as graphs, as
well as editing and printing these graphs. It also includes
that allow you to customize the appearance of graphics as well as
build complete graphical user interfaces on your MATLAB
applications.
– http://courses.washington.edu/css485/graphg.pdf
Why MATLAB?
1. It is very easy to use Matlab.
- The grammar is simple.
-Many program development tools are provided to make the
program easy to use, including editor/debugger, online
documentation, workspace browser, and so forth.
Why MATLAB?
2. Matlab has many predefined functions to use in various applications.
- More than 1000 functions in the basic Matlab;
- These functions solve very complex problems, such as differential
equations, matrix and vector formulas, and so on;
- The functions are efficient, robust.
Why MATLAB?
Why MATLAB?
Suppose we want to multiply two matrices A and B
A11
A21
AIxJ = …
…
AI1
A12
…
…
…
…
…
…
Aij
…
...
…
…
…
…
…
A1J
…
…
…
AIJ
B11
B21
BJxK = …
…
BJ1
B12 …
…
…
… Bjk
…
…
… ...
…
…
…
…
…
B1K
…
…
…
BJK
C = A·B is a new matrix where
Cik = AB
ik =
J
j=1 AijBjk
= A 𝑖1B1j + A𝑖2B2j+ … + A𝑖𝑗Bjk
C11
C21
C = A·B = …
…
CI1
C12 …
…
…
… C
ik
…
…
… ...
…
…
…
…
…
C1K
…
…
…
CIK
Why MATLAB?
- Comparison with C++
C++ Code for matrix multiplication (O(I*K+I*J*K))
C11 C12
C21 …
…
𝐂= …
…
…
CI1 …
…
…
Cij
…
...
…
…
…
…
…
C1K
…
…
…
CIK
If I =20, J= 20, K= 30
I*K+I*J*K = 12600 !
Why MATLAB?
- Comparison with C
Matlab Code for matrix multiplication:
Why MATLAB?
3. Toolboxes extend functions’ capacity with many more functions in
various specialties.
- Signal Processing,
- Image Processing;
- Parallel Computation,
- Curve Fitting,
- Control Theory,
- Simulation,
- Optimization.
https://www.mathworks.com/search/site_search.html?c%5B%5D=enti
re_site&q=toolbox
Why MATLAB?
4. Matlab offers a variety of ways to visually display data. It is easy to
make the visualization of results immediately available.
https://www.mathworks.com/help/matlab/ref/plot.html
6. Matlab has interfaces to C/C++, Java, .NET, Python, SQL, Hadoop,
and Microsoft Excel.
Disadvantage:
1. A full copy of Matlab is much more expensive than a
conventional Fortran or C compiler.
2. Matlab is an interpreted language and therefore it executes
more slowly than compile language.
Source code
compiler
Interpreter
(line by line)
Object code
Machine code
execute
Machine code
execute
MATLAB is an interpreted language
Advantages
Disadvantages
•easy to learn
•easy to debug
•No compiler
•run quite slowly
•use a lager RAM.
What types of companies use MATLAB?
• MATLAB is used in many industries.
- Large aerospace and defense companies.
- Major car manufacturers.
- Computer vision-related companies.
- Robotics.
• Some financial companies.
MATLAB Desktop
•Command Window
•Command History Window
•Editor Window
•Figure Window
•Current Folder Browser
•Workspace
•Toolstrip
•Help Browser
Current Folder Browser
Details
Window
MATLAB file path
Command Window
Workspace
Line Numbers
* means the file is not saved
Help files
Comments
Debugging Tools
Editor Window
Command Window:
As you work in MATLAB, you issue commands that create variables
and call functions. For example, create a variable named a by typing
this statement at the command line:
a=1
MATLAB adds variable a to the workspace and displays the result in
the Command Window.
a=1
Create a few more variables.
b = 2*a
c = a + b^2
Command History Window:
The Command History window displays a log of statements that
you ran in the current and previous Matlab sessions.
You can recall previous commands by pressing the up- and
down-arrow keys, ↑ and ↓.
Press the arrow keys after you type the first few characters of a
command.
For example, to recall the command b = 2, type b, and then press
the up-arrow key.
Editor Window:
You can write a program or script called a m-file (has a .m extension) in
the Editor Window.
The Editor Window is a word processor specifically designed for
Matlab commands so it automatically formats certain things for you.
Figure Editor :
• MATLAB figures include the ability to fine-tune the appearance of
the plot, zoom, etc.
• The figures can be saved in a wide variety of formats.
- .fig file is preferred.
Current Directory :
The directory that MATLAB is currently working in. This is where anything
you save will go by default, and it will also influence what files MATLAB
can see.
Variable Editor or Array Editor:
Opens variables in an Excel-like format, and is useful for checking what
data is in which column/row, checking that value is where you meant it to
be, etc. Data can also be edited or created in this window. Double-clicking
on a variable in the Workspace will open it for editing.
Workspace:
Work space shows the all the variables that we have currently
defined and some basic information about each variable, including its
dimensions and values.
Managing the workspace:
In order to display a list of the variables currently in the
memory, type
>> who
If you want to know more details about size, space allocation,
and class of the variables.
>> whos
Managing the workspace:
-
The contents of the workspace persist between the executions of
separate commands.
- The results of one problem to have an effect on the next one.
- To avoid this possibility, it is a good idea to issue a clear
command at the start of each new independent calculation.
>> clear
The command clear frees up system memory.
Toolstrip:
The Toolstrip organizes MATLAB functionality in a series of tabs.
Toolstrip - Home:
The Home tab is where you do general purpose operations:
• Creating new files,
• Importing data,
• Managing your workspace,
• Setting your Desktop layout.
Toolstrip - Plots:
The Plots tab can be used to create MATLAB Plots.
Toolstrip - Apps:
The Apps tab is the place you go to run interactive MATLAB applications.
Getting help:
To view the online documentation, select MATLAB Help from Help
menu or MATLAB Help directly in the Command Window.
Information about any command is available by typing:
>> help
Use on-line help to request info on a specific function
>> help sqrt
The help command searches for an exact function name match.
Getting help:
Another way to get help is to use the lookfor command.
The lookfor command searches the quick summary information in each
function for a match.
>> help inverse
>> lookfor inverse
Questions?
Download