Professional Documents
Culture Documents
Csbme Lab Workbook Final
Csbme Lab Workbook Final
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
3 7
𝑥𝑥 = 12 − × 16 𝑦𝑦 = 1 −
3 30 × 8
36 − 48 − 3 7 √30𝑥𝑥8
𝑥𝑥 = 𝑦𝑦 = 1 − 𝑥𝑥
3 30 × 8 √30𝑥𝑥8
𝑥𝑥 = −5 𝑦𝑦 = 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
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 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
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
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
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.
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
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
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)
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)
(b)
syms t x
f = 5*exp(-2*t)*cos(5*t);
laplace(f, x)
b) Generate symbolically F(s) shown. Obtain your result symbolically in both factored and polynomial forms.
num=[1 2 2];
den=[1 6 4 7 2];
[z,p] = tf2zp(num,den)
(2)
disp('a. The ratio of factors')
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:
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.
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 :
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.