LAB 0 – INTRODUCTION TO MATLAB
Introduction
MATLAB is a powerful high-level programming language for scientific computations. It is very easy to
learn and use in solving numerically complex engineering problems. The exercises in this book have
been written assuming you are not proficient in MATLAB. MATLAB consists of functions that are either
built into the interpreter or available as M-files, with each containing a sequence of program statements
that execute a certain algorithm. A completely new algorithm can be written as a program containing
only a few of these functions and can be saved as another M-file.
MATLAB Environment
MATLAB works with three types of windows on your computer screen. These are the Command window,
the Figure window and the Editor window. The Command window has the heading Command, the
Figure window has the heading Figure No. 1, and the Editor window has the heading showing the name
of an opened existing M-file or Untitled if it is a new M-file under construction. The Command window
also shows the prompt >> indicating it is ready to execute MATLAB commands. Results of most printing
commands are displayed in the Command window. This window an also be used to run small programs
and saved M-files. All plots generated by the plotting commands appear in a Figure window. Either new
M-files or old M-files are run from the Command window. Existing M-files can also be run from the
Command window by typing the name of the file.
Quick Tutorial of MATLAB
This section quickly goes through matrix entry, number formats, operators, expression, useful functions
and graphics in MATLAB.
Variables
MATLAB does not require any type declarations or dimension statements. When MATLAB encounters a
new variable name, it automatically creates the variable and allocates the appropriate amount of
storage. If the variable already exists, MATLAB changes its contents and, if necessary, allocates new
storage. For example,
num_students = 25
creates a 1-by-1 matrix named num_students and stores the value 25 in its single element. Variable
names consist of a letter, followed by any number of letters, digits, or underscores. MATLAB uses only
the first 31 characters of a variable name. MATLAB is case sensitive; it distinguishes between uppercase
and lowercase letters. A and a are not the same variable. To view the matrix assigned to any variable,
simply enter the variable name.
Entering Matrices
>> x = [1 2 3 4; 5 6 7 8; 3 5 8 4]
x =
1 2 3 4
5 6 7 8
3 5 8 4
>> y = [1 2 3 4 5 6 7 8]
y =
1 2 3 4 5 6 7 8
>> z = [3 ; 6 ; 8 ; 2; 0 ; 4 ]
z =
3
6
8
2
0
4
Number Formats
>> x = [1 2 ; 3 4] % Elements of a matrix can be integers
x =
1 2
3 4
>> x = [5 0 ; -3 7] % Elements of a matrix can be negative integers
x =
5 0
-3 7
>> >> y = [1.2 4.5 9.2; -2.7 8.3 -5.8] % Elements can be real numbers
y =
1.2000 4.5000 9.2000
-2.7000 8.3000 -5.8000
>> z = [1e3 2.3e-2; 4.1e-1 6.3e2] % Numbers in Exponential format
z =
1.0e+003 *
1.0000 0.0000
0.0004 0.6300
>> x = [2+3j 4-6j; 1+5i 8-2j] % Elements can be complex numbers
x =
2.0000 + 3.0000i 4.0000 - 6.0000i
1.0000 + 5.0000i 8.0000 - 2.0000i
Operators
>> a = [1 2 ; 3 4]
a =
1 2
3 4
>> b = [4 5 ; 6 7]
b =
4 5
6 7
>> c = a+b
c =
5 7
9 11
>> d = a-b
d =
-3 -3
-3 -3
>> e = a*b
e =
16 19
36 43
>> f = a/b
f =
2.5000 -1.5000
1.5000 -0.5000
>> g = a.*b
g =
4 10
18 28
>> h = a./b
h =
0.2500 0.4000
0.5000 0.5714
>> x = a\b
x =
-2.0000 -3.0000
3.0000 4.0000
>> y = b\a
y =
4.0000 3.0000
-3.0000 -2.0000
>> z = a/b
z =
2.5000 -1.5000
1.5000 -0.5000
>> w = b/a
w =
-0.5000 1.5000
-1.5000 2.5000
>> a = [1+3j 4-5j 7+2j; 6-2j 5+7j 9+2j]
a =
1.0000 + 3.0000i 4.0000 - 5.0000i 7.0000 + 2.0000i
6.0000 - 2.0000i 5.0000 + 7.0000i 9.0000 + 2.0000i
>> b = a'
b =
1.0000 - 3.0000i 6.0000 + 2.0000i
4.0000 + 5.0000i 5.0000 - 7.0000i
7.0000 - 2.0000i 9.0000 - 2.0000i
>> c = conj(a)
c =
1.0000 - 3.0000i 4.0000 + 5.0000i 7.0000 - 2.0000i
6.0000 + 2.0000i 5.0000 - 7.0000i 9.0000 - 2.0000i
Relational Operators
>> X = [1 2 3 ; 4 5 6 ]
X =
1 2 3
4 5 6
>> Y = [1 7 2 ; 6 5 1]
Y =
1 7 2
6 5 1
>> X < Y
ans =
0 1 0
1 0 0
>> X > Y
ans =
0 0 1
0 0 1
>> X <= Y
ans =
1 1 0
1 1 0
>> X >= Y
ans =
1 0 1
0 1 1
>> X == Y
ans =
1 0 0
0 1 0
>> X ~= Y
ans =
0 1 1
1 0 1
Colon Operator
>> a = 1:8
a =
1 2 3 4 5 6 7 8
>> b = 1:3:11
b =
1 4 7 10
>> c = 10: -2: -3
c =
10 8 6 4 2 0 -2
>> d = 0: pi/5: pi
d =
0 0.6283 1.2566 1.8850 2.5133 3.1416
Advanced Matrix Operations
>> x = [1 2 ; 3 4]
x =
1 2
3 4
>> y = [x x ; x -x]
y =
1 2 1 2
3 4 3 4
1 2 -1 -2
3 4 -3 -4
>> z = y(2:4, [2 4])
z =
4 4
2 -2
4 -4
>> y (:, 1) = []
y =
2 1 2
4 3 4
2 -1 -2
4 -3 -4
>> y([2 4], :) =[]
y =
2 1 2
2 -1 -2
Examples of Expressions
rho = (1+sqrt(6))/7
rho =
0.4928
a = abs(6+8i)
a =
10
>> z = sqrt(besselk(5/3,rho-i))
z =
0.5195 + 1.0303i
>> x = -5: 0.001: 10; y = x.^2 - 4*x + 3; plot(x, y)
Simple Example
The section illustrates solving the two linear equations
x + y = 3
2x + 3y = 8
Method-1
>> a = [1 1 ; 2 3]
a =
1 1
2 3
>> b = [3;8]
b =
>> x = a\b
x =
1
2
Method-2
>> x = -4: 1e-3 : 4;
>> y1 = 3 - x;
>> y2 = (8-2*x)/3;
>> plot(x,y1,x,y2); grid;
-1
-4 -3 -2 -1 0 1 2 3 4
Commonly used Matrices
MATLAB provides four functions that generate basic matrices.
>> c = zeros(3,4)
c =
0 0 0 0
0 0 0 0
0 0 0 0
>> d = ones(4,2)
d =
1 1
1 1
1 1
1 1
>> e = eye(5)
e =
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
>> f = rand(3,5)
f =
0.9501 0.4860 0.4565 0.4447 0.9218
0.2311 0.8913 0.0185 0.6154 0.7382
0.6068 0.7621 0.8214 0.7919 0.1763
>> g = randn(4,5)
g =
-0.4326 -1.1465 0.3273 -0.5883 1.0668
-1.6656 1.1909 0.1746 2.1832 0.0593
0.1253 1.1892 -0.1867 -0.1364 -0.0956
0.2877 -0.0376 0.7258 0.1139 -0.8323
General Purpose Commands
• help fft
• helpwin fft2
• doc freqz
• lookfor fourier
• clc
• clear
• close all
• dir
• pwd
• ver
• date
• save abcd a b c
• load abcd
• who
• whos
• cd
• quit
• exit
Graphics
Simple Plots
>> x = 0: pi/100: 6*pi;
>> y = sin(x);
>> plot(x, y, 'r')
Multiple Plots
>> x = 0: pi/100: 6*pi;
>> y = sin(x); z = cos(x);
>> plot(x, y, 'r:', x, z, 'k--'); grid;
Subplots
>> x = 0: pi/100: 6*pi;
>> y = sin(x); z = cos(x);
>> figure;
>> subplot(2, 2, 1); plot(x, y);
>> subplot(2, 2, 2); plot(x, z);
>> subplot(2, 2, 3); plot(x, y+z);
>> subplot(2, 2, 4); plot(x, y-z);
1 1
0.5 0.5
0 0
-0.5 -0.5
-1 -1
0 5 10 15 20 0 5 10 15 20
2 2
1 1
0 0
-1 -1
-2 -2
0 5 10 15 20 0 5 10 15 20
Labeling Axes
>> x = linspace(-5, 5, 200);
>> y = x.*x -4*x + 3;
>> plot(x, y);
>> grid
>> xlabel('x axis');
>> ylabel('y axis');
>> title('Quadratic Equation');
>> legend('x^{2}-4x+3');
Quadratic Equation
50
x 2-4x+3
40
30
y axis
20
10
-10
-5 -4 -3 -2 -1 0 1 2 3 4 5
x axis
3-D Plots
[X,Y] = meshgrid(-3:.125:3);
Z = peaks(X,Y);
meshc(X,Y,Z);
axis([-3 3 -3 3 -10 5])