0% found this document useful (0 votes)
19 views10 pages

Signals and Systems Experience No 1

This document provides a comprehensive introduction to MATLAB, covering its environment, basic operations, and applications for mathematical computations and visualizations. It includes instructions for starting MATLAB, performing arithmetic operations, creating and manipulating matrices and vectors, and obtaining help within the software. Additionally, it demonstrates how to generate plots using built-in functions like linspace and logspace, along with exercises to reinforce learning.

Uploaded by

Zulqurnan Anjum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views10 pages

Signals and Systems Experience No 1

This document provides a comprehensive introduction to MATLAB, covering its environment, basic operations, and applications for mathematical computations and visualizations. It includes instructions for starting MATLAB, performing arithmetic operations, creating and manipulating matrices and vectors, and obtaining help within the software. Additionally, it demonstrates how to generate plots using built-in functions like linspace and logspace, along with exercises to reinforce learning.

Uploaded by

Zulqurnan Anjum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Experiment No.

1
Getting Started with MATLAB: A Comprehensive Introduction
Objective:
To familiarize yourself with the MATLAB environment, understand basic operations, and
learn to use MATLAB for simple mathematical computations and visualizations.
Introduction:
The name MATLAB stands for MATrix LABoratory. MATLAB was written originally to
provide easy access to matrix software developed by the LINPACK (linear system package) and
EISPACK (Eigen system package) projects. MATLAB is a high-performance language for
technical computing. It integrates computation, visualization, and programming environment.
Furthermore, MATLAB is a modern programming language environment: it has sophisticated
data structures, contains built-in editing and debugging tools, and supports object-oriented
programming. These factors make MATLAB an excellent tool for teaching and research.
MATLAB has many advantages compared to conventional computer languages for solving
technical problems. MATLAB is an interactive system whose basic data element is an array that
does not require dimensioning. The software package has been commercially available since
1984 and is now considered as a standard tool at most universities and industries worldwide. It
has powerful built-in routines that enable a very wide variety of computations. It also has easy to
use graphics commands that make the visualization of results immediately available. Specific
applications are collected in packages referred to as toolbox. There are toolboxes for signal
processing, symbolic computation, control theory, simulation, optimization, and several other
fields of applied science and engineering. In addition to the MATLAB documentation, which is
mostly available online, we would recommend the following books: [2], [3], [4], [5], [6], [7], [8],
and [9]. They are excellent in their specific applications.
Starting with MATLAB:
After logging into your account, you can enter MATLAB by double-clicking on the
MATLAB shortcut icon (MATLAB 7.0.4) on your Windows desktop. When you start MATLAB,
a special window called the MATLAB desktop appears. The desktop is a window that contains
other windows. The major tools within or accessible from the desktop are:
1. The Command Window
2. The Command History
3. The Workspace
4. The Current Directory
5. The Help Browser
6. The Start button

1
Figure 1.1: The graphical interface to the MATLAB workspace
When MATLAB is started for the first time, the screen looks like the one that is shown in Figure
1.1. This illustration also shows the default configuration of the MATLAB desktop. You can
customize the arrangement of tools and documents to suit your needs.
Now, we are interested in doing some simple calculations. We will assume that you have
sufficient understanding of your computer under which MATLAB is being run.
You are now faced with the MATLAB desktop on your computer, which contains the prompt
(>>) in the Command Window. Usually, there are 2 types of prompts:
>> for full version
EDU> for educational version
Note: To simplify the notation, we will use this prompt, >>, as a standard prompt sign, though
our MATLAB version is for educational purposes.
Basic Commands in MATLAB:
Arithmetic Operations:
1. Addition: +
2. Subtraction: -
3. Multiplication: *
4. Division: /
5. Power: ^

2
Using MATLAB as a calculator:

As an example of a simple interactive calculation, just type the expression you want to
evaluate. Let’s start at the very beginning. For example, let’s suppose you want to calculate
the expression, 1 + 2 × 3. You type it at the prompt command (>>) as follows,

>>x=1+2*3

Ans=7

You will have noticed that if you do not specify an output variable, MATLAB uses a default
variable Ans, short for answer, to store the results of the current calculation. Note that
variable Ans is created (or overwritten, if it has already existed). To avoid this, you may
assign a value to a variable or output argument name. For example,

>>x=1+2*3

Ans=7

will result in x being given the value 1+2*3 =7. This variable name can always be used to
refer to the results of the previous computations. Therefore, computing 4x will result in
>> 4*x

Ans=28.0000
Quitting MATLAB:

To end your MATLAB session, type quit in the Command Window or select File −→Exit
MATLAB in the desktop main menu.
Creating and manipulating matrices and vectors
MATLAB works on matrices and vectors that may be real or complex. Variables do not have
to be “declared”. Results can be displayed in a variety of different formats.
Let us enter a row vector into the MATLAB workspace. Type in:
v = [2 4 7 5]
This creates a variable v whose current value is a row vector with four elements as shown.
After pressing “return” the value of v will have been echoed back to you. To suppress the
echo, one uses a semi-colon following the command line. Thus
w = [1 3 8 9];
Creates another row vector w but does not echo. To check that we have appeared in the
MATLAB workspace, type,
who
Which will display all the variables in the MATLAB workspace, and to check the value of w
simply type w
Operations on row vectors can be best illustrated by some simple exercises.

3
Exercise 1: Investigate the effect of the following commands:
v = [2 4 7 5]
w = [1 3 8 9]
(a) v (2)
Ans = 4
(b) sum = v + w
Ans = [3,7,15,14]
(c) diff = v – w
Ans = [1,1, -1, -4]
(d) vw = [v w]
Ans = [2,4,7,5,1,3,8,9]
(e) vw (2:6)
Ans = [4, 7, 5, 1, 3]
(f) v’
Ans = [2
4
7
5]
One way to generate a column vector is to take the transpose of a row vector, as you will
have found in exercise 1(f). Column vectors can be typed directly in one or two ways. For
example, to enter the command vector

[]
1
1
z=
0
0
you can type
z = [1; 1; 0; 0]; or
z = [1
1
0
0]
Exercise 2: Investigate the effect of the following commands.
(i) z’ (b) z*v (c) [v; w] (d) v*z (e) [z; v’] (f) z + v’
(a) z’
ans= [1 1 0 0]
(b) z*v
ans= [2 4 7 5
2 4 7 5
0 0 0 0
0 0 0 0]
(c) [v; w]
Ans= [2 4 7 5
1 3 8 9]

(d) v*z

4
ans= [6]

(e) [z; v’]


Ans= [2;4;7;5]

(f) z + v’
Ans= [3;5;7;5]

One way of creating matrices is by multiplying row and column vectors as seen in the
above exercises. There are various ways of entering matrices. For example, to enter the
matrix
M=
[ ]
1 2
3 4
The most obvious ways are to type
M = [1 2; 3 4]
but there are more perverse ways such as
M = [[1 3]’ [2 4]’]
Exercise 3: Investigate the effect of the following commands:
(a) N = inv(M) (b) M*N (c) I = eye (2) (d) M + I (e) M*z (1:2) (f) v (3:4) *M
(g) M (1,1) (h) M (1:2,1:2) (i) M (:1) (j) M (2, :)

(a) n= inv(m)
Ans= [-2.0000 1.0000;1.50000 -0.50000]
(b) m*n
Ans= [1.0000 0;0 1.0000]
(c) i= eye (2)
Ans= [1 0;0 1]
(d) m + i
Ans= [2 2;3 5]
(e) m*z (1:2)
Ans= [3;7]
(f) v (3:4) *m
ans= [22,34]
(g) m (1,1)
Ans= [1]
(h) m (1:2,1:2)
Ans= [1,2;3,4]
(i) m (:1)
Ans= [1;3]
(j) m (2:)
Ans= [3 4]

Getting help:

5
To view the online documentation, select MATLAB Help from Help menu or
MATLAB Help directly in the Command Window. The preferred method is to use the
Help Browser. The Help Browser can start by selecting the? icon from the desktop toolbar.
On the other hand, information about any command is available by typing
>> help Command
Another way to get help is to use the look for command. The look for command differs
from the help command. The help command searches for an exact function name match,
while the look for command searches the quick summary information in each function for
a match. For example, suppose that we were looking for a function to take the inverse of
a matrix. Since MATLAB does not have a function named inverse, the command help
inverse will produce nothing. On the other hand, the command look for inverse will
produce detailed information, which includes the function of interest, inv.
>> look for inverse
NOTE - At this time of our study, it is important to emphasize one main point. Because
MATLAB is a huge program, it is impossible to cover all the details of each function one by
one. However, we will give you information on how to get help. Here are some examples:
Use on-line help to request info on a specific function
>> help sqrt
In the current version (MATLAB version 7), the doc function opens the on-line version of
the help manual. This is very helpful for more complex commands.
>> doc plot
Use look for to find functions by keywords. The general form is
>> look for Function Name
Exercise 4:
Use the help command to find out about the following built-in functions and make up your own simple
examples:
1. ones 2. zeros 3. det 4. Linspace 5. Logspace
1. ones
The ones’ function creates an array filled with ones. You can specify the size of the array.
>> help ones
Example:
A = ones (3, 4); % Creates a 3x4 matrix filled with ones disp(A).
2. zeros
The zeros function creates an array filled with zeros. You can specify the size of the array.
>> help zeros
Example:
B = zeros (2, 5); % Creates a 2x5 matrix filled with zeros disp(B).
3. det
The det function computes the determinant of a square matrix.
>> help det
Example:
C = [4, 7; 2, 6]; % A 2x2 matrix det_C = det(C); % Calculates the determinant disp(det_C).

4. Linspace

6
The linspace function generates linearly spaced vectors. It is particularly useful for creating a
range of values with a specified number of points.
MATLAB Help:
>> help linspace
Example:
D = linspace (0, 10, 5); % Generates 5 linearly spaced points between 0 and 10 disp(D).
5. logspace
The logspace function generates logarithmically spaced vectors, often used in logarithmic scale
plotting.
>> help logspace
Example:
E = logspace (1, 3, 4); % Generates 4 points logarithmically spaced between 10^1 and 10^3
disp(E).
Graphs:
The results of signal processing computations in MATLAB are huge matrices containing, for example,
frequency responses, which one would like to display in graphical form. We will go through a simple
example of generating data, plotting the graph, putting labels on the axes etc.
We will plot the graph of
Graph of linspace:
The linspace function generates linearly spaced values. Here's how to visualize it on a graph.
Code:
x = linspace (0, 10, 100); % 100 linearly spaced points between 0 and 10
y = sin(x); % Compute sine of x
plot(x, y); % Plot the sine wave
title('Sine Wave using linspace');
xlabel('x values');
ylabel('sin(x)');
grid on;
Graph of logspace:
The logspace function generates logarithmically spaced values. Here's an example to plot these
values.
Code:
x = logspace (0, 2, 50); % 50 logarithmically spaced points between 10^0 and 10^2
y = log10(x); % Compute the base-10 logarithm of x
semilogx(x, y); % Plot on a logarithmic x-scale
title('Logarithmic Scale Plot using logspace');
xlabel('Logarithmic x values');
ylabel('log10(x)');
grid on;

Graph with ones and zeros:

7
Visualize arrays created using ones and zeros.
Code:
x = 1:10; % Create x values from 1 to 10
y1 = ones(1, 10); % Create a constant array of ones
y2 = zeros(1, 10); % Create a constant array of zeros
plot(x, y1, '-o', x, y2, '-s'); % Plot both arrays
title('Ones and Zeros Plot');
xlabel('x values');
ylabel('y values');
legend('Ones', 'Zeros');
grid on;
Graph for Matrix Determinant (det):
A 3D plot for determinants of matrices with varying values.
Code:
[X, Y] = meshgrid (1:5, 1:5); % Create a grid of values
Z = arrayfun (@(x, y) det([x, y; y, x]), X, Y); % Compute determinants
surf (X, Y, Z); % 3D surface plot
title ('Determinant of 2x2 Matrices');
xlabel ('x values');
ylabel ('y values');
zlabel('Determinant');
These examples illustrate how to use MATLAB graphing capabilities with the built-in functions.
Exercise 5:
Generate a vector yc containing the values of cos(t) for the same time range as before.
Code:
% Define the time range
t = linspace (0, 2*pi, 100); % 100 points between 0 and 2π
% Compute the cosine values
yc = cos(t);
% Display the vector
disp ('The values of cos(t):');
disp(yc);
% Plot the cosine function
plot (t, yc, '-b', 'LineWidth', 1.5);
title ('Cosine Function');
xlabel ('Time (t)');
ylabel('cos(t)');
grid on;

Explanation:
 linspace (0, 2*pi, 100): Creates a vector t with 100 evenly spaced points between 0 and
2π2\pi2π. This represents the time range.
 cos(t): Computes the cosine of each value in the vector t, storing the results in yc.

8
 Plot: Displays the cosine wave as a function of time.
From the “help” entry for “plot” find out how to get both ys and yc plotted against t on the
same axis.
Code:
% Define the time range
t = linspace (0, 2*pi, 100); % 100 points between 0 and 2π
% Compute sine and cosine values
ys = sin(t); % Values of sin(t)
yc = cos(t); % Values of cos(t)
% Plot both ys and yc against t
plot(t, ys, '-r', 'LineWidth', 1.5); % Plot sine (red line)
hold on; % Hold the current plot
plot(t, yc, '-b', 'LineWidth', 1.5); % Plot cosine (blue line)
hold off; % Release the plot
% Add titles and labels
title('Sine and Cosine Functions');
xlabel('Time (t)');
ylabel('Function Value');
legend('sin(t)', 'cos(t)'); % Add legend
grid on; % Add grid for better visualization
Explanation:
 plot (t, ys, '-r'): Plots sin⁡(t)\sin(t)sin(t) as a red line.
 hold on: Allows you to overlay the next plot on the same axes.
 Plot (t, yc, '-b'): Plots cos⁡(t)\cos(t)cos(t) as a blue line.
 hold off: Releases the plot so subsequent plots do not overlay.
 legend: Adds a legend to differentiate the two curves.
You can run this code in MATLAB to see sin⁡ (t)\sin(t) sin(t) and cos⁡ (t)\cos(t) cos(t) plotted
together on the same axis.
From the “help” entry for “subplot” find out how to plot ys and yc plotted on two separate
graphs, one above the other.
Code:
% Define the time range
t = linspace (0, 2*pi, 100); % 100 points between 0 and 2π
% Compute sine and cosine values
ys = sin(t); % Values of sin(t)
yc = cos(t); % Values of cos(t)
% Create the first subplot for sine
subplot (2, 1, 1); % 2 rows, 1 column, plot in the 1st position
plot (t, ys, '-r', 'LineWidth', 1.5); % Plot sine in red
title ('Sine Function');
xlabel ('Time (t)');
ylabel('sin(t)');
grid on;

9
% Create the second subplot for cosine
subplot (2, 1, 2); % 2 rows, 1 column, plot in the 2nd position
plot (t, yc, '-b', 'LineWidth', 1.5); % Plot cosine in blue
title ('Cosine Function');
xlabel ('Time (t)');
ylabel('cos(t)');
grid on;
Explanation:
 subplot (2, 1, 1): Divides the figure into 2 rows and 1 column, then activates the 1st plot area for
ys=sin⁡(t)ys = \sin(t)ys=sin(t).
 subplot (2, 1, 2): Activates the 2nd plot area for yc=cos⁡(t)yc = \cos(t)yc=cos(t).
 plot: Plots each function in its respective subplot.
 Titles, labels, and grids: Enhance clarity and visualization for each graph.
Run this code in MATLAB to generate the desired plots.
Conclusion:
Summarize your understanding of MATLAB’s interface, basic operations, and how it can be used
for computations and visualizations.

1
0

You might also like