Introduction To Matlab
Introduction To Matlab
Gowtham Bellala
MATLAB
pi
eps
inf
NaN
i and j
realmin
realmax
Value of
Smallest incremental number
Infinity
Not a number e.g. 0/0
i = j = square root of -1
The smallest usable positive real number
The largest usable positive real number
<
<=
>
>=
==
~=
(NOT != like in C)
~
&
|
% highest precedence
% equal precedence with or
% equal precedence with and
MATLAB Matrices
Scalars are matrices with only one row AND one column
Generating Matrices
Generating Matrices
A(2,4)
A(17)
Extracting a Sub-matrix
Extracting a Sub-matrix
Example :
>> X = [1,2,3;4,5,6;7,8,9]
X =
1
2
3
4
5
6
7
8
9
>> X22 = X(1:2 , 2:3)
X22 =
2
3
5
6
Matrix Extension
>> a = [1,2i,0.56]
a =
1
0+2i
0.56
>> a(2,4) = 0.1
a =
1
0+2i
0.56
0
0
0
0
0.1
Concatenation
>> a = [1,2;3,4]
a =
1
2
3
4
>> a_cat =[a,2*a;3*a,2*a]
a_cat =
1
2
2
4
3
4
6
8
3
6
2
4
9
12
6
8
NOTE: The resulting matrix must
be rectangular
Matrix Addition
Matrix Multiplication
Matrix multiplication
>> a = [1,2;3,4];
>> b = [1,1];
>> c = b*a
c =
4
6
(2x2)
(1x2)
>> c = a*b
??? Error using ==> mtimes
Inner matrix dimensions
must agree.
>> a = [1,2;1,3];
>> b = [2,2;2,1];
abs
- finds absolute value of all elements in the matrix
sign
- signum function
sin,cos, - Trignometric functions
asin,acos - Inverse trignometric functions
exp
- Exponential
log,log10 - natural logarithm, logarithm (base 10)
ceil,floor
- round towards +infinity, -infinity respectively
round
- round towards nearest integer
real,imag - real and imaginary part of a complex matrix
sort
- sort elements in ascending order
Graphics Fundamentals
2D Plotting
Example 1: Plot sin(x) and cos(x) over [0,2], on the same plot with
different colours
Method 1:
>> x = linspace(0,2*pi,1000);
>> y = sin(x);
>> z = cos(x);
>> hold on;
>> plot(x,y,b);
>> plot(x,z,g);
>> xlabel X values;
>> ylabel Y values;
>> title Sample Plot;
>> legend (Y data,Z data);
>> hold off;
2D Plotting
Method 2:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
x = 0:0.01:2*pi;
y = sin(x);
z = cos(x);
figure
plot (x,y,x,z);
xlabel X values;
ylabel Y values;
title Sample Plot;
legend (Y data,Z data);
grid on;
2D Plotting
t
Example 2: Plot the following function y
1 / t
Method 1:
>> t1 = linspace(0,1,1000);
>> t2 = linspace(1,6,1000);
>> y1 = t1;
>> y2 = 1./ t2;
>> t = [t1,t2];
>> y = [y1,y2];
>> figure
>> plot(t,y);
>> xlabel t values, ylabel y values;
0 t 1
1 t 6
2D Plotting
Method 2:
>>
>>
>>
>>
>>
>>
>>
>>
t = linspace(0,6,1000);
y = zeros(1,1000);
y(t()<=1) = t(t()<=1);
y(t()>1) = 1./ t(t()>1);
figure
plot(t,y);
xlabelt values;
ylabely values;
Subplots
Importing/Exporting Data
Flow control
if statement
Example 1:
>> if i == j
>>
a(i,j) =
>> elseif i >=
>>
a(i,j) =
>> else
>>
a(i,j) =
>> end
2;
j
1;
0;
Example 2:
>> if (attn>0.9)&(grade>60)
>>
pass = 1;
>> end
switch statement
>>
case case_expr2
>>
>>
otherwise
>>
>> end
Example :
>> x = 2, y = 3;
>> switch x
>> case x==y
>>
disp('x and y are equal');
>> case x>y
>>
disp('x is greater than y');
>> otherwise
>>
disp('x is less than y');
>> end
x is less than y
Note: Unlike C, MATLAB doesnt need
BREAKs in each case
for loop
>>
>> end
Example 1:
Example 2:
>> a = zeros(n,m);
>> for i = 1:n
>>
for j = 1:m
>>
a(i,j) = 1/(i+j);
>>
end
>> end
while loop
>>
>> end
Example 1:
>> n = 1;
>> y = zeros(1,10);
>> while n <= 10
>>
y(n) = 2*n/(n+1);
>>
n = n+1;
>> end
Example 2:
>> x = 1;
>> while x
>>
%execute statements
>> end
Note: In MATLAB 1 is
synonymous to TRUE and 0 is
synonymous to FALSE
break statement
Example:
>> y = 3;
>> for x = 1:10
>>
printf(%5d,x);
>>
if (x>y)
>>
break;
>>
end
>> end
1
2
3
4
Efficient Programming
In most cases, one can replace nested loops with efficient matrix
manipulation.
Avoid using your own functions, MATLABs functions are more likely
to be efficient than yours.
Example 1
Let x[n] be the input to a non causal FIR filter, with filter
coefficients h[n]. Assume both the input values and the filter
coefficients are stored in column vectors x,h and are given to
you. Compute the output values y[n] for n = 1,2,3 where
19
Solution
>>
>>
>>
>>
>>
Method 1:
y = zeros(1,3);
for n = 1:3
for k = 0:19
y(n)= y(n)+h(k)*x(n+k);
end
>>
>>
>>
>>
>> end
Method 3 (avoids both the loops):
>> X= [x(1:20),x(2:21),x(3:22)];
>> y = h*X;
Example 2
Solution
>>
>>
>>
>>
>>
>>
>>
>>
>>
Method 1:
y = zeros(20,1);
y(1) = 1;
for n = 2:20
for m = 1:n
temp = temp + m^3;
end
y(n) = y(n-1)*temp;
temp = 0
end
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
On the Web :
http://www.mathworks.com/support
http://www.mathworks.com/products/demos/#
http://www.math.siu.edu/MATLAB/tutorials.html
http://math.ucsd.edu/~driver/21d -s99/MATLAB-primer.html
http://www.mit.edu/~pwb/cssm/
http://www.eecs.umich.edu/~aey/eecs216/.html