Student’s ID
Programming Name : _______________
Under Matlab NIM : _______________
Sign :
Environment
A. Objectives
The objectives of this practicum are listed as follows:
1. To understand the if condition program and to implement it in several applications,
2. To understand the for looping program and to implement it in several applications,
3. To introduce an additional programming language called Simulink and
4. To understand how Matlab and Simulink communicate to each other.
B. Student’s Worksheet
1. If Condition and For Looping
Exercise#1 Determining the Roots of a Quadratic Equation
Execute the following commands to determine the roots of 𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0.
a = input('Nilai a = ');
b = input('Nilai b = ');
c = input('Nilai c = ');
det = b^2-4*a*c;
if det<0;
disp('The roots are complex numbers.')
elseif det>0;
disp('There are two distinct real roots.')
else
disp('There roots are twin real numbers.')
end
Complete the above commands to obtain the value of the roots, i.e. 𝑥0 and 𝑥2 . Draw your code
here:
Answer: ____________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
Based on the above exercise, write down the general structure of an if-condition program.
Answer: ____________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
8
Exercise#2 Checking a Matrix Dimension
Execute the following commands to check if a matrix is rectangular.
clear all;
close all;
clc
% We want to check if the matrix of A is rectangular
A = input('Matrix A = ');
[bA,kA] = size(A);
if bA > kA;
disp('A is not rectangular.')
elseif bA < kA;
disp('A is not rectangular.')
else
disp('A is recognized as a rectangular matrix.')
end
Check if the program works properly or not.
Exercise#3 Modifying Matrix Elements
Execute the following commands to modify the matrix elements.
clear all;
close all;
clc
% Here, the matrix elements are modified to be 0 when
% the value is < 3 but modified to be 1 if it is >= 3.
A = [1 2 3 4 5 6 7 8 9];
n = length(A);
for i = 1:n;
if A(i)<3;
A(i)=0;
else
A(i)=1;
end
end
Based on the above exercise, write down the general structure of a for-looping program.
Answer: ____________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
9
Exercise#4 Modifying Matrix Elements
Execute the following commands to modify the matrix elements.
clear all;
close all;
clc
% Here, the matrix elements are modified to be 0 when
% the value is < 3 but modified to be 1 if it is >= 3.
A = [1 2 3 4 5 6 7 8 9];
n = length(A);
for i = 1:n;
if A(i)<3;
A(i)=0;
else
A(i)=1;
end
end
Check if the program works properly or not.
Exercise#5 Matrix Multiplication
Execute the following commands to perform the matrix multiplication of 𝔸 × 𝔹. Firstly, check if
the size of both matrices fulfills the criterion for the multiplication by executing the following code:
clear all;
close all;
clc
disp('Multiplication AXB')
A = input('Matrix A = ');
B = input('Matrix B = ');
[bA,kA] = size(A);
[bB,kB] = size(B);
if kA == bB;
disp('Multiplication can be performed.');
else
disp(' Multiplication cannot be performed.');
end
Check if the above program works properly. Furthermore, complete the program with:
for i = 1:bA;
for j = 1:kB;
x = A(i,:);
y = B(:,j);
D(i,j)=x*y;
end
end
10
Exercise#6 Recursive Process
The following program is to resample a discrete signal of 𝑥0 (𝑡). This discrete signal is recorded
from a continuous signal of 𝑥(𝑡) = sin(2𝜋𝑡) with sampling period of 𝑇 = 0.1 ms, which means
the signal is captured once in every 0.1 ms, thus there will be 10,000 data points captured every
second. The signal of 𝑥0 (𝑡) is then resampled with lower sampling rate to get a new digital signal
of 𝑥2 (𝑡) where the two consecutive data points constructing 𝑥2 (𝑡) are taken from two data points
of 𝑥0 (𝑡) separated by another 199 consecutive data points in between.
clear all
close all
clc
t = 0:0.0001:10;
x_1 = sin(2*pi*1*t); % The original signal
n = length(x_1);
k = 1:200:n;
x_2 = x_1(k);
t_2 = t(k);
figure(1)
plot(t,x_1,'*k',t_2,x_2,'*r')
grid on
legend('x_1','x_2')
xlabel('Time (sec.)')
ylabel('Position (mm)')
figure(2)
plot(t,x_1,t_2,x_2,'*r')
grid on
legend('x_1','x_2')
xlabel('Time (sec.)')
ylabel('Position (mm)')
What is the step size of 𝑥2 (𝑡)? Step size = sampling period.
Answer: ____________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
Modify the program so that there is 1 data point of 𝑥2 (𝑡) only taken once for every 700 data points
of 𝑥0 (𝑡). What is the new step size of 𝑥2 (𝑡)?
Answer: ____________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
What will happen to 𝑥2 (𝑡) if the sampling rate is too low?
Answer: ____________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
11
Exercise#7 The Numerical Derivative of a Discrete Signal
Execute the following program to obtain the numerical derivative of 𝑥(𝑡).
clear all
close all
clc
to = input('to (s) = '); % t initial
tf = input('tf (s) = '); % t final
T = input('Step size (s) = '); % step size
t = to:T:tf; % time
x = sin(2*pi*1*t); % discrete signal x(t)
y_ideal = 2*pi*1*cos(2*pi*1*t); % analytical derivation
n = length(x);
y_num = zeros([1 n-1]); % temporary matrix 1X(n-1)
for i = 1:n-1;
y_num(i) = (x(i+1)-x(i))/T;
end
t_num = t(1:n-1);
figure(1)
subplot 211
plot(t,x)
subplot 212
plot(t,y_ideal,t_num,y_num,'r*-')
legend('Analytical','Numerical')
Are the analytical and the numerical derivatives different? If yes, how do we make the numerical
derivative getting close to the analytical one?
Answer: ____________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
Exercise#8 A Brief Introduction to Simulink
Execute the following program to declare a time series object that will be further processed using
Simulink.
clear all
close all
clc
t = 0:0.0001:10; % time
x = sin(2*pi*1*t); % discrete signal x(t)
Signal = timeseries(x,t); % creates a time series object
12
Open Simulink by executing the command of simulink in command window, then make the
following graphical program under Simulink environment. This program aims to send the signal
from Matlab to Simulink.
Run the program then do a double-click on Scope to get the plot of 𝑥(𝑡) against (𝑡). Draw the
signal for two periods here.
Then, complete the above program to generate the signal of 𝑦(𝑡) = 2 𝑥(𝑡) + 1. Execute the
program to send 𝑦(𝑡) from Simulink to Matlab.
Go back to Matlab and execute the following program to plot 𝑦(𝑡) against 𝑡. Draw the plot here
for two periods of 𝑦(𝑡).
13