You are on page 1of 59

Introduction to MATLAB

Dr. Ben Mertz


FSE 100 Lecture 4
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
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
MATLAB also does
http://jennifertsau.com/uncategorized/matlab-fun/
http://www.mathworks.com
Command window
with command lines
and results
Command histories
Workspace: variables
Current
Directory
Mathematical
Computations
Expression MATLAB Commands
Addition
3+5
Subtraction
4-9
Multiplication
3*2
Division
9/3
Square Root
sqrt(4)
Power
2^4
3 2
9 3
4
4
2
3 5 +
4 9
Mathematical
Functions
ln2
sin(2 ) t
cos(0)
tan( )
4
t
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)
5
e
ln(2)
10
log (2)
5
sin( ) t
cos(2 ) t
tan( )
4
t
Examples
Write down the mathematical expressions of
the following MATLAB commands:
5*exp(-5)+3^2/4

3+5*2-2*sqrt(9)
Examples
Write down the mathematical expressions of
the following MATLAB commands:
5*exp(-5)+3^2/4

3+5*2-2*sqrt(9)
2
5
3
5
4
e

+
3 5 2 2 9 +
Examples
Write down the MATLAB commands of the
following mathematical expressions:
( ) ( )
2
2sin 4 cos t t
ln(1)
5 4
4

Examples
Write down the MATLAB commands of the
following mathematical expressions:
2*sin(4*pi)-(cos(pi))^2

log(1)/4-5*sqrt(4)
( ) ( )
2
2sin 4 cos t t
ln(1)
5 4
4

MATLAB Variables
Variables
Variables are used
as tags to which
values are assigned
Variable names
start with a letter
and are case
sensitive
1
1
t
i
j
pi
Inf
NaN
Infinity
Not a number
Predefined variables:
Define Variables
variable name = a value (or an expression)
Examples:
a = 10
A = 5*a-a^2
Variables could be defined in terms of
pre-defined variables
Define Variables
variable 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
Example
Calculate the area of a circle:


What variables should be defined?

What expression(s) should be
typed in MATLAB?
Example
Calculate the area of a circle:

Define two variables:
Radius of the circle;
Area of the circle.

Expressions in MATLAB:
r=5
A=pi*r^2
Example

for and

What variable(s) should be defined?
What expression(s) should be typed in
MATLAB?
2
5 3 2 y x x = + 0 x =
2 x =
Calculate
Example
for and
2
5 3 2 y x x = + 0 x = 2 x =
Calculate
x=0
y=5*x^2-3*x+2
x=-2
y=5*x^2-3*x+2
When the same
variable name is
used again, its
value is updated
Examples in MATLAB
Vectors and Matrices
Example
Define the following vector in MATLAB
| |
2 0 2 4 6 vec =
Example
vec = [-2 0 2 4 6]
vec = -2:2:6
vec = linspace(-2,6,5)

| |
2 0 2 4 6 vec =
Example
Define a vector between 0 and 20, with 50
elements
v50 = linspace(0,20,50)

Example
Define the following matrix in MATLAB
4
0.5 sin( )
9 4
mat
t
t
(
(
=
(
(


mat = [pi sqrt(4); -0.5 sin(pi); 9 -4]
MATLAB Import data
MATLAB is good for
Data Analysis
Visualization
Import Data
Data stored in a text file could be
directly imported to MATLAB
Example
John measured the width, depth, and length of
three objects and stored his measurements in
the file

dim.txt

Example



dim.txt
Example
How could John import the
measurements into MATLAB so
he could work with them?



Import Data
.txt file that contains
only numbers
Numbers are
separated by Tab
No spaces for the file
name
Requirements of the file:
Import Data
The .txt file could be
created using Excel
Save the
excel file as type
Text (Tab Deliminated)
Requirements of the file:
Import Data
Name = load('filename.txt')
Dimensions=load('dim.txt')
Dimensions is now defined as a 3 x 3 matrix in MATLAB
Example
Now John wants to separate the dimensions of
the 1
st
object from the rest of the data, what
would he do in MATLAB?
Example
Object1=Dimensions(1,:)
The 1
st
row of the matrix Dimensions is now
defined as a new vector Object1:
3.0 4.0 5.2
MATLAB
2D Plots
How would you plot a function y=f(x)?

What if this function is NOT well known, e.g.

2
sin( ) 3 log( )
with 1 3
y x x x x
x
= +
s s
Question
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
= +
s s
y
x
0
1 3
Create 2D Plots
In 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 x
coordinates of the points
y stores the y
coordinates of the points
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)
Example
Create a plot of

sin( ), 0 4 y x x t = s s
Example
Create a plot of


Use 10 points

sin( ), 0 4 y x x t = s s
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
Example
Create a plot of


Use 500 points

sin( ), 0 4 y x x t = s s
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
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
s
i
n
(
x
)
sine function
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
Plot Multiple Curves
Plot



and



sin( ), 0 4 y x x t = s s
cos( ), 0 4 y x x t = s s
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)

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)')
Example
Plot the two functions for
3 3
3 5
y x
y x
=
= +
0 3 x s s
Blue dashed line



Yellow asterisk



Example
Plot the two
functions for:

3 3
3 5
y x
y x
=
= +
0 3 x s s
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)')
MATLAB
Well Documented m-
files
Well-Documented m-file
Create a file
with a list of
commands
Save the file
Run the file
Header
comments
run it
To Create an m-file,
Click File -> New -> Script
This new window will pop-up. You may type in
all the commands line by line here.

Each command occupies its own line

% 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.
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
suppresses
expressions so
that results will
NOT be shown
in the command
window.
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
Example
Create a well documented m-file to:
calculate the area of a circle with a radius of 5
inches
plot the function for sin(2 ) y x =
0 x t s s
Example

You might also like