MATLAB: How Do You Work With Data To Solve Problems?
MATLAB: How Do You Work With Data To Solve Problems?
High Level
Languages such as
C, Pascal etc.
Assembly
Why Use MATLAB?
• Used mainly for algorithm development and data
visualization
– Algorithms can be implemented and tested more
quickly and easily than with traditional programming
languages
• Quickly get numerical and graphic answers to matrix and
vector related math problems
• A way to solve complex numerical problems without
actually writing a program
– Built-in tools
– No complex programming knowledge needed
• MATLAB focuses on ease of use and quick development
6
Some of MATLAB’s Toolboxes
Signal & Image Processing
Signal Processing
Math and Analysis
Image Processing
Optimization
Communications
Requirements Management Interface
Frequency Domain System Identification
Statistics
Higher-Order Spectral Analysis
Neural Network
System Identification
Symbolic/Extended Math
Wavelet
Partial Differential Equations
Filter Design
PLS Toolbox
Mapping
Control Design
Spline
Control System
Fuzzy Logic
Data Acquisition and Import
Robust Control
Data Acquisition
μ-Analysis and Synthesis
Instrument Control
Model Predictive Control
Excel Link
Portable Graph Object
MATLAB’s Appeal
Current Directory
View folders and m-files
Workspace
View program variables
Double click on a variable
to see it in the Array Editor
Command History
view past commands
save a whole session
using diary
Some MATLAB Development Windows
• Command Window: where you enter commands
• Command History: running history of commands which
is preserved across MATLAB sessions
• Current directory: current location for session
• Workspace: GUI for viewing, loading and saving
MATLAB variables
• Array Editor: GUI for viewing and/or modifying contents
of MATLAB variables (openvar varname or double-click
the array’s name in the Workspace)
• Editor/Debugger: text editor, debugger; editor works
with file types in addition to .m (MATLAB “m-files”)
Entering Commands and Expressions
>> 3*4^2 + 5
ans =
53
>>(3*4)^2 + 5
ans =
149
>>27^(1/3) + 32^(0.2)
ans =
5
>>27^(1/3) + 32^0.2
ans =
5
>>27^1/3 + 32^0.2
ans =
11
Variables
• No need for types. i.e.,
int a;
double b;
float c;
24
Matrices & Vectors
• All (almost) entities in MATLAB are matrices
• Easy to define:
• Transpose
Vector : Matrix:
>> a=[1 2 3]; >> A=[1 2; 3 4];
>> a' >> A'
1 ans =
2 1 3
3 2 4
Creating Vectors
Create vector with equally spaced intervals
>> x=0:0.5:pi
x =
0 0.5000 1.0000 1.5000 2.0000 2.5000 3.0000
Given A and B:
b = x .* y c=x./y d = x .^2
x = A(1,:) y = A(3 ,:)
b= c= d=
x= y= 3 8 -3 0.33 0.5 -3 1 4 9
1 2 3 3 4 -1
Some Built-in functions
• mean(A):mean value of a vector
• max(A), min (A): maximum and minimum.
• sum(A): summation.
• sort(A): sorted vector
• median(A): median value
• std(A): standard deviation.
• det(A) : determinant of a square matrix
• dot(a,b): dot product of two vectors
• Cross(a,b): cross product of two vectors
• Inv(A): Inverse of a matrix A
• length(A): number of values in an array
Operators (relational, logical)
• == Equal to
• ~= Not equal to
• < Strictly smaller
• > Strictly greater
• <= Smaller than or equal to
• >= Greater than equal to
• & And operator
• | Or operator
Indexing Matrices
Given the matrix: A = n
0.9501 0.6068 0.4231
m
0.2311 0.4860 0.2774
Then:
A(1,2) = 0.6068
A(:,1) = [0.9501
0.2311 ]
1:m
A(1,2:3)=[0.6068 0.4231]
Adding Elements to a Vector or a Matrix
>> A=1:3 >> C=[1 2; 3 4]
A= C=
1 2 3 1 2
>> A(4:6)=5:2:9 3 4
A= >> C(3,:)=[5 6];
1 2 3 5 7 9 C=
1 2
>> B=1:2 3 4
B= 5 6
1 2
>> B(5)=7; >> D=linspace(4,12,3);
B= >> E=[C D’]
1 2 0 0 7 E=
1 2 4
3 4 8
5 6 12
Flow Control
• if
• for
• while
• break
• ….
Control Structures
Some Dummy Examples
• If Statement Syntax if ((a>3) & (b==5))
Some MATLAB Commands;
if (Condition_1) end
38
Control Structures
Some Dummy Examples
• For loop syntax for i=1:100
Some MATLAB Commands;
end
Dummy Example
while (condition)
while ((a>3) & (b==5))
MATLAB Commands Some MATLAB Commands;
end
end
You can perform operations in MATLAB in two
ways:
• Extension “.m”
• A text file containing script or function or program to run
43
Save file as Denem430.m
Use of M-File
44
Writing User Defined Functions
• Functions are m-files which can be executed by
specifying some inputs and supply some desired
outputs.
• The code telling the MATLAB that an m-file is actually
a function is
function out1=functionname(in1)
function out1=functionname(in1,in2,in3)
function [out1,out2]=functionname(in1,in2)
Same Name
Writing User Defined Functions
• Another function which takes an input array and returns the sum and product of its
elements as outputs
47
Keep in mind when using script files:
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
xlabel('x = 0:2\pi')
ylabel('Sine of x')
title('Plot of the
Sine Function')
A graphics window showing a plot.
Some MATLAB plotting commands
Multiple Graphs
t = 0:pi/100:2*pi;
y1=sin(t);
y2=sin(t+pi/2);
plot(t,y1,t,y2)
grid on
Multiple Plots
t = 0:pi/100:2*pi;
y1=sin(t);
y2=sin(t+pi/2);
subplot(2,2,1)
plot(t,y1)
subplot(2,2,2)
plot(t,y2)
Graph Functions (summary)
• plot linear plot
• stem discrete plot
• grid add grid lines
• xlabel add X-axis label
• ylabel add Y-axis label
• title add graph title
• subplot divide figure window
• figure create new figure window
• pause wait for user response
Notes:
• “%” is the neglect sign for MATLAB
(equaivalent of “//” in C). Anything after it on
the same line is neglected by MATLAB
compiler.
• Sometimes slowing down the execution is
done deliberately for observation purposes.
You can use the command “pause” for this
purpose
pause %wait until any key
pause(3) %wait 3 seconds
Getting Help From MATLAB:
The Function Browser after plot has been selected
1-35
The MATLAB Help Browser
1-36
The Help Navigator
1-37
Useful Commands
>>lookfor keyword
>>demos
Random Numbers
x=rand(100,1);
stem(x);
hist(x,100)
Coin Tosses
• Simulate the outcomes of 100 fair coin tosses
x=rand(100,1);
p=sum(x<0.5)/100
p =
0.5400
p =
0.5110
Coin Tosses
• Simulate the outcomes of 1000 biased coin
tosses with p[Head]=0.4
x=rand(1000,1);
p=sum(x<0.4)/1000
p =
0.4160
Sum of Two Dies
• Simulate 10000 observations of the sum of
two fair dies
6 . . . . . .
5 . . . . . .
(1,6) (2,6) (3,6) (4,6) (5,6) (6,6)
4 . . . . . .
(1,5) (2,5) (3,5) (4,5) (5,5) (6,5)
3 . . . . . .
(1,4) (2,4) (3,4) (4,4) (5,4) (6,4)
2 . . . . . .
(1,3) (2,3) (3,3) (4,3) (5,3) (6,3)
1 . . . . . .
(1,2) (2,2) (3,2) (4,2) (5,2) (6,2)
1 2 3 4 5 6
Sum of Two Dies
• Simulate 10000 observations of the sum of two
fair dies
x1=floor(6*rand(10000,1)+1);
x2=floor(6*rand(10000,1)+1);
y=x1+x2;
sum(y==2)/10000 ans = 0.0275 p[2]=0.0278
sum(y==3)/10000 ans = 0.0554 p[3]=0.0556
sum(y==4)/10000 ans = 0.0841 p[4]=0.0833
sum(y==5)/10000 ans = 0.1082 p[5]=0.1111
sum(y==6)/10000 ans = 0.1397 p[6]=0.1389
sum(y==7)/10000 ans = 0.1705 p[7]=0.1667
sum(y==8)/10000 ans = 0.1407 p[8]=0.1389
sum(y==9)/10000 ans = 0.1095 p[9]=0.1111
sum(y==10)/10000 ans = 0.0794 p[10]=0.0833
sum(y==11)/10000 ans = 0.0585 p[11]=0.0556
sum(y==12)/10000 ans = 0.0265 p[12]=0.0278
Sum of Two Dies
for i=2:12
z(i)=sum(y==i)/10000
end
bar(z)
Basic Data Analysis
Each column is
the raw rpm
sensor data from
a different sensor
used in an
instrumented
engine test. The
rows represent
the times
readings were
made.
Plotting the Data
>> plot(rpm_raw)
>> xlabel('sample number - during time slice');
>> ylabel('Unfiltered RPM Data');
>> title(‘3 sequences of samples from RPM sensor’)