lecture4 matlab
TRANSCRIPT
-
8/10/2019 Lecture4 MATLAB
1/59
Introduction to MATLAB
Dr. Ben Mertz
FSE 100Lecture 4
-
8/10/2019 Lecture4 MATLAB
2/59
Data Analysis
MATLAB is a numerical computing environment and
programming language
2-D and 3-D graphics
Numeric Computation
Algorithm Development
And more
http://www.orbisnap.com/vrtdesc.html
http://www.mathworks.com
Design, Problem Solving;
Custom GUIs;
Modeling and Analysis;
Signal and Image Processing
-
8/10/2019 Lecture4 MATLAB
3/59
Algorithm Development
Data Analysis
2-D and 3-D graphics
Numeric Computation And more
Design, Problem Solving
Custom GUIs
Modeling and Analysis Signal and Image
Processing
MATLAB is
http://www.orbisnap.com/vrtdesc.html http://www.mathworks.com
a numerical computing environment and programming language
-
8/10/2019 Lecture4 MATLAB
4/59
MATLAB also does
http://jennifertsau.com/uncategorized/matlab-fun/http://www.mathworks.com
http://jennifertsau.com/uncategorized/matlab-fun/http://jennifertsau.com/uncategorized/matlab-fun/http://jennifertsau.com/uncategorized/matlab-fun/http://jennifertsau.com/uncategorized/matlab-fun/http://jennifertsau.com/uncategorized/matlab-fun/ -
8/10/2019 Lecture4 MATLAB
5/59
Command window
with command lines
and results
Command histories
Workspace: variables
Current
Directory
-
8/10/2019 Lecture4 MATLAB
6/59
Mathematical
ComputationsExpression MATLAB Commands
Addition
3+5Subtraction 4-9Multiplication 3*2Division
9/3Square Root sqrt(4)Power 2^4
3 2
9 3
442
3 54 9
-
8/10/2019 Lecture4 MATLAB
7/59
Mathematical
Functions
ln 2
sin(2 )
cos(0)
tan( )4
Expression MATLAB Commands
Exponential exp(5)Natural Logarithm log(2)Base 10 Logarithm log10(2)Absolute Value abs(-5)Sine sin(pi)Cosine cos(2*pi)Tangent tan(pi/4)
5e
ln(2)
10log (2)
5
sin( )
cos(2 )
tan( )4
-
8/10/2019 Lecture4 MATLAB
8/59
ExamplesWrite down the mathematical expressions of
the following MATLAB commands:
5*exp(-5)+3^2/4
3+5*2-2*sqrt(9)
-
8/10/2019 Lecture4 MATLAB
9/59
ExamplesWrite down the mathematical expressions of
the following MATLAB commands:
5*exp(-5)+3^2/4
3+5*2-2*sqrt(9)
25 35
4e
3 5 2 2 9
-
8/10/2019 Lecture4 MATLAB
10/59
ExamplesWrite down the MATLAB commands of the
following mathematical expressions:
22sin 4 cos
ln(1)5 4
4
-
8/10/2019 Lecture4 MATLAB
11/59
ExamplesWrite down the MATLAB commands of the
following mathematical expressions:
2*sin(4*pi)-(cos(pi))^2
log(1)/4-5*sqrt(4)
22sin 4 cos
ln(1) 5 44
-
8/10/2019 Lecture4 MATLAB
12/59
MATLABVariables
-
8/10/2019 Lecture4 MATLAB
13/59
Variables Variables are used
as tags to which
values are assigned
Variable names
start with a letter
and are casesensitive
1
1
i
j
pi
InfNaN
Infinity
Not a number
Predefined variables:
-
8/10/2019 Lecture4 MATLAB
14/59
Define Variablesvariable name = a value (or an expression)
Examples:
a = 10A = 5*a-a^2
Variables could be defined in terms of
pre-defined variables
-
8/10/2019 Lecture4 MATLAB
15/59
Define Variablesvariable name = a value (or an expression)
Examples:
Semicolon suppresses expressions so that results will
NOT be shown in the command window.
b = 5;
c = sin(b)+log(b)-exp(b)
This_is_a_valid_name = 4^4
-
8/10/2019 Lecture4 MATLAB
16/59
ExampleCalculate the area of a circle:
What variables should be defined?
What expression(s) should be
typed in MATLAB?
-
8/10/2019 Lecture4 MATLAB
17/59
ExampleCalculate the area of a circle:
Define two variables:
Radius of the circle; Area of the circle.
Expressions in MATLAB: r=5A=pi*r^2
-
8/10/2019 Lecture4 MATLAB
18/59
Example
for and
What variable(s) should be defined?
What expression(s) should be typed in
MATLAB?
25 3 2y x x 0x2x
Calculate
-
8/10/2019 Lecture4 MATLAB
19/59
Example
for and25 3 2y x x 0x2x
Calculate
x=0
y=5*x^2-3*x+2
x=-2y=5*x^2-3*x+2
When the same
variable name is
used again, its
value is updated
-
8/10/2019 Lecture4 MATLAB
20/59
Examples in MATLAB
Vectors and Matrices
-
8/10/2019 Lecture4 MATLAB
21/59
ExampleDefine the following vector in MATLAB
2 0 2 4 6vec
-
8/10/2019 Lecture4 MATLAB
22/59
Example
vec = [-2 0 2 4 6]
vec = -2:2:6
vec = linspace(-2,6,5)
2 0 2 4 6vec
-
8/10/2019 Lecture4 MATLAB
23/59
Example
Define a vector between 0 and 20, with 50
elements
v50 = linspace(0,20,50)
-
8/10/2019 Lecture4 MATLAB
24/59
ExampleDefine the following matrix in MATLAB
40.5 sin( )
9 4
mat
mat = [pi sqrt(4); -0.5 sin(pi); 9 -4]
-
8/10/2019 Lecture4 MATLAB
25/59
MATLABImport data
-
8/10/2019 Lecture4 MATLAB
26/59
MATLAB is good for
Data Analysis
Visualization
-
8/10/2019 Lecture4 MATLAB
27/59
Import Data
Data stored in a text file could be
directly imported to MATLAB
-
8/10/2019 Lecture4 MATLAB
28/59
Example
John measured the width, depth, and length of
three objects and stored his measurements in
the file
dim.txt
-
8/10/2019 Lecture4 MATLAB
29/59
Example
dim.txt
-
8/10/2019 Lecture4 MATLAB
30/59
Example
How could John import the
measurements into MATLAB sohe could work with them?
-
8/10/2019 Lecture4 MATLAB
31/59
Import Data
.txt file that contains
only numbers
Numbers are
separated by Tab
No spaces for the file
name
Requirements of the file:
-
8/10/2019 Lecture4 MATLAB
32/59
Import Data
The .txt file could be
created using Excel Save the
excel file as type
Text (Tab Deliminated)
Requirements of the file:
-
8/10/2019 Lecture4 MATLAB
33/59
Import DataName = load('filename.txt')
Dimensions=load('dim.txt')
Dimensionsis now defined as a 3 x 3 matrix in MATLAB
-
8/10/2019 Lecture4 MATLAB
34/59
ExampleNow John wants to separate the dimensions of
the 1stobject from the rest of the data, what
would he do in MATLAB?
-
8/10/2019 Lecture4 MATLAB
35/59
Example
Object1=Dimensions(1,:)
The 1st
row of the matrix Dimensionsis nowdefined as a new vector Object1:
3.0 4.0 5.2
-
8/10/2019 Lecture4 MATLAB
36/59
MATLAB
2D Plots
-
8/10/2019 Lecture4 MATLAB
37/59
How would you plot a function y=f(x)?
What if this function is NOT well known, e.g.
2sin( ) 3 log( )
with 1 3
y x x x x
x
Question
-
8/10/2019 Lecture4 MATLAB
38/59
Create a plot of the function y=f(x)
x y=f(x)
1 3.842
1.5 7.840
2 13.125
2.5 19.330
3 26.325
2
sin( ) 3 log( ) with 1 3
y x x x x
x
y
x0 1 3
-
8/10/2019 Lecture4 MATLAB
39/59
Create 2D PlotsIn MATLAB, to create the plot of y=f(x), two
vectors, x and y, need to be defined
x y=f(x)
1 3.842
1.5 7.840
2 13.125
2.5 19.330
3 26.325
x= [1,1.5,2,2.5,3]
y= [3.841,7.840,13.125,
19.330,26.325]
x stores the xcoordinates of the points
y stores the y
coordinates of the points
-
8/10/2019 Lecture4 MATLAB
40/59
Create 2D Plots
Once the two vectors, x and y, are defined, the
command in MATLAB to create a 2D plot of
y=f(x) is
plot(x,y)
-
8/10/2019 Lecture4 MATLAB
41/59
Example
Create a plot of
sin( ),0 4y x x
-
8/10/2019 Lecture4 MATLAB
42/59
Example
Create a plot of
Use 10 points
sin( ),0 4y x x
x=linspace(0,4*pi,10)y=sin(x)
plot(x,y)
0 2 4 6 8 10 12 14-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
-
8/10/2019 Lecture4 MATLAB
43/59
Example
Create a plot of
Use 500 points
sin( ),0 4y x x
x=linspace(0,4*pi,500)y=sin(x)
plot(x,y)
0 2 4 6 8 10 12 14-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
-
8/10/2019 Lecture4 MATLAB
44/59
Title and Axes
x=linspace(0,4*pi,500)
y=sin(x)
plot(x,y)xlabel('x')
ylabel('sin(x)')
title('sine function')0 2 4 6 8 10 12 14
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
x
sin(x)
sine function
-
8/10/2019 Lecture4 MATLAB
45/59
Attributes of Plots
x=linspace(0,4*pi,500)
y=sin(x)
plot(x,y, 'm-')0 2 4 6 8 10 12 14
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
-
8/10/2019 Lecture4 MATLAB
46/59
Plot Multiple Curves
Plot
and
sin( ),0 4y x x
cos( ),0 4y x x
-
8/10/2019 Lecture4 MATLAB
47/59
Plot Multiple Curves
Step 1: Define two vectors (x and y) for
each curve:x1=linspace(0,4*pi,500)
y1=sin(x)
x2=linspace(0,4*pi,500)y2=cos(x)
-
8/10/2019 Lecture4 MATLAB
48/59
Plot Multiple Curves
Step 2: Plot both curves and
use different attributes:
Step 3: Add legend
plot(x1,y1, 'k--', x2,y2, 'r-.')
legend('sin(x)', 'cos(x)')
-
8/10/2019 Lecture4 MATLAB
49/59
ExamplePlot the two functions for
3 3
3 5
y x
y x
0 3x
Blue dashed line
Yellow asterisk
-
8/10/2019 Lecture4 MATLAB
50/59
ExamplePlot the two
functions for:3 3
3 5
y x
y x
0 3x
Blue dashed line
Yellow asterisk
x=linspace(0,3,50)y1=3*x-3
y2=-3*x+5
plot(x,y1, 'b--',x,y2,'y*')
legend('y1', 'y2')title('Plots')
xlabel('x between 0 and 3')
ylabel('y=f(x)')
-
8/10/2019 Lecture4 MATLAB
51/59
MATLAB
Well Documented m-files
ll d fil
-
8/10/2019 Lecture4 MATLAB
52/59
Well-Documented m-file
Createa file
with a list of
commands
Savethe file
Runthe file
Header
comments
run it
-
8/10/2019 Lecture4 MATLAB
53/59
To Create an m-file,Click File -> New -> Script
-
8/10/2019 Lecture4 MATLAB
54/59
This new window will pop-up. You may type in
all the commands line by line here.
Each command occupies its own line
-
8/10/2019 Lecture4 MATLAB
55/59
% symbol will make the line a comment NOT
executable. Comments appear in green.
Commands (lines without %) appear in black.
Always include three
comment lines:
-Description of this file
-Author
-Date of creation
Comments are used as explanations. Units could
also be indicated using comments.
-
8/10/2019 Lecture4 MATLAB
56/59
When you click this RUN button, MATLAB will execute all
the commands in this file line by line.
And results will be displayed in the command window.
Semicolon
suppressesexpressions so
that results will
NOT be shown
in the command
window.
-
8/10/2019 Lecture4 MATLAB
57/59
Name Requirements Name of an m-file could contain letters,
numbers and symbols
First character must be a letter
Spaces between characters are NOT allowed
-
8/10/2019 Lecture4 MATLAB
58/59
ExampleCreate a well documented m-file to:
calculate the area of a circle with a radius of 5
inches
plot the function forsin(2 )y x 0 x
E l
-
8/10/2019 Lecture4 MATLAB
59/59
Example