UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING
SIGNAL AND SYSTEMS LAB
Lab-01:Basic Data Operators in MATLAB and use of build-in
functions
1.1 Objectives:
• To learn the basics of MATLAB
• Evaluate different Mathematical Expressions
• To use various build-in functions
1.2 Equipment Required:
Computer/Laptop with MATLAB installed.
1.3 Starting 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:
• The COMMAND WINDOW
• The COMMAND HISTORY
• The WORKSPACE
• The CURRENT DIRECTORY
• The HELP BROWSER
• The START button.
Semester: Spring-2024
Session: 2K21 Designed by: Dr. Junaid Mir, Engr Zainab Shahid
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING
1.4 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
calculatethe expression, 1 + 2 × 3. You type it at the prompt command (>>) as follows,
>> 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 the variable ans is created (or overwritten, if it already existed).
To avoid this, you may assign a value to a variable or output argument name. For
example,
>> x = 1+2*3
x
=
7
Semester: Spring-2024
Session: 2K21 Designed by: Dr. Junaid Mir, Engr Zainab Shahid
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING
Creating MATLAB variables:
MATLAB variables are created with an assignment statement. The syntax of variable
of assignment is:
variable name = a value (or an expression)
For example,
>> x = expression
where expression is a combination of numerical values, mathematical operators,
variables, and function calls. On other words, expression can involve:
• manual entry
• built-in functions
• user-defined functions
Overwriting variables
Once a variable has been created, it can be reassigned. In addition, if you do not
wish to see the intermediate results, you can suppress the numerical output by putting
a semicolon (;) at the end of the line. Then the sequence of commands looks like this:
>> t = 5;
>> t = t+1
t =
6
Semester: Spring-2024
Session: 2K21 Designed by: Dr. Junaid Mir, Engr Zainab Shahid
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING
Error messages
If we enter an expression incorrectly, MATLAB will return an error message. For
example, in the following, we left out the multiplication sign, *, in the following
expression.
>> x = 10;
>> 5x
??? 5x
|
Error: Unexpected MATLAB expression.
Controlling the hierarchy of operations or precedence
Let’s consider the previous arithmetic operation, but now we will include parentheses.
For example, 1 + 2 × 3 will become (1 + 2) × 3
>> (1+2)*3
ans =9
Managing the workspace
The contents of the workspace persist between the executions of separate commands.
Therefore, it is possible for the results of one problem to influence the next one. To
avoid this possibility, it is a good idea to issue a clear command at the start of each
new independent calculation.
>> clear
The command clear or clear all removes all variables from the workspace.
This frees up system memory. To display a list of the variables currently in the
memory, type
Semester: Spring-2024
Session: 2K21 Designed by: Dr. Junaid Mir, Engr Zainab Shahid
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING
>> who
while, whos will give more details which include size, space allocation, and class of
the variables.
Entering multiple statements per line
It is possible to enter multiple statements per line. Use commas (,) or semicolons (;)
to enter more than one statement at once. Commas (,) allow multiple statements per
line without suppressing output.
1.5 Mathematical Functions
MATLAB offers many predefined mathematical functions for technical computing
which contains a large set of mathematical functions. Typing help elfun and help
specfun calls up full lists of elementary and special functions respectively.
There is a long list of mathematical functions that are built into MATLAB.
These functions are called built-in. Many standard mathematical functions, such as
sin(x), cos(x), tan(x), ex, ln(x), are evaluated by the functions sin, cos, tan, exp, and
log respectively in MATLAB.
Semester: Spring-2024
Session: 2K21 Designed by: Dr. Junaid Mir, Engr Zainab Shahid
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING
We illustrate here some typical examples which related to the elementary functions previously
defined.
>> a = 10; x = 142;
>> log10(x)
ans = 2.1523
Note the difference between the natural logarithm log(x) and the decimal logarithm
(base10) log10(x).
To calculate sin(π/4) and e10, we enter the following commands in MATLAB,
>> y = exp(a)
ans =
2.2026e+004
>> y = sin(pi/4)
ans =
0.7071
1.6 Entering a vector
A vector is a special case of a matrix. The purpose of this section is to show how to
create vectors and matrices in MATLAB. As discussed earlier, an array of 1 × n is
×
Semester: Spring-2024
Session: 2K21 Designed by: Dr. Junaid Mir, Engr Zainab Shahid
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING
called a row vector, whereas an array of dim m 1 is called column vector.
The elements of vectors in MATLAB are enclosed by square brackets and are
separated by spaces or by commas. For example, to enter a row vector, v, type,
>> v = [1 4 7 10 13]
v =
1 4 7 10 13
Column vectors are created in a similar way, however, semicolon (;) must separate
the components of a column vector,
>> w = [1;4;7;10;13]
w =
1
4
7
10
13
On the other hand, a row vector is converted to a column vector using the transpose
operator. The transpose operation is denoted by an apostrophe or a single quote (’).
>>
v’
1
4
7
10
13
Thus, v(1) is the first element of vector v, v(2) its second element, and so forth.
To create a vector, you can also use:
>> x=1:0.5:3
x=
Furthermore, to access blocks of elements, we use MATLAB’s colon notation (:).
For example, to access the first three elements of v, we write,
>> v(1:3)
ans =
1 4 7
Or, all elements from the third through the last elements,
>> v(3:end)
ans
Semester: Spring-2024
Session: 2K21 Designed by: Dr. Junaid Mir, Engr Zainab Shahid
UNIVERSITY OF ENGINEERING AND TECHNNOLOGY, TAXILA
DEPARTMENT OF ELECTRICAL ENGINEERING
=
7 10 13
where end signifies the last element in the vector. If v is a vector, writing.
>> v(:)
produces a column vector, whereas writing
>> v(1:end)
produces a row vector.
1.7 Lab Tasks:
1. Solve the following expressions:
a.
Write the MATLAB expression in the next page.
b. 6-2/5+7^2-1. Explain the precedence followed.
2. Learn to use help for built-in functions.
a. Search polyval() function in MATLAB and evaluate the polynomial p(s) = s2+5s+1 at
s=1.
b. For the above polynomial find the roots (Use roots())
3. Learn to create and amend vectors.
a. Create a row vector v =[1 5 4 9 8] and perform the following? Write the one-line code in
the line below.
• Add 13 to all the elements of vector.
• Take square root of all the elements.
• Compute square of all elements
• Make all the elements in odd index equal to ‘1’
b. Write the one-line code in the line below.
• Create a vector ‘x’ with elements 1 to 10 using colons operator (:)
• Omit the first 5 elements and display the vector ‘y’.
• Convert row vector to column vector.
1.8 Conclusion:
Semester: Spring-2024
Session: 2K21 Designed by: Dr. Junaid Mir, Engr Zainab Shahid