Escolar Documentos
Profissional Documentos
Cultura Documentos
NIM : ________________
Date : ________________
1 Matlab as a Tool
A. Introduction
Matlab is suitable to be a simple calculator as well as a programing language. It is worked on
the basis of matrix.
1
C. Work Sheet
1. Basic Operation of Matrix
Execute the following operation!
>> a = [1 2 3
4 5 2
1 2 6]
>> b = inv(a)
>> c = a*b
>> d = b*a
>> e1 = a*a
>> f1 = a.*a
>> f2 = a.^2
>> g = [1 2
3 4
5 6]
>> h = a*g
>> j = g*a
What is the result? Answer:_______________________________
>> k = g*g
What is the result? Answer:_______________________________
>> l = g.^g
>> m = g.^2
>> n = g’
>> p = g*n
>> q = n*g
2
𝑥 𝑦1 = 𝑥 3 𝑦2 = 0,8𝑦1 𝑦3 = 0,5𝑥 𝑦 = 𝑦1 + 𝑦2 + 𝑦3
2
3
4
5
6
7
8
2. Variable Management
There are some commands designed to manage the variable saved on the workspace. Some
of them are:
whos : To show the list of variable saved in the workspace,
clear : To clear (delete) a variable,
clc : To clean the command window.
3. Graphic
Make a plot of 𝑦(𝑥 ) = 𝑥 3 − 2𝑥 by executing the following command!
>> 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 distance x <m>')
>> ylabel('Height y <m>')
>> title('Graphic y = x^3 - 2x')
>> grid on
To show multiple plots on a single figure, the ‘subplot’ command must be applied. Execute
the following command to understand the subplot function.
>> subplot 211
>> plot(x,y)
>> subplot 212
>> plot(x1,y1)
3
>> close all
What’s happened? Answer:__________________________________
>> subplot 121
>> plot(x,y)
>> subplot 122
>> plot(x1,y1)
1
Exercise 3 Make a plot of (𝑥 ) = 𝑥+1 ; −10 ≤ 𝑥 ≤ 10. The graph has an asymptote at
𝑥 = −1. Make the asymptote line in red and give a Latin symbol of alpha on
the y label, by writing \alpha.
Execute the following command line to make a 3D graphic and contour map of
2 2
𝑓 (𝑥, 𝑦) = 𝑒 −𝑥 −3𝑦 .
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
What is the difference between Figure(2) and Figure(3)? Answer: ____________________
__________________________________________________________________________
4. File Management
On this section some of the function related to file management will be discussed. Those
functions are current directory, data loading, and data saving and these functions are
important in terms of programming.
4
Current Directory setting
The setting of current directory can be done by executing ‘cd’ command. Make a
dummy folder named Praktikum_Matlab in drive D:\. Execute the following
command.
>> cd(‘D:\Praktikum_Matlab’)
What’s happened?
Answer:___________________________________________________
>> cd C:
>> cd D:
In the command window, type ‘cd’, (do not push Enter) then push tab button.
What’s happened?
Answer:___________________________________________________
Data Loading
Matlab can load the data or table saved on the form of .xls, .xlxs, .csv, or .txt. To
understand the data loading procedure, copy the data on Table 1.1 to an Excel file.
Named the file after ‘Latihan.xlxs’ then save it to the same folder as it is shown on
current directory. After that, execute the following command.
>> a = xlsread(‘Latihan.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
Matlab can save a/some/the whole variable on the workspace as well as the figure that
is build. The variable is saved in the form of .mat file type. Meanwhile, a figure is saved
in the form of .fig file type. Execute the following command to understand data saving in
Matlab.
>> save(‘nama_file.mat’,’x’ ,’y1’,’y2’,’y3’,’y4’)
>> plot(x,y1,x,y2,x,y3,x,y4,x,y5,’linewidth’,2)
>> saveas(gcf,’nama_file.fig’)
Data Deletion
Matlab is also capable to delete a file located on the current directory. Execute the
following command to delete a file.
>> delete(’nama_file.fig’)