Lab1-2
Lab1-2
Lab1-2
The objective of this lab is to introduce the basic operations of MATLAB. Read through the
handout and practice each new command by completing the examples and exercise. Turn-in
the answers for all the exercise problems as your lab report.
When answering the problems, indicate the commands you entered and the output displayed. It
may be easier to open a word document, and after each command you may copy and paste the
commands and outputs to the word document. Similarly, plots can also be pasted to your word
document.
1. Introduction
In this we will learn
how to perform basic mathematical operations on simple variables, vectors, matrices
and complex numbers.
generate 2-D plots.
2. Matlab commands
2.1 Help
Typing help by itself on the command line gives you a list of help topics. If want to find more
about the syntax of a certain command, type in
>> help function_name
where the word function_name is in the above entry is substituted by the actual name of a
function for which description is sought. For example, we can type in
>> help plot
and MATLAB will display the description, syntax and options of the plot function. The other
helpful command is lookfor. If we are not sure the exact name of the function, we can make a
search for a string which is related to the function, and MATLAB displays all m-files (commands)
that have a matching string. MATLAB searches for the string on the first comment line of the m-
file. For example:
>>lookfor inverse
will display all m-files which contain the string ‘inverse’ in the comment line.
Two other useful commands are
>>whos
which lists all your active variables (this info also appears in your Workspace Window), and
>> clear
which clears and deletes all variables (for when you want to start over).
MATLAB has tab-completion for when you’re typing long function names repeatedly. This
means that you can type part of a command and then press <Tab> and it will fill in the rest of
the command automatically. Moreover, MATLAB stores command history, and previous
commands can be searched by pressing the up or down arrow keys.
Exercise 1:
Define the following arrays in MATLAB: a = [50 20] and b = [9 11]
Then issue the following commands. Show all your commands and results.
b’
a * b’
a .* b
a’ * b
5*b
Briefly explain what each of these three operators does:
′ * .*
Exercise 2:
For the matrices given above, perform the following operation:
a*b
What is the error message? What does the message mean? What is the correct fix?
Exercise 3:
Perform the following:
c=a+b
d = a + b;
What does the ; do?
Exercise 4:
Define e = 90 in MATLAB. Use size to find the dimensions of a, b’, and e?
There is an alternate syntax for size to determine the length of a vector. Can you guess? Search
for the command either by using the help commands of MATLAB or by searching online. Use
the alternate syntax for size to determine the height of b.
2.4 Matrices
You can also just use whitespace to separate elements within a row. The following two
ways to define the variable are equivalent:
>> y = [20, 30, 40]
y=
20 30 40
>> y2 = [20 30 40]
y2 =
20 30 40
The ( ) operator allows you to access the contents of a matrix. MATLAB is 1-indexed, meaning
that the first element of each array has index 1 (indexing starts from 1 not from 0).
>> y = [10, 20, 30];
>> y(1)
ans =
10
To access a single element in a multidimensional matrix, use (m, n). The syntax is
matrixName(row, column):
>> y= [10, 20; 30, 40];
>> y(2, 1)
ans =
30
There is a quick way to define arrays that are a range of numbers, by using the : operator.
Exercise 5:
Define the following:
u = 10 : 0.2 : 30;
v = -10: 0.4 : 15;
How big (size) are u and v? Write the command to find their sizes. What are the 1st, 2nd, 3rd,
and the last elements of each matrix/vector? Briefly describe what exactly u and v are?
Exercise 6:
1. Enter sqrt(-1) into MATLAB. Does the result make sense?
2. Enter 3i + 2j. Does the result make sense?
3. Define z1 = 5 + 4j. Find the magnitude, phase, and real part of z using the following operators:
abs(x), angle(x), real(x), imag(x). Is the phase in degree or radian? (How to check?)
4. Find the magnitude of z1 + z2, where z2 = 3exp(1/2 * j * π)
5. Compute the magnitude of i 3. What do you expect the result to be?