Beruflich Dokumente
Kultur Dokumente
Usha B S
bsusha@gmail.com
What is MATLAB?
2
Numerical
Computation
Usha B S, ECE Dept, RNSIT
Data
Analysis
Data
Visualization
MATLAB
MATLAB is a high-level language and interactive
environment for
Numerical computation,
Data Analysis and Visualization
Acquire, Analyze, Visualize, Document
Applications
Application Development and Deployment
3
MATLAB Toolboxes
MATLAB has a number of add-on software modules, called
Applications of MATLAB
5
Applications of MATLAB
6
Applications of MATLAB
7
Current
Folder
View folders
and m-files
Command
Window
Work Space
type
commands
View
variables
view past
commands
Command
History
tools
Mathematical Function Library
10
Matlab Help
Different ways to find information
help
help general, help mean, sqrt...
helpdesk - an html document with
links to further information
11
12
Setting Fonts
14
Variables
No need to declare data type of variables
int a;
double b;
float c;
All variables are created with double precision unless specified and
After these statements, the variables are 1x1 matrices with double
precision
15
Semicolon suppresses
screen output
b=20;
Avg=(a+b)/2;
whos
Name
Size
a
1x1
avg
1x1
b
1x1
16
Bytes
8
8
8
Class
double
double
double
-5/(4.8+5.32)^2
ans =
-0.0488
a = 2;
b = 5;
a^b
ans =
(3+4i)*(3-4i)
ans =
Semicolon suppresses
screen output
32
x = 5/2*pi;
Results assigned to
ans if name not given
y = sin(x)
25
y =
1
z = asin(y)
z =
Use parentheses ( )
for function inputs
1.5708
18
^ or .^ a^b or
a.^b
* (matrix multiply) or
.*(array
a*b
or
a.*b
/ or ./ a/b or
a./b
\ or .\ b\a or
b.\a
56/8 = 8\56
+
a + b
a - b
=
a = b (assign b to a)
multiply)
prompt
...
,
%
;
19
Less Than
Less Than or Equal
Greater Than
Greater Than or Equal
Equal To
Not Equal To
20
<
<=
>
>=
==
~=
not
and
or
exor
21
~
&
|
^
% highest precedence
% equal precedence with or
% equal precedence with and
% equal precedence with and
Worksheet I
22
Solution-WS1
clc;
clear all;
close all;
A = input('Enter the first point coordinates in square brackets ');
B= input('Enter the second point coordinates in square brackets ');
d = sqrt((A(1)-B(1))^2+(A(2)-B(2))^2);
disp('The distance between given points is');
disp(d);
23
24
Creating Vectors
Order of Matrix M X N
25
row vector
column vector
Creating Vectors
A vector is 1-by-n or n-by-1 matrix and appears in
MATLAB as a row or column of real or complex numbers.
We use square brackets to construct vector
26
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
Create vector with N equally spaced intervals
x =
>> x=logspace(1,2,7)
10.0000 14.6780 21.5443 68.1292
100.0000
Transpose
a=[1 2 3];
>> a'
1
2
3
Work Problems of worksheet-II
28
A=
29
16
5
9
4
3
10
6
15
2
11
7
14
13
8
12
1
Subscripts
The element in row i and column j of A is given by A(i,j).
So to compute the sum of the elements in the fourth
column of A, we have: A(1,4) + A(2,4) + A(3,4) + A(4,4)
which is same as sum(A(:,4))
30
Matrix Operations
Enter the Matrix
a(3,2)
ans =
8
colA = matName(:,colNumber)
To read an entire row
varA = matName(rowNumber,:)
Worksheet III
32
Linear Indexing
2D array can be treated as 1D array, as all the
34
Concatenation of Matrices
35
ones(M,N)
rand(M,N)
>> x = zeros(1,3)
x=
0 0 0
>> x = ones(1,3)
x=
1 1 1
>> x = rand(1,3)
x=
0.9501 0.2311 0.6068
Matrices Operations
Given A and B:
Addition
Subtraction
Product
Transpose
Given A:
38
Multiply each
element of A by 3
Square each
element of A
command.
Example: to get the size of matrix a.
[numRow, numCol] = size(a);
39
Sorting Matrices
To sort a matrix, use the sort command.
40
41
42
Graphics
MATLAB provides a variety of techniques to display data
graphically.
Interactive tools enable you to manipulate graphs to achieve
results that reveal the most information about your data.
You can also edit and print graphs for presentations, or
export graphs to standard graphics formats for presentation
in Web browsers or other media.
43
Plot
Example
Y versus vector X
columns of Y versus
their index
44
x = [-3 -2 -1 0 1 2 3];
y1 = (x.^2) -1;
plot(x, y1,'bo-.');
Plot Properties
XLABEL X-axis label.
Example
...
xlabel('x values');
ylabel('y values');
Hold
HOLD Hold current graph.
HOLD ON holds the current
Example
hold on;
y2 = x + 2;
plot(x, y2, 'g+:');
default mode
46
Subplot
SUBPLOT Create axes in tiled positions.
SUBPLOT(m,n,p), or
x = [-3 -2 -1 0 1 2 3];
y1 = (x.^2) -1;
% Plot y1 on the top
subplot(2,1,1);
plot(x, y1,'bo-.');
xlabel('x values');ylabel('y values');
% Plot y2 on the bottom
subplot(2,1,2);
y2 = x + 2;
Usha B S, ECE Dept, RNSIT
plot(x, y2, 'g+:');
47
Figure
FIGURE Create figure window.
FIGURE, by itself, creates a new
Example
x = [-3 -2 -1 0 1 2 3];
y1 = (x.^2) -1;
% Plot y1 in the 1st Figure
plot(x, y1,'bo-.');
xlabel('x values');
ylabel('y values');
% Plot y2 in the 2nd Figure
figure
y2 = x + 2;
plot(x, y2, 'g+:');
48
49
50
51
52
53
Programming in Matlab
Conditional Control
54
A simple example:
a=1
while length(a) < 10
a = [0 a] + [a 0]
end
1
11
121
1331
14641
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
(with a= before each line).
55
57
t1 = -9:0.5:9;
i = 1;
for t = -9:0.5:9;
if t>=0
y(1, i) = -3*(t^2)+5;
else
y(1, i) = 3*(t^2)+5;
end
i = i+1;
end
plot(t1,y);
tic;
for i=1:9000
j=1;
x(i)=j+i;
end
toc
58
Features-Preallocation
Increases Performance
tic;
tic;
for I = 1:10000
pre_allo =
not_pre_allo(I)=rand(1);
zeros(10000,1);
End
for I = 1:10000
toc
pre_allo(I) =
rand(1);
End
toc
Same resulting vector in both cases:
hold on;
cub=x.^3;
pl2=plot(x,cub,b-o');
60
Graphics - Annotation
Use title, xlabel, ylabel and legend for
annotation
61
Graphics - Annotation
62
Graphics-Stem()
stem()is to plot discrete sequence data
plot()
cos(n/4)
1
n=-10:10;
f=stem(n,cos(n*pi/4))
title('cos(n\pi/4)')
xlabel('n')
0.5
-0.5
-1
-10
63
-5
0
n
10
Save plots
Use saveas(h,'filename.ext')
to save a figure to a file.
f=figure;
x=-5:0.1:5;
h=plot(x,cos(2*x+pi/3));
title('Figure 1');
xlabel('x');
saveas(h,'figure1.fig')
64
Plot Tools
65
Figure Toolbox
You can open and configure plotting tools in many ways.
66
% First subplot
x = 0:pi/100:2*pi;
y1 = sin(x);
y2 = sin(x+.25);
y3 = sin(x+.5);
subplot(2,1,1);
plot(x,y1,x,y2,x,y3);
axis tight;
67
% Second sublot
w1 = cos(x);
w2 = cos(x+.25);
w3 = cos(x+.5);
subplot(2,1,2);
plot(x,w1,x,w2,x,w3);
axis tight;
Plot
1
0.5
0
-0.5
-1
1
0.5
0
-0.5
-1
68
69
70
Sound
71
Example
lambda=1000; %change to 2000 and see the effect
fs=8000;
f0=1000;
t1=2;
it=(0:fs*t1-1)/fs;
theta=2*pi*it*f0+pi*lambda*(it.^2);
X=cos(theta);
soundsc(X,fs);
74
Functions
Function with One Output
Define a function in a file named average.m that accepts
an input vector, calculates the average of the values, and
returns a single result.
function y = average(x)
if ~isvector(x)
error('Input must be a vector')
end
y = sum(x)/length(x);
function y = mymean(x)
%MYMEAN Average or mean value.
Online Help
MATLAB
Code
if m == 1
m = n;
end
y = sum(x)/m;
76
ave =
47.3400
stdev =
29.4124
77
MATLAB Help
MATLAB Help is an
extremely powerful
assistance to learning
MATLAB
Help not only contains the
theoretical background,
but also shows demos for
implementation
MATLAB Help can be
opened by using the
HELP pull-down menu
78
79
Thank You!!!
Questions??