0% found this document useful (0 votes)
2 views5 pages

Lab1-2

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

MEGN201

LAB 1 Introduction to Matlab

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.

2.2 Matrix Operations


MATLAB is designed to operate efficiently on matrices (hence the name MATLAB = “Matrix
Laboratory). Consequently, MATLAB treats all variables as matrices, even scalars! Like many
other programming languages, variables are defined in MATLAB by typing:
<VariableName> = <Assignment>
MATLAB will then acknowledge the assignment by repeating it back to you. The following
are what you would see if you defined a scalar x, a vector y, and a matrix z:
>> x = 3
x=
3
>> y = [1, 2, 3]
y=
123
>> z = [1, 2, 3; 4, 5, 6]
z=
123
456
Matrices are denoted by square brackets [ ]. Commas separate the elements within a row, and
semicolons separate different rows. A row array, such as y, is just a special case of a matrix
that has only one row.

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?

2.3 size command


The size command is extremely useful. This command tells you the dimensions of the matrix
that MATLAB is using to represent the variable. To determine the dimension of a matrix x, for
example, you type in
>> size(x)

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?

Create the following vector k as follows:


w = -3 : 0.1 : 5;
How big is w?
What are the 1st, 2nd, 3rd, and the last elements in w? (show your Matlab commands)
What exactly does w contain?

2.5 Complex numbers


One of the strengths of MATLAB is that most of its commands work with complex numbers,
too. MATLAB, by default, uses the letter i for the square root of (-1). However, electrical
engineers typically prefer using j, and so MATLAB has both predefined. Because of this, you
may wish to avoid using i and j as variables if your code deals with complex numbers. That
being said, everything in MATLAB is a variable. You may redefine the variables i and j to be
anything you like.

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?

You might also like