Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Experiment No.

1 INTRODUCTION TO MATLAB AND


SIMULINK
1.1 Objective
To familiarize students with MATLAB and Simulink interface.

1.2 Introduction to MATLAB


1.2.1 The MATLAB Interface
1.2.1.1 The Prompt
Open MATLAB on your computer. You will be greeted by an interface similar to this one:

The >> symbol indicates that MATLAB is ready to accept input and it is called a prompt.
Type in the following and press enter:
>> x = 3
You will see two changes: First, MATLAB will output the value now stored in x. Second,
on the right, x will appear in the list of variables. When there is a long process taking place,
the prompt will disappear and appear again when the calculation has completed. Usually
when you add a semicolon after a statement, it means that the output won’t be shown on
the screen.
1.2.1.2 The MATLAB Editor
When writing long programs, or writing programs you may want to edit or run over and
over, it is better to create a script in the editor. Click “New Script” near the top left to open
the editor.
The editor is where you will write and save all your programs. For example, type the
following code in the editor:
clc; x =
inv(x);
disp(x);
And press “Run”. Once you do that, it will ask you where to save your script. Save it
somewhere. You should see something similar to the following:
1.2.1.3 Using MATLAB as a Calculator Try
entering the following in MATLAB:
>> 3 +
5 ans =
8 >> 3
- 5 ans
= -
2
Experiment with MATLAB. Do the same as we have done above with different numbers
and also*, / and ^.

Apart from normal division in MATLAB, there is also the left-division operator, \. It
divides the second number (on its right) by the first and gives the result.
You can use parentheses in MATLAB as you would in normal mathematics, or on your
calculator. For example,
>> (3 + 5) *
8 ans =
64 >> 3 + 5 *
8 ans =
43
Note that MATLAB does not simply evaluate expressions from left to right, but it respects
the order of operations, which means that it does multiplication and division first, then
addition and subtraction.

1.2.2 Variables

In MATLAB, you can store the results of any calculation in a variable. Try the following
in MATLAB:
>> x = 1 / 2
x =
0.5000
>>x = 3;
If you look on the right side of MATLAB in the Workspace, you will see at least two
variables defined. You will see x with the value of 3 that you just assigned, and you will
see ans, which stores the result of the last calculation you did. Just keep in mind a few
rules, MATLAB variables always start with a letter, and can have letters, numbers or
underscores in them.
1.2.3 Built-In Functions and Variables

MATLAB has a big library of built-in functions that can do special jobs. For example,
>> theta = pi;
>> x = cos(theta);
>> y = sin(theta);
Check the workspace and you will see that it has three new variables: theta, x, and
y. In the table below you will find some common functions used in calculations and
their explanation. Note also that the semicolon after a statement means it won’t show
the output, but it will store it.
MATLAB Expression Meaning
sqrt(x) √𝑥
𝑛
nthroot(x, n)
√𝑥
log(x) ln 𝑥 = log𝑒 𝑥
log10(x) lg 𝑥 = log10 𝑥
log(x)/log(b) log𝑏 𝑥
exp(x) 𝑒𝑥
pi 𝜋
sin(x) sin 𝑥
cos(x) cos 𝑥
tan(x) tan 𝑥
csc(x) csc 𝑥 = 1
sin𝑥
sec(x) sec 𝑥 = 1
cos𝑥
cot(x) cot 𝑥
tan𝑥
asin(x) sin−1 𝑥
acos(x) cos−1 𝑥
atan(x) tan−1 𝑥
atan2(y,x) tan (𝑦, 𝑥) (takes care of the quadrant)
−1

sinh(x) sinh𝑥 = 𝑒 𝑥−𝑒−𝑥


2
cosh(x) cosh 𝑥 = 𝑒 𝑥+𝑒−𝑥
2
tanh(x)
𝑒𝑥−𝑒−𝑥
cosh𝑥 𝑒𝑥+𝑒−𝑥
i 𝑖
real(z) Re{𝑧}
imag(z) Im{𝑧}
abs(z) |𝑧|
angle(z) ∠𝑧
conj(z) 𝑧∗
rand Random number between 0 and 1.
randn Random number that follows a standard
Normal (Gaussian) distribution.
randi([min max]) Random integer between min and max.
1.2.4 Binary Operations in MATLAB

MATLAB has a lot of operators and functions you can use to check relationships between
two numbers. Each of them returns 0 or 1, for false and true, respectively.
MATLAB Expression Explanation

a == b Is a equal to b?
a > b Is a greater than b?
a >= b Is a greater than or equal to b?
a < b Is a less than b?
a <= b Is a less than or equal to b?
a ~= b Opposite of a == b.
a && b Returns true if both a and b are true.
a || b Returns true if one or both of a and b are true.
xor(a, b) Returns true if exactly one of a and b are true.
~a Returns the opposite of a.
true Always true.
false Always false.
We can use these as follows:
>> 5 >
3 ans =
1 >> 5
< 3 ans
=
0
>> 5 > 3 && 3 <
5 ans = 1

1.2.5 The Help Section

Are you stuck somewhere? There’s always the MATLAB help. Please attempt to solve
your own problems before asking the instructor; that’s how you learn. There are two main
ways to access the help. The first is by pressing F1 on your keyboard. This should bring
up a window like the following in which you can search for in-built functions or help, or
look up the syntax of a function:
The second way is to type in help<function name>. This way, you can quickly look
up the syntax or what a function does without opening the Help Centre. Type in help inv.
You will see something like the following:

You can also jump directly to the help section of a function by typing doc <function
name>.

1.2.6 Vectors/Arrays

Row vectors, also known as 1-D arrays, are defined in MATLAB like this:
>> x = [2 5 4 3 1]
x =
2 5 4 3 1
>> x = [2, 5, 4, 3, 1] x =
2 5 4 3 1
To access one element of a row vector, you can do this:
>>
x(3)
ans =
4
Note that the numbering of an array starts at 1 in MATLAB. Many of the functions we
studied above can be applied to arrays as well. For example,
>> sqrt(x)
ans =
1.4142 2.2361 2.0000 1.7321 1.0000
If arrays are of the same length, addition and subtraction works as usual. You can also
multiply/divide an array by a number (also known as a scalar) with * and /. However, if
you want to multiply two arrays or divide them element by element, you need to use the
operators.*and . /, along with .\, for example:

>> x .* x
ans =
4 25 16 9 1
You can do something similar for exponentiation:
>> x .^ 3
ans =
8 125 64 27 1
A similar thing applies for binary operators. You need to use & instead of && and | instead
of || when working with arrays.

Here are some special commands for generating arrays, along with their explanation. Note
that commands that have a different behavior as compared to scalars are repeated below
for clarity.
MATLAB Expression Explanation

An array starting at a and ending at b with


a:b
steps of 1.
An array starting at a and ending at b with a
a:step:b
step size of step.
An array starting at a, ending at b, with a total
linspace(a, b, n)
of n points.
An array of n points where each element is a
rand([1 n])
random number between 0 and 1.
An array of n points where each element is a
randn([1 n])
sample from a standard normal distribution.
An array of random integers with n points,
randi([min max], [1 n])
each of them between min and max.
true([1 n]) An array of n points, all containing true.
false([1 n]) An array of n points, all containing false.
zeros([1 n]) An array of n points, all containing 0.
ones([1 n]) An array of n points, all containing 1.
mean(a) Mean of all elements of a.
median(a) Median of all elements of a.
sort(a) Sorts the elements of a.
Sort(a, 'descend'); Sorts the elements of a in descending order.
max(a) Maximum of all elements of a.
min(a) Minimum of all elements of a.
Stores the maximum of a in m and its index in
[m, i] = max(a)
i.
Stores the minimum of a in m and its index in
[m, i] = min(a)
i.
sum(a) Sum of all elements of a.
Cumulative sum, where each element is the
cumsum(a)
sum of all previous elements.
prod(a) Product of all elements of a.
diff(a) Difference between successive elements of a.
Returns the indexes where the array a is not 0
find(a)
or false.
Returns x only where a is not false. For
example, if x = [1 2 3 4 5] and a =
x(a)
[true false true false true], then
x(a) will return [1 3 5].

You can also combine indexing and arrays. For example,


>> x(1:3)
ans =
1 2
3 >> x(1:2:5) ans
=
1 3
5 >> x(4:end) ans
= 4 5 >>
x(end-2:end) ans
=
3 4 5

1.2.7 Matrices

First, we will go through some basic operations in MATLAB. Basic programming


knowledge is assumed. If the matrix 𝑥 is defined as
𝑥
We write this in MATLAB as
>>x = [1 2; 3 4];
or, equivalently
>> x = [1, 2; 3, 4]
x = 1 2
3 4
Below are some MATLAB expressions relating matrices and their explanation.
MATLAB Expression Explanation

A * B The matrix multiplication of A and B.


A.' The transpose of the matrix A.
A' The conjugate-transpose of the matrix A.
A^n The square matrix A to the power of n.
An matrix of order 𝑚 × 𝑛where each element
rand([m n])
is a random number between 0 and 1.
An matrix of order 𝑚 × 𝑛 where each element
randn([m n]) is a sample from a standard normal
distribution.
An matrix of random integers of order 𝑚 × 𝑛,
randi([min max], [m n])
each of them between min and max.
true([m n]) An matrix of order 𝑚 × 𝑛, all containing true.
An matrix of order 𝑚 × 𝑛, all containing
false([m n])
false.
zeros([m n]) An matrix of order 𝑚 × 𝑛, all containing 0.
ones([m n]) An matrix of order 𝑚 × 𝑛, all containing 1.
eye(n) An identity matrix of order 𝑛 × 𝑛.
inv(x) The inverse of square matrix x.
det(x) The determinant of the square matrix x.
size(x) Returns [m n] if x is 𝑚 × 𝑛
If x is a matrix, this returns an array with the
diagonal elements of x. If x is an array, this
diag(x)
creates a diagonal matrix where the elements of
x lie along the diagonal and the rest are zeroes.
Solves the matrix equation 𝐴𝑋 = 𝐵 for the
A\B or B/A matrix 𝑋. More efficient than doing inv(A) *
B.
Returns x only where a is not false. For
example, if x = [1 2; 3 4] and a =
x(a)
[true false; false true], then x(a)
will return [1 4].
1.3 Introduction to Simulink
1.3.1 The Simulink Interface

To start using Simulink, type in simulink at the MATLAB prompt and press enter.
Some time will pass, and you will be presented with a window similar to this one:

Click on the (new model) icon, near the top center of the window. A new window will
pop up in which you can view and edit your model.
In the Simulink Library Browser, click on Sources under Simulink, and drag the Sine wave
generator onto your model. Your model and window will look something like this:
The outwards pointing arrow from the sine-wave generator is an output of the Sine Wave
block. You can take the input of this block and feed it to another block. Double-clicking
the block brings up different properties related to it you can modify.
Drag out a Scope block from Sinks and connect it to the Sine Wave block by selecting the
Sine Wave block, holding down the Ctrl key, and clicking the Scope block. Then click the
button and double-click on the scope. Observe the output.

1.4 Lab Task

Question 1
Create a vector 𝑎 = [0, 0.1, 0.2, … , 10] and a vector 𝑏 = [cos0 , cos0.2 , … , cos 20].
Compute the following:

a. 𝑐 where 𝑐𝑖 = 𝑎𝑖/𝑏𝑖.
b. 𝑑 where 𝑑𝑖 = 𝑎𝑖4
c. The element-wise product of 𝑎 and 𝑏.
d. The dot product of 𝑎 and 𝑏.

Question 2
Create a 3 × 3 matrix with all its elements randomly distributed in [0, 1]. Compute:

a. The inverse matrix.


b. The transpose of the matrix.
c. The determinant of the matrix.
d. The size of the matrix.

Question 3
Use the built-in function eig to find the eigenvalues and eigenvectors of the matrix 𝑥,
where
1⁄4 1⁄4 5⁄4
𝑥 ≜ [ −1 2 1 ]
− 5⁄4 − 1⁄4 15⁄4
Hint: If you are stuck, use help eig to find out all the different things eig can do.

Question 4
a. Create a function that plots the function sinc 𝑥 = sin .
𝜋𝑥(𝜋𝑥)

b. Plot, using your function, the graph of sinc 𝑥 in the interval[−2𝜋, 2𝜋].
Question 5
a. Find out, how functions are written in MATLAB? Write a function that performs the
convolution of two input signals without using any built-in functions, other than
those we have covered in this lab manual.
b. Compare the output of your function with the built-in conv function for multiple
inputs to see that it produces the same output. Hint: Use all (A == B) to compare
two arrays.

Question 6
The inputs given below are used for what characterization of the system. Explain briefly.
1) Impulse
2) step
3) ramp
4) sinusoidal
Question 7
Implement the following transfer function in Simulink.

1
𝐻(𝑠) = 𝑠 2+ 2𝑠 + 1

Observe its

• Impulse response
• Step Response
You may use any blocks or functions available in MATLAB or Simulink.
Hint: You will need the Step, Scope, Derivative and Transfer Function blocks.

You might also like