SAS EXP-1
SAS EXP-1
SAS EXP-1
Remarks:
Objectives:
1. To familiar with the Matlab environment.
2. Write the simple Matlab code to perform certain operations.
1
DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS
2
DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS
Concise Code: High-level languages like MATLAB require fewer lines of code to
perform complex tasks compared to low-level languages. For example, solving a
system of linear equations can be done in a single line in MATLAB using the \
operator.
5. Ease of Learning and Use
Intuitive Syntax: MATLAB’s syntax is designed to be intuitive and closely
resembles mathematical notation, which makes it accessible to non-
programmers like scientists and engineers.
Rich Documentation: MATLAB provides extensive documentation, examples,
and community support, making it easier to learn and use.
Example: Matrix Multiplication in MATLAB vs. C
MATLAB:
A = [1, 2; 3, 4];
B = [5, 6; 7, 8];
C = A * B;
C Language:
3
DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS
In this example, the MATLAB code is much more concise and easier to
understand, demonstrating the high-level nature of MATLAB compared to a low-
level language like C.
4
DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS
5
DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS
xlabel('x');
ylabel('sin(x)');
Run Script:Save your script as sine_wave.m and run it. MATLAB will display the
sine wave plot in the Figure Window.
Inspect Results: Use the Workspace to check your variables and the Command
History to review past commands.
Computations can be carried out in one of three ways:
1. Directly from the command line,
2. by writing a script that carries out predefined instructions, or
3. by writing your own functions.
Writing your own functions is much like programming in other languages,
except that you have the full resources of MATLAB's functions at your disposal, making for
very compact code. The MATLAB-6 and above environment has several windows at your
disposal.
Command Window: The main window is the command window, where you will type
all your commands, for small programs.
* Note that commas could have been used in place of spaces to separate the
elements. that is
>> v = [1,3,5,7];
6
DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS
Previously defined vectors can be used to define a new vector. For example, with ' v '
defined above
>> a = [9 10];
>> b = [v a] % creates the vector b = [1 3 5 7 8 9 10].
Method2: The second method is used for creating vectors with equally spaced
elements:
t = 0: 0.1:10 % creates a 1x101 vector with the elements 0, .1, .2, .3,...,10.
Note that the middle number defines the increment.
If only two numbers are given, then the increment is set to a default of 1: For
example.
k = 0:10 % creates a 1x11 vector with the elements 0, 1, 2, ..., 10.
2. Matrices: Matrices are defined by entering the elements row by row
>> M = [1 2 4; 3 6 8; 2 6 5] % creates a 3X3 matrix
>> M' % Transpose of matrix M
-1
*The inverse of M is M denoted in Matlab as M^-1
>> M^-1
>> M(2,3) % displays the element of second row and third column
7
DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS
5. Special Matrices
M = [ ] ; % null matrix:
M = zeros(n,m); % n x m matrix of zeros:
M = ones(n,m); % n x m matrix of ones:
M = eye(n); % n x n identity matrix:
6. Help, Document and Demos : Matlab provides excellent tutorials that are
accessible by typing >> demo
The Basic matrix operations tutorial under the Matrices tutorial, the Image
Processing and Signal Processing tutorial under Toolboxes are highly recommended.
To get information on a particular function of Matlab, we type help function_name
>> help fft % help for Fast Fourier Transform
>> help mean % help for mean or average value
Comments in Matlab must be proceeded by % .
To define a function in Matlab, we first create a function with the name of the
function. We then define the function in the file. For example, the Matlab
documentation recommends stat.m written as below for calculating mean and
standard deviation of a signal ‘ x ’.
8
DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS
9
DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS
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);
11. Using Matlab Editors : Very small programs can be typed and executed in
command window. Large programs on other hand can be used the Matlab editor.
The complete program can be typed in the Matlab editor and saved as
'file_name.m', and can be retrieved when-ever
necessary. You can execute either directly from
editor window or type the file_name in the
command window and press the enter button.
Creating, Saving, and Executing a Script File
% CIRCLE - A script file to draw a pretty circle
10
DEPARTMENT OF ECE ECE2003-SIGNALS AND SYSTEMS
11