Вы находитесь на странице: 1из 13

Matlab as Student’s ID

Name : _______________
NIM : _______________
a Calculator Sign :

A. Introduction

Matlab (laboratory matrix) is a numerical computing environment and programming language


developed by MathWorks. Due to its ability to manipulate matrices, plot functions and data,
implement algorithms, create user interfaces, and interfacing with programs written in other
languages, Matlab is widely applied in large areas in educational and research institutions as well as
industrial worlds. In this module, students will utilize MATLAB as a calculator to solve some basic
engineering problems including:
1. matrix operations,
2. variables management,
3. plotting functions and
4. files management.

B. Matlab Environment

The environment of Matlab is depicted in Figure 1. It comprises:


1. toolbar : Matlab main menu,
2. current folder : the active folder where you save and load data or functions,
3. command window : the window to write the command,
4. workspace : the list of variables have been declared and
5. Command history : the history of executed command.

Figure 1 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

Use Matlab to fill the following table.

Table 1 Function evaluation employing Matlab


𝑥 𝑦0 = 𝑥 1 𝑦2 = 0.8𝑦0 𝑦1 = 0.5𝑥 𝑦 = 𝑦0 + 𝑦2 + 𝑦1
0
1
2
3
4
5
6
7
8

2. Variable Management

Execute the command ‘whos’. What information do you get once the command is executed?
Answer: ____________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________

Execute the command ‘clear’. What happen to the workspace?


Answer: ____________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________

Execute the command ‘clc’. What happen to the command window?


Answer: ____________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________

3. Plotting Functions

Execute the following commands to plot 𝑦(𝑥) = 𝑥 1 − 2𝑥 against 𝑥.


>> clear all
>> clc
>> x = -2:0.01:2;
>> y = x.^3-2*x;
>> plot(x,y)
>> x1 = -2:0.5:2;
>> y1 = x1.^3-2*x1;
>> plot(x,y,x1,y1,'*')
>> xlabel('Horizontal position x <m>')
>> ylabel('Height y <m>')
>> title('The Graph of y = x^3 - 2x')
>> grid on

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

What is the difference between figure#2 and figure#3?


Answer: ____________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________
____________________________________________________________________________

Exercise#5

Generate the 3–dimensional graphs of the following function


𝜋𝑦 2 tan(𝜋𝑥)
𝑓(𝑥, 𝑦) = × −1
𝑥 (𝜋𝑦) + tan2 (𝜋𝑥)
2

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)

Data Saving and Deletion

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.

~Thank you and good luck.~

7
Programming Student’s ID
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

Вам также может понравиться