Академический Документы
Профессиональный Документы
Культура Документы
Name : _______________
NIM : _______________
a Calculator Sign :
A. Introduction
B. Matlab Environment
1
C. Student’s Worksheet
1. Matrix Operations
Execute the following commands.
>> a = [1 2 3
4 5 2
1 2 6]
>> b = inv(a)
Answer: ____________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
>> c = a*b
>> d = b*a
>> e1 = a*a
>> f1 = a.*a
What is the difference between e1 and f1?
Answer: ____________________________________________________________________
____________________________________________________________________________
>> f2 = a.^2
>> g = [1 2
3 4
5 6]
>> h = a*g
>> j = g*a
Does the operation of j = g*a work properly? Why?
Answer: ____________________________________________________________________
____________________________________________________________________________
>> k = g*g
Does the operation of k = g*g work properly? Why?
Answer: ____________________________________________________________________
____________________________________________________________________________
>> l = g.^g
>> m = g.^2
>> n = g’
>> p = g*n
>> q = n*g
>> log(10)
>> log10(10)
>> sin(30)
>> sind(30)
>> atand(-1/1)
>> atand(1/-1)
>> atan2d(-1,1)
>> atan2d(1,-1)
Exercise#1
Find 𝕩 for 𝔸𝕩 = 𝔹, where:
16 2 3 13 - 28
𝔸 = %& 5 11
9 7
10
6
8 () and 𝔹 = %-118) , Ans: 𝕩 =
12 - 38
4 14 15 11 -212
2
Exercise#2
2. Variable Management
Execute the command ‘whos’. What information do you get once the command is executed?
Answer: ____________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
3. Plotting Functions
3
Sketch the plot of 𝑦(𝑥) = 𝑥 1 − 2𝑥 against 𝑥 here:
Graphs can be organized in subplots. Execute the following programs to understand subplot.
>> subplot 211
>> plot(x,y)
>> subplot 212
>> plot(x1,y1)
What do 211 and 212 stand for?
Answer: ________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
>> close all
What happen to the plot once the command of close all is executed?
Answer: ________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
>> subplot 121
>> plot(x,y)
>> subplot 122
>> plot(x1,y1)
What do 121 and 122 stand for?
Answer: ________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
4
Exercise#3
0
Plot the function of 𝛼(𝑥) = =>0 against 𝑥 in the range of −10 ≤ 𝑥 ≤ 10. Indicate the vertical
asymptote of 𝑥 = −1 in your figure. To create the symbol of 𝛼, type \alpha inside the ylabel
command.
Exercise#4
Execute the following commands to generate the 3–dimensional graphs and the contour of (𝑥, 𝑦) =
D D
𝑒 C= C1E .
clear all
close all
clc
[x,y] = meshgrid(-2:0.1:2,-2:0.1:2);
f = exp(-x.^2-3*y.^2);
figure(1)
surf(x,y,f)
figure(2)
[C,h] = contour(x,y,f);
clabel(C,h);
grid on
figure(3)
v = 0:0.2:1;
[C,h] = contour(x,y,f,v);
clabel(C,h);
grid on
D C1E D
Draw the contour of 𝑓(𝑥, 𝑦) = 𝑒 C= here.
5
Figure#2 Figure#3
Exercise#5
in the range of 0 ≤ (𝑥, 𝑦) ≤ 0,5. Furthermore, create the contour of 𝑓(𝑥, 𝑦) for 𝑓(𝑥, 𝑦) =
−1, −0.9, −0.8, … , −0.1, 0. Draw the contour of 𝑓(𝑥, 𝑦) here.
6
4. Files Management
Files management is absolutely required when the computation process involves many files located
in different directories. In this section, students will organize the files through Matlab command
execution.
Changing the Current Directory
The current directory can be changed either manually by clicking the folder shown in File Explorer,
or automatically by executing commands in the command window. To understand the command,
follow these instructions: (1) make a folder entitled Praktikum1 in the D directory. (2) execute the
following commands:
>> cd(‘D:\Praktikum_Matlab’)
What happen to the current directory?
Answer: ________________________________________________________________
_______________________________________________________________________
>> cd C:
>> cd D:
In command window, type cd (do not hit the Enter button), then hit Tab button. What does
Matlab provide?
Answer: ________________________________________________________________
_______________________________________________________________________
Data Loading
Data loading is sometimes crucial since the data obtained from measurements (e.g. voltage, current,
temperature, vibration, etc.) are saved in form of Excel or txt files. To understand how Matlab
loads the data, follow these instructions: (1) copy the data shown in Table 1 to a new Excel file,
(2) save the file in the current directory and name it as Exercise_A, (3) execute the following
commands:
>> a = xlsread(‘Exercise_A.xls’);
>> x = a(:,1);
>> y1 = a(:,2);
>> y2 = a(:,3);
>> y3 = a(:,4);
>> y4 = a(:,5);
>> plot(x,y1,x,y2,x,y3,x,y4,x,y5)
Last, execute the following commands to save and delete the data.
>> save(‘nama_file.mat’,’x’ ,’y1’,’y2’,’y3’,’y4’)
>> plot(x,y1,x,y2,x,y3,x,y4,x,y5,’linewidth’,2)
>> saveas(gcf,’filename.fig’)
Check if the data has been successfully saved.
>> delete(’ filename.fig’)
Check if the data has been successfully deleted.
7
Programming Student’s ID
Name : _______________
Under Matlab NIM : _______________
Sign :
Environment
A. Objectives
B. Student’s Worksheet
Based on the above exercise, write down the general structure of an if-condition program.
Answer: ____________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
8
Exercise#2 Checking a Matrix Dimension
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
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
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
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)')
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: ____________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
11
Exercise#7 The Numerical Derivative of a Discrete Signal
clear all
close all
clc
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: ____________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
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