INTRODUCTION TO MATLAB - PowerPoint PPT Presentation

1 / 55
About This Presentation
Title:

INTRODUCTION TO MATLAB

Description:

It offers a powerful programming language, excellent graphics, and a ... Matlab = Mat rix Lab oratory. Originally a user interface for numerical linear algebra ... – PowerPoint PPT presentation

Number of Views:132
Avg rating:3.0/5.0
Slides: 56
Provided by: frankr52
Category:

less

Transcript and Presenter's Notes

Title: INTRODUCTION TO MATLAB


1
INTRODUCTION TO MATLAB
MATLAB is a software package for computation in
engineering, science, and applied
mathemat-ics. It offers a powerful programming
language, excellent graphics, and a wide range of
expert knowledge.
2
INTRODUCTION TO MATLAB
  • A software environment for interactive numerical
    computations
  • Examples
  • Matrix computations and linear algebra
  • Solving nonlinear equations
  • Numerical solution of differential equations
  • Mathematical optimization

3
INTRODUCTION TO MATLAB
  • Statistics and data analysis
  • Signal processing
  • Modelling of dynamical systems
  • Solving partial differential equations
  • Simulation of engineering systems
  • ETC.

4
INTRODUCTION TO MATLAB
Matlab Background Matlab Mat rix Lab
oratory Originally a user interface for numerical
linear algebra
5
INTRODUCTION TO MATLAB
optional windows
workspace current directory
type commands here
command window
Multipaneled MATLAB desktop
6
Matlabs help features
type help at the command prompt and Matlab
returns a list of help topics
7
Matlabs help features
gtgt help lang
Matlabs language constructs
8
Matlabs help features
gtgt help for
how to use Matlabs for statement
9
Matlabs help features
you can also access on-line help by clicking
the question mark in the toolbar
separate window
10
Basic Command and Syntax
all variables are stored in 32bit floating point
format
no distinction between real and integer
gtgta 3
same assignment for a
gtgta 3.0
Matlab is case sensitive
gtgtA3
A?a
gtgta2
11
If you type in a valid expression and press
Enter, MATLAB will immediately execute it and
return the result. gtgt 22 ans 4 gtgt 42 ans
16 gtgt sin(pi/2) ans 1
12
MATLAB Variables
can use numbers and underscore in variable names
gtgtcase346.45
OK
gtgtcase_346.45
names must start with a letter
results in a syntax error
gtgt34case23.45
string (text) variables enclosed in single
quotes. The variable is stored as array of
characters
gtgttitleThis is the title
13
INTTRODUCTION gtgt x sqrt(3) x 1.7321 gtgt
atan(x) ans 1.0472 gtgt pi/ans ans 3
14
MATLAB Variables
if a variable is defined, typing the variable
name returns its value
gtgta45.57
gtgta
a 45.57
Matlab returns the value
to clear a variable from memory
gtgta4
gtgtclear a
15
Arrays and Matrices The simplest way to construct
a small array is by enclosing its elements in
square brackets. gtgt A 1 2 3 4 5 6 7 8 9 A
1 2 3 4 5 6 7 8 9
16
MATLAB Variables
Matlab will echo commands unless a semi-colon
is used
gtgta23.2 gtgt
gtgta23.2 a 23.2 gtgt
Matlab echoes the command
17
gtgt b 010 b 0 1 0 gtgt A b ans 1 2 3 0 4
5 6 1 7 8 9 0
18
MATLAB Variables
Vectors
column vectors
row vectors
gtgta123 gtgta a 1 2 3
gtgta1,2,3 gtgta a 1 2 3
use semi-colon to separate rows
use comma to separate columns
19
MATLAB Variables
Matrices
2-dimensional matrices
gtgta1,2,34,5,6 gtgta a 1 2 3
4 5 6
again, separate columns with commas and rows with
semi-colons
20
MATLAB Variables
Indexing Matrix elements
A vector is a special type of matrix
row vector is a 1 x n matrix, 1 row n columns
column vector is a n x 1 matrix, n rows 1 column
gtgta1,2,3 gtgta(2) ans 2
could also reference by a(1,2)
note, a(2,1) would produce an error because a
only has one row
21
MATLAB Variables
Indexing Matrix elements
more examples
gtgta1,2,34,5,6
assigning
addressing
gtgta(2,2)9 gtgta a 1 2 3 4 9
6
gtgta(2,3) ans 6
22
USAGE Solutions using Matrix Operation 3x2y-z4 2
x4y-2z -6 6x-7y4z8 gtgt D3 2 -1 2 4 -2 6 -7
4 D 3 2 -1 2 4 -2
6 -7 4
23
gtgt E4 -68 E 4 -6 8 gtgt
XD\E X 3.5000 -39.0000 -71.5000
24
An especially important construct is the colon
operator. gtgt 18 ans 1 2 3 4 5 6 7 8 gtgt
0210 ans 0 2 4 6 8 10 gtgt 1-.5-1 ans
1.0000 0.5000 0 -0.5000 -1.0000
25
MATLAB Variables
complex-valued numbers
Typically, the variable i or j is used to
represent the complex variable e.g.
Then, a complex number is represented as
z a ib
Re(z) a
Im(z) b
26
An especially important construct is the colon
operator. gtgt 18 ans 1 2 3 4 5 6 7 8
27
MATLAB Variables
complex-valued numbers
Unless i or j has been previously defined, Matlab
assigns i and j the complex variable value
In Matlab, a complex variable is represented in
the following format
(assuming all variables are cleared)
gtgtz23i56 gtgtz z 23.00 56.00i
gtgtz23j56 gtgtz z 23.00 56.00i
Matlab always uses the symbol i to represent a
complex number
28
MATLAB Variables
complex-valued numbers
What happens in this case?
What happens in this case?
gtgti3 gtgt z23i56 gtgtz z
gtgtasqrt(-1) gtgtz23a56 gtgtz z
29
MATLAB Variables
complex-valued numbers
Note, a real-valued number is a special case of a
complex-valued number
assigning any element of a matrix as
complex-valued makes the entire matrix
complex-valued
gtgta1,2 gtgta a 1 2
gtgta(1)1i5 gtgta a 1.005.00i
2.000.00i
30
MATLAB Variables
Advanced data types
n-dimensional arrays structures cell arrays
31
MATLAB Operations
Basic operations
addition subtraction
- multiplication division right
division / left division \
?
gtgta3b4 gtgtc1a/b gtgtc2a\b
c10.75
so, be careful!
c21.3333.
32
MATLAB Operations
Mixed Real and Complex valued Variables
if both variables are real-valued, a real-valued
result is obtained
if one variable is complex-valued, Matlab recasts
the real variable as complex and then performs
the operation. The result is complex-valued
however, the type casting is done internally, the
real-valued variable remains real after the
operation
33
MATLAB Operations
Other (Scalar) Operations
Math representation
Matlab interpretation
gtgtzyx
gtgtyexp(x)
gtgtylog(x)
gtgtylog10(x)
gtgtyasin(x) gtgtyacos(x) gtgtyatan(x)
gtgtysin(x) gtgtycos(x) gtgtytan(x)
34
MATLAB Operations
Examples
gtgtyx0.5 gtgtyx(1/2) gtgtysqrt(x)
All variables in the preceding operations can be
real or complex, negative or positive
for x lt 0, y is complex. Matlab assumes you
allow complex valued numbers. If y is not to be
complex, you must provide error checking.
35
MATLAB Operations
Matrices
Only matrices of the same dimension can be added
and subtracted
For multiplication, the inner dimensions must be
the same
No error
Error
gtgtDAC gtgtDAB gtgtDBA
gtgtDAB gtgtDA-B gtgtDAC gtgtDCA
Matrix multiplication not commutative
36
MATLAB Operations
Left(\) and Right(/) Matrix division
Math representation
Matlab interpretation
gtgtCA\B
gtgtCB/A
Remember, A must be square and full rank
(linearly independent rows/columns)
37
MATLAB Operations
Matrix Transpose
Math representation
Matlab interpretation
gtgtCA
For complex-valued matrices, complex conjugate
transpose
gtgtba
gtgtBA
38
MATLAB m-files
Two types of m-files
script files collection of commands that Matlab
executes when the script is run
function files collection of commands which
together represent a function, a procedure or a
method
Both types are separate files with a .m
extension
39
MATLAB m-files
To create an m-file, open the Matlab text editor
Click on the page icon
The Matlab text editor window will open
40
MATLAB m-files
Script Files
On the command line
In the script file named test.m
gtgtx3.0 gtgtyx2 gtgty y 9.0 gtgt
On the command line
gtgttest y 9.0 gtgt
41
MATLAB m-files
Script Files
script files share the workspace memory
test.m script
gtgtx5.0 gtgttest gtgty y 25.0 gtgt
42
MATLAB m-files
Script Files
script files can call other script files
inner.m script
gtgtoutter y 36.0 gtgt
outter.m script
43
MATLAB m-files
Function Files
Matlab identifies function files from script
files by using the function and return
keywords
the name of the function file must be the same
name as the function
44
MATLAB m-files
Function Files
The function file x2.m
gtgtr3 gtgtdx2(r) gtgtd d 9.0 gtgt
gtgthx2(4.2) gtgth h 17.64 gtgt
45
MATLAB m-files
Function Files
Multiple Inputs and Outputs
outputs in square brackets,
inputs in parentheses ( )
46
MATLAB m-files
Function Files
variables created in the function are not
retained in the workspace, except for the output
variables
the function does not have access to workspace
variables, except for the inputs
variables passed to the function are copies of
the workspace variables. Changing their value
inside the function has no effect on their value
in the workspace.
47
MATLAB Flow Control
The while and if statements
if expression statements end
if expression statements1 else statements2 end
while expression statements end
Matlab evaluates expression as logical true or
false
false equivalent to zero true equivalent to
any non-zero number
statements, any valid Matlab command
48
MATLAB Flow Control
evaluating expression
conditional operators
any valid equation
equal to lt less than gt greater than lt
less than or equal to gt greater than or equal
to not equal to
a4 b5 c5 if ab if b-c
True
False
watch out for round-off and word length error
logical operators
and or
if sin(0) if sin(pi) sin(pi) 1.22e-16
False
True
while(3lta)(alt5)
49
MATLAB Flow Control
The for statement
for index start increment
end statements end
index, start, increment, and end do not need to
be integer valued
increment is optional, if increment is not
specified increment defaults to 1
index can be incremented positive (increment gt 0)
or negative (increment lt 0)
loop stops when index gt end (or index lt end)
50
MATLAB Flow Control
example
script file to cycle through x values
function file to generate the y values
51
MATLAB Plotting
Basic 2D plotting functions
plot(x1,y1,x2,y2,x3,y3.....) xlabel(x axis
name) ylabel(y axis name) title(graph name)
Additional functions
grid on grid off axis(xmin,xmax,ymin,ymax)
52
MATLAB Plotting
example y sin(t)
the plot function alone
53
MATLAB Plotting
example y sin(t)
script file to generate a graph of y sin(t)
54
MATLAB Plotting
example y sin(t)
function file to generate a graph of y sin(t)
gtgtgraphsin gtgt
55
MATLAB Plotting
Adding a Legend for multiple graphs
legend remembers the order the graphs were
plotted
Write a Comment
User Comments (0)
About PowerShow.com