Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Lab Exercise 01

1) Evaluate the following mathematical expressions by transforming them into MATLAB (notation) and estimate
the values of x and y, respectively, by hand using the operational hierarchy, then use MATLAB, and verify the
results obtained by hand.
√42 −7
a) 𝑥𝑥 = 4 + 23 − × 42 − 1
22 −1
3 −42
2
b) 𝑦𝑦 = 1 +
�√24 +9+5�×23

Analytical Solution (a) Analytical Solution (b)


√42 − 7 32 − 42
𝑥𝑥 = 4 + 2 − 2 3
× 42 − 1 𝑦𝑦 = 1 +
2 −1 �√24 + 9 + 5� × 23

3 7
𝑥𝑥 = 12 − × 16 𝑦𝑦 = 1 −
3 30 × 8

36 − 48 − 3 7 √30𝑥𝑥8
𝑥𝑥 = 𝑦𝑦 = 1 − 𝑥𝑥
3 30 × 8 √30𝑥𝑥8

−15 1920 − 56√30


𝑥𝑥 = 𝑦𝑦 =
3 1920

𝑥𝑥 = −5 𝑦𝑦 = 0.8402

MATLAB Solution (a) MATLAB Solution (b)


X= 4+2^3-sqrt(4^2-7)/(2^2-1) *4^2-1 Y= 1+((3^2-4^2)/sqrt(2^4+9+5)+2^3
X= -5 Y= 0.8402

1) The following MATLAB program was written to evaluate the hypotenuse of a right triangle with sides A = 4
and B = 3. Fill out the missing comments for a better understanding of the code.
>>A = 3; % A is the base of the right-angled triangle
>>B = 4; % B is the perpendicular
>>C = sqrt(A^2+B^2); % Calculation of hypotenuse
>> Hypotenuse = C; % Indicating that C is the Hypotenuse
What would be the final output displayed on the screen? (Write in the exact same format as shown on the
screen)
Hypotenuse=5
2) Write down the correct syntax and the associated outputs for the following:

𝑐𝑐 = 𝑙𝑙𝑙𝑙(𝑒𝑒) + 𝑙𝑙𝑙𝑙(𝑒𝑒 3 )

Syntax C=log(exp(1))+log(exp(3))
Output C=4

𝑑𝑑 = 𝑙𝑙𝑙𝑙𝑙𝑙(𝑒𝑒) + 𝑙𝑙𝑙𝑙𝑙𝑙(𝑒𝑒 3 )

Syntax D=log(exp(1))+log(exp(3))

Output D=4

𝑒𝑒 = 𝜋𝜋

Syntax E=pi

Output E=3.1415
𝜋𝜋
𝑓𝑓 = 𝑐𝑐𝑐𝑐𝑐𝑐 � �
4

Syntax F=cos(pi/4)

Output F=0.7071

𝑔𝑔 = 𝑒𝑒 3√131

Syntax G=exp(131)^1//3

Output 9.2086e+18

ℎ = 𝑙𝑙𝑙𝑙𝑙𝑙(5) + log 𝑒𝑒 (5) + log 2 5

Syntax D=log10(5)+log(5)+log2(5)

Output 4.603

3) Use the format long and short specifiers to compare between the outputs of the following:

Command Format Long Format Short


a = pi 3.14159265358919 3.1415926
b = eps 2.220446049250313 2.22044604
c = a+b 3.141592653589793 3.14159265
d = realmax 1.79693134862316 1.79693134
e = realmin 2.225073858507201 2.22507385
f = intmax 2147483647 2147483647
flops - -
date 18-Jul-2021 18-Jul-2021
clock 1.0e+09*2021 1.0e+09*2021
cputime 1.1492187500000 1.14921875
computer AVALONSWARLOCK AVALONSWARLOCK
ver 9.2.0.5 (r2018b) 9.2.0.5 (r2018b)
beep -
license ‘Professional’ ‘Professional’

Also %comment on the function being performed in each command.

Command Function
a = pi Instantiates variable a with value pi
b = eps Instantiates variable b with value eps
c = a+b Instantiates variable c with value being sum of a and b
d = realmax Instantiates variable d with value realmax
e = realmin Instantiates variable e with value realmin
f = intmax Instantiates variable f with value int32
flops Measures the computers performance
date Shows the date
clock Shows date and time as vector
cputime Shows CPU time
computer Shows Computer’s name
ver Shows version of MATLAB installed
beep Produces OS beep sound
license Shows license type
ANS 1)

ANS 2(a)

ANS 3)
ANS 2(b)
ANS(4)

ANS(5)
Lab Exercise 02
1) Do as Directed and write down the output obtained:
a) >>A= input (‘Enter the MATLAB expression’) and find sqrt(pi)
ans = 1.7725
b) >>My _ name _ is = input (‘Enter your name’,’s’)

My_name_is ='Saad'
c) >>My _ name _ is = input (‘Enter your name’) %Don’t input anything!
0×0 empty char array
d) >> My _ name _ is = input (‘Enter your name’) %Input a name without quotes
'Saad'
e) >> My _ name _ is = input (‘Enter your name’) %Input a name with quotes

'"saad"'
f) >> format compact
>> A = [1 2; 3 4]

A = 1 2
3 4
1 2 6 7
g) Let, 𝐴𝐴 = � � 𝑎𝑎𝑎𝑎𝑎𝑎 𝐵𝐵 = � � then, find: i)7 9 ii) 6 14
3 4 8 9
i) C = A+B 11 13 24 36
ii) D = A.*B iii) 0.1667 0.2857 iv) 6.0000 3.5000
iii) E=A./B
0.3750 0.4444 2.6667 2.2500
iv) F=A.\B
v) G=A-B
vi) H=A.^B
vii) I = A.^2

v) -5 -5
-5 -5

vi) 1 128
6561 262144
vii) 1 4
9 16

h) Create a vector over the range of X be over 2π ≤ X ≤ 4π, consisting of eight linearly spaced points organize
as a 2 × 4 matrix. Obtain the matrix Y consisting of the natural logarithmic value of each of the elements of
X.
x=linspace(2*pi,4*pi,8)
q=reshape(x,2,4)
y=log(q)
disp(y)
i) Let 𝑉𝑉 = [1 3 0.1 8 5 12 13 6 3.9 − 9 1.3 − 5.4 0.2 13.8]
j) Execute and observe the responses of the following commands using MATLAB and comment on the outputs:
i) Y = sum(V)
ii) Z1 = prod(V)
iii) Vmax = max(V)
iv) [maxv, indexmax] = max(V)
v) Vmin = min(V)
vi) [minv, indexmin] = min(V)
vii) Y1= Sort(V)
viii) [y, z] = sort(V)
0 1 2
ix) 𝐴𝐴 = �3 4 5� , 𝑠𝑠𝑠𝑠𝑠𝑠(𝐴𝐴)
6 7 8
1 2 3 4
0 3 −4 5
x) 𝐵𝐵 = � � , 𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠(𝐵𝐵)
4 7 8 9
−3 −5 0.7 5
xi) 𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠(𝐵𝐵(: , : )
Output Comment
i. Y = 52.9000 % Sum of all components

ii. Z1 = 7.6385e+06 % Product of all components

iii. Vmax = 13.8000 % Maximum Value in Matrix V

iv. maxv = 13.8000 indexmax = 14 % Maximum value and maximum index in V

v. Vmin = -9 % Minimum value in V

vi. minv = -9 indexmin = 10 % Minimum value and minimum index in V

vii. Y= -9.0000 -5.4000 0.1000 0.2000 6.0000 8.0000 12.0000 13.0000 13.8000
1.0000 1.3000 3.0000 3.9000 5.0000

viii. z = 10 12 3 13 1 11 2
9 5 8 4 6 7 14

ix. 9 12 15 %summation of all columns in A

x. -3.0000 -5.0000 0.7000 5.0000 %values in B sorted in numerical order in rows


0 3.0000 -4.0000 5.0000
1.0000 2.0000 3.0000 4.0000
4.0000 7.0000 8.0000 9.0000

xi. -3.0000 -5.0000 0.7000 5.0000 %values in B sorted in numerical order in columns
0 3.0000 -4.0000 5.0000
1.0000 2.0000 3.0000 4.0000
4.0000 7.0000 8.0000 9.0000
Problems
1) Evaluate and give a descriptive comment next to each instruction of the following program:
a) X = [-10:1:10]
a) % Creates a matrix X of specified
b) A = eye(length(x))
range
c) B = fix(rand(size(A))
b) % Creates an eye matrix of 21x 21
d) C = zeros(size(A)) + B
c) % Creates a zero matrix of 21 x 21
e) D = tril(ceil(rand (4)))
d) % Creates a zero matrix of 21 x 21
f) E = tril(fix(5*randn(4)))
e) % Creates a ceiling matrix of 4 x 4
g) F = diag(d).* diag(E)
f) % Creates a triangle matrix of 4 x 4
2) Create the following matrices using MATLAB:
g) % Multiplies the diagonals of D and
E and results in a 4 x1 multiplies
matrix

Determine using MATLAB commands


a) Size of A and B a) 3x3
b) 2, and 6.6613e-16
b) Rank of A and Determinant of A
c) 1 4 7; 2 5 8; 3 6 9 and trace = 15
c) Transpose of A and Trace of A
d) 1.0e+16 * -0.4504 0.9007 -0.4504 ;0.9007 -
d) Inverse of A
1.8014 0.9007;0.4504 0.9007 -0.4504
e) Maximum and minimum values of the elements in C e) Max values in c) : 9 , 15
f) Append B to A to return a 3 × 5 matrix f) Code: [A,B], Output: 3x5 Matrix
g) Create a 3 × 2 array D, consisting of all the elements in g) D=A(:,[1,2]) , Output: 3x2 Matrix
the first two columns of A

3) Execute the following MATLAB command: x = 10 * rand(1,20) and record vector x.


Write a set of MATLAB commands that return
a) The maximum value of x and the minimum value of x ANS: max(x) and min(x)
b) The sum of all the elements of x and the product of all the elements of x ANS: sum(x) and prod(A,’all’)
c) The average value of the elements of x and the median value of the elements of x ANS: mean(x,’all’) and
median(x,’all’)
d) The size of x ANS: size(x)
e) Identify and display the value of the 7th element of x ANS: x(7)
f) Rearrange the values of x in ascending and descending order ANS: sort(x) and sort(x,’descend’)

4) Enter the MATLAB command x = rand(1, 100) that returns a row vector consisting of 100 random elements.
Write a MATLAB program that adds all the elements of x with even indexes indicated by the following equation:

ANS: sum(x(1:2:end))
Lab Exercise 03 Problems
1) let y1(x) = 10 sin(x) and y2(x) = 2 cos(x) + noise(x), where noise(x) is a random function over the range 0 ≤ x ≤
3π using 100 linearly spaced points. Create a program that returns the following plots:
a) [y1 = 10 sin(x)] versus x and [y2 = 2 cos(x) + noise(x)] versus. x using the same y scale
b) [y1 = 10 sin(x)] versus x and [y2 = 2 cos(x) + noise(x)] versus x using different y scales
2) Write a program that returns the following plots in a single figure:
a) [cos(x)] versus x and [sin(x)] versus x, representing the discrete points with the markers “*” and “d,”
respectively
b) [cos(x)] versus x and [sin(x)] versus x, showing the points defined by the markers in part a, and by connecting
the markers with a solid line.
Use 20 points linearly spaced over the range 0 ≤ x ≤ 2π.
3) Generate an overlay plot for plotting three lines Use the plot command, the hold command and the line command.
Use the functions for plotting x-y data for plotting the following functions.

𝑖𝑖) 𝑓𝑓(𝑡𝑡) = 𝑡𝑡 𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐, 0 ≤ 𝑡𝑡 ≤ 10𝜋𝜋

𝑖𝑖𝑖𝑖) 𝑥𝑥 = 𝑒𝑒 𝑡𝑡 , 𝑦𝑦 = 100 + 𝑒𝑒 3𝑡𝑡 , 0 ≤ 𝑡𝑡 ≤ 2𝜋𝜋

4) Using appropriate example, make the following 2D plots

5) Create a program that returns the plots of y1(x) versus x and y2(x) versus x with the specs indicated as follows:
a) Use the marker “*” to indicate the points of y1(x) and connect the points with a solid line
b) Use the marker “+” to indicate the points of y2(x) and connect the points with a solid line
c) Label properly the x and y axes
d) Place the following text as title: 5 cos(2X) and 3 sin(X) versus X
e) Place the text 5 cos 2x at the (Cartesian coordinate) location <0.5, 4>
f) Place the text 3 sin(X) using the command gtext at the (Cartesian coordinate) location <2, 3>
g) Add an appropriate legend onto your figure.
ANS 1a):

ANS 1b):

ANS 2a):

ANS 2b):
ANS 3):

ANS 4):
ANS 5):
Lab Exercise 04
1) Write a program that returns
a) The matrix B with the information that indicates if each element of A is greater than 2
b) The matrix C with the information that indicates if each element of A is negative
c) The matrix D with the information that indicates if each element of A is equal to 2

A= [3 -3;4 2]
B= A>2
C= A<0
D= A==2
2) Write a MATLAB program that returns
a) An array C with the information indicating if the elements in A are greater than the elements in B
b) An array D with the information indicating if the elements in A are equal to the corresponding elements in B
c) An array E with the information indicating if the elements in A are smaller than the elements in B
d) An array F with the information indicating if the elements in B are positive
e) An array G with the information indicating if the elements in A are greater or equal to 5

A= -5:5
B=(-1).^A
C= A>B
D= A==B
F= B>0
G= A>=5

3) For example, let A = 1:10 and B = (−2).^A. Write and execute the MATLAB statements that return
a) An array C that indicates the locations where A > 6 and B < 3
b) An array D that indicates the locations where A > 6 or B < 3

A=1:10
B=(-2).^A
C= A>6 & B< 3
D= A>6 | B< 3

4) In the given syntax below, write down the probable output or comment corresponding to each line.

Syntax Output
>> format compact. A =0 1 2
>> A = [0 1 2; 4 5 6; -1 -2 -3] 4 5 6
-1 -2 -3
>> X = -3:10 -3 -2 -1 0 1 2 3 4
5 6 7 8 9 10
>> Y= [] []
>> String =’ABC’ 'ABC'
>> all(X) % part (a) 0
>> all(A) % part (b) 0 1 1
>> all(Y) % part (c) 1
>> C = ones(1,14)./X % part (d) -0.3333-0.5000 -1.0000 Inf
1.0000 0.5000 0.3333 0.2500
0.2000 0.1667 0.1429 0.1250
0.1111 0.1000
>> any(X) % observe that one element 1
>> isnumeric(A) % part (e) 1
>> isempty(A) % part (f) 0
>> isempty(Y) % part (g) 1
>> isinf(A) % part (h) A 3x3 zero matrix

>> isfinite(A) % part (i) A 3x3 unit matrix


>> isinf(C) % part (j) 0 0 0 1 0 0 0 0 0 0 0 0 0 0
>> find(A) % part(k) 2 3 4 5 6 7 8 9 (in column form)
>> ischar(String) % part (l) 1
ANS 1):

ANS 2):
ANS 3):

ANS 4):
ANS 5):
ANS 6):

ANS 7):
ANS 8):
ANS 9a):

ANS 9b):
Lab Exercise 05

1) Evaluate the given polynomial at x = 0; 𝑝𝑝(𝑥𝑥) = 𝜋𝜋𝑥𝑥 4 – √7𝑥𝑥 3 + 5𝑥𝑥 – 1 using MATLAB.
x = 0;
p = (pi*x^4)-(sqrt(7)*x^3)+5*x-1;
y = polyval(p,x) ANSWER: -1

2) Evaluate the following polynomials using MATLAB

s = 2; %Supposing the value of s


p1 = s^6+7*s^5+2*s^4+9*s^3+10*s^2+12*s+15 =471
p2 = s^6+9*s^5+8*s^4+9*s^3+12*s^2+15*s+20 =650
p3 = p1+p2 =1121
p4 = p1-p2 =-179
p5 = p1*p2 =306150

3) Find the partial fraction expansion for the following fraction expressed as factors in the numerator divided by
factors in the denominator using MATLAB
8(𝑠𝑠+1)(𝑠𝑠+3)
a) 𝐺𝐺1 (𝑠𝑠) =
(𝑠𝑠+2)(𝑠𝑠+4)(𝑠𝑠+6)2

b)
(a) numf=[8 32 24];
denf=poly([-2 -4 -6 -6]);
[r, p, k]= residue(numf,denf)

(b) num = [1 17 99 223 140];


den = [1 32 363 2092 5052 4320];
[K,p,k] = residue(num,den)

4) Expand the following function F(s) into partial fractions using MATLAB. Determine the inverse Laplace
transform of F(s).
% The MATLAB program for determining the partial fraction expansion
b = [0 0 0 0 1];
a = [1 5 7 0 0];
[r, p, k] = residue (b, a)

% To Find Inverse Laplace


syms s
f = 1/(s^4 + 5*s^3 + 7*s^2);
ilaplace ( f )

5) Find the Laplace transform of the following function using MATLAB.


a) 𝑓𝑓(𝑡𝑡) = 7𝑡𝑡 3 𝑐𝑐𝑐𝑐𝑐𝑐(5𝑡𝑡 + 60°)
b) 𝑓𝑓(𝑡𝑡) = 5 𝑒𝑒 –2𝑡𝑡 𝑐𝑐𝑐𝑐𝑐𝑐 5𝑡𝑡
(a)
syms t % tell MATLAB that “t” is a symbol.
f = 7 * t^3*cos(5*t + (pi/3)); % define the function.
laplace( f )
pretty(laplace(f )) % the pretty function prints symbolic output in a
format format that resembles typeset mathematics.

(b)
syms t x
f = 5*exp(-2*t)*cos(5*t);
laplace(f, x)

6) Use MATLAB and the Symbolic Math Toolbox to


a) Generate symbolically the time function f(t) shown.

b) Generate symbolically F(s) shown. Obtain your result symbolically in both factored and polynomial forms.

c) Find the Laplace transform of f(t) shown in part a.


d) Find the inverse Laplace transform of F(s) shown in part b
(a)
syms(t)
ft=.0075*t-.00034*exp(-2.5*t)*cos(22*t)+.087*exp(-2.5*t)*sin(22*t)-.0072*exp(-8*t);
c)
syms(t)
ft=.0075*t-.00034*exp(-2.5*t)*cos(22*t)+.087*exp(-2.5*t)*sin(22*t)-.0072*exp(-8*t);
FS=laplace(ft)
(b)
x=zpk([-3 -5 -7], [0 -8 -5.0000+8.6603*i -5.0000-8.6603*i],[2])
a=tf(x) % outputs factored and polynomial fraction
(d)
I=(2*(s+3)*(s+5)*(s+7))/(s*(s+8)*(s^2+10*s+100));
z=ilaplace(I)
CLASS WORK:
Lab Exercise 06
1) Use MATLAB to find the zeros and poles of the given transfer function

num=[1 2 2];
den=[1 6 4 7 2];
[z,p] = tf2zp(num,den)

2) Generate the following transfer functions using MATLAB

a) As the ratio of factors


b) As the ratio of polynomials
(1)
disp('a. The ratio of factors')

Gzpk = zpk([-9 -21 -57] , [0 -30 roots([1 5 35]) roots([1 28


42])'],3);
% zpk is used to create zero-pole-gain models or to convert TF or
% SS models to zero-pole-gain form.

disp('b. The ratio of polynomials')

Gp = tf(Gzpk) % generate the transfer function

(2)
disp('a. The ratio of factors')

Gtf = tf([1 20 27 17 35] , [1 8 9 20 29 32]) % generate the transfer


function

disp('b. The ratio of polynomials')

Gzpk = zpk(Gtf) % zpk is used to create zero-pole-gain models


%or to convert TF or SS models to zero-pole-gain form.
• OUTPUT PLOTS OF LAB 7:

Creating
Creating M-file to
produce plot
M-file for
parameter
values.

Creating M-file
to make subplot.
Exercise 1:

Exercise 2:
(a) (b)

c) The results of (a) and (b) are similar because at these small intervals, the step and impulse
response show similar patterns
Exercise 3: z=3

z=6:
z=12:

Exercise 4:
LAB 09
Open Ended Lab:

Theory Of Electrical Network Systems:


In this exercise, we formally apply the transfer function to the mathematical modeling of electric circuits
including passive networks and operational amplifier circuits. Equivalent circuits for the electric
networks that we work with first consist of three passive linear components: resistors, capacitors, and
inductors. This example summarizes the components and the relationships between voltage and current
and between voltage and charge under zero initial conditions. We now combine electrical components
into circuits, decide on the input and output, and find the transfer function. Our guiding principles are
Kirchhoff’s laws. We sum voltages around loops or sum currents at nodes, depending on which
technique involves the least.

Theory Of Translational Mechanical Systems:


Mechanical systems parallel electrical networks to such an extent that there are analogies between
electrical and mechanical components and variables. Mechanical systems, like electrical networks, have
three passive, linear components. Two of them, the spring and the mass, are energy-storage elements;
one of them, the viscous damper, dissipates energy. The two energy-storage elements are analogous to
the two electrical energy-storage elements, the inductor and capacitor. The energy dissipator is
analogous to electrical resistance.

Theory Of Rotational Mechanical Systems:


Rotational mechanical systems are handled the same way as translational mechanical systems, except
that torque replaces force and angular displacement replaces translational displacement. The mechanical
components for rotational systems are the same as those for translational systems, except that the
components undergo rotation instead of translation. The concept of degrees of freedom carries over to
rotational systems, except that we test a point of motion by rotating it while holding still all other points
of motion. The number of points of motion that can be rotated while all others are held still equals the
number of equations of motion required to describe the system. Writing the equations of motion for
rotational systems is similar to writing them for translational systems; the only difference is that the free-
body diagram consists of torques rather than forces. We obtain these torques using superposition. First,
we rotate a body while holding all other points still and place on its free-body diagram all torques due to
the body’s own motion. Then, holding the body still, we rotate adjacent points of motion one at a time
and add the torques due to the adjacent motion to the free-body diagram. The process is repeated for
each point of motion. For each free-body diagram, these torques are summed and set equal to zero to
form the equations of motion.

Q. Find an impulse and step response of the following system by using MATLAB. Use Simulink to
find both responses and compare them with MATLAB results.

We consider an Electrical network system of transfer function:


10
𝐺𝐺(𝑠𝑠) = 2
𝑠𝑠 + 2𝑠𝑠 + 20
Introduction:
We will design two systems in Simulink, one for impulse response and one for step response. Then use
the given equation to obtain the result using scope.
To find both the responses in MATLAB, write the required code and finally compare both the results (of
Simulink and MATLAB).

i. Step Response Using Simulink:

Procedure:
1. Type “Simulink” in MATLAB command prompt.
2. “Simulink Library Browser” window will appear.
3. Create a new file by selecting “model” from the fileoption(file->New->Model)
4. Drag the step function from the Simulink library browser to the new file created followed by
transfer function and scope. Scope is used to see the result(resulting waveform).
5. Give values to the transfer function according to the question.
6. After saving followed by simulation, click on the scope in the figure to see the resulting
waveform(graph).
7. A window showing the step response will appear
ii. Step Response Using MATLAB:
Procedure:
• Create a new M-file (File->New->M-file)
• Write the MATLAB code given below :

%tf() is used to find the transfer function.


• Save and then run the program. A window showing the step response will appear which will be similar
to the one obtained in Simulink as shown below.

iii. Impulse Response Using Simulink:


Now in order to find the impulse response repeat the same procedure as explained above but provide the
output of the step function as input to the derivative as shown in figure:

The result obtained in this case will be:


iv. Impulse Response In MATLAB
Procedure
• Create a new M-file(File->New->M-file)
• Write the MATLAB code given below

As derivative of step function is an impulse that’s why diff(step(t)) is used in the above code.
The figure obtained will be

Analysis:
Compare the step and impulse responses of MATLAB and Simulink.

You might also like