EXERCISE 2
By : Dr. Indra Komara
Civil Engineering Department - ITATS
72
Fourth Meeting
Practice 01
Please calculate a simple program to count the area of rectangular shape.
The given dimension is informed as follows,
Length = 10 cm Width = 10 cm
73
Practice 02
of the angle of inclination as informed bellow:
X = [0:10:180]
Problem:
1. Calculate the sine date of Y1 =
sin
��.π 180
2. Analyses the sine of theta Y1 = cos��.π
180
74
Practice 03
Concerned to previous practice 01.
Please calculate a simple program to count the area of rectangular shape.
It is made as a simple function program.
75
Practice 04
Calculate the iteration if the simple matrix data of laboratory of
concrete. The data is considered to evaluate the behavior of modulus of elasticity.
Z = ��2 + (5 × ��)
76
Practice 05
Calculate the a volume of pool and compare to the volume of bucket. In fact, it is needed to be known how
many buckets requires to fill the whole pool if there is still a water inside.
While volume of pool > 0
the number of buckets will increase one of each iteration takes place and
the contents of the pool will
decrease as much as the contents of the bucket of each iteration are
being considered.
77
Practice 06
by the classification of point as illustrated bellows
UTS 1 = 20%
UTS 2 = 30%
UAS = 50%
If the presentation of in this term A = > 80
B = 70 – 80 C = 60 – 70 D = 50 – 60 E = < 60
78
Quiz 1
Evaluate the given matrix A = 1 5 3 which times with sin(0), -cos(150) and log(2000) × 100, respectively.
Plot the value into graph
x = value 1 : value 2 : value 3;
y = cos (x × pi / 180);
Plot (x,y)
Grid on
79
Practice 07
Evaluate several simple calculation
as follows:
1. Square area 2. Square volume 3. Cylinder area 4. Cylinder volume
80
Practice 08
and matrix manipulation.
i = 1 : 100000 a = a + 1
x (i) = a
With the modification
y (i) = �� (��)2 + 15 & y (i) = ��
(��)2 − 25
And count by using matrix manipulation;
y1 = (��1)2+15 & y2 = �� (��2)2
− 25
81
Practice 09
In this practice you have to make a recall memory to be applicated in the practice 10. First data is indicated to be a data base as follows:
- First data 0 - Step data 5 - End data 180
Practice 10
Recall data from practice 09. Illustrate the sin and cos data due to time
consideration.
83
Practice 11
Evaluate the step of the calculation - First data 0
- Step data 2 - End data 90
X = [ first : step : end]
Y1 = ��2 × sin ��
Y2 = ��2 × sin ��2 × cos ��
First, the output data should be saved and loaded for the next step.
EVALUATION
Evaluate the step of the calculation First data 0
Step data 1 End data 10
X = [ first : step : end]
Y1 = ��2 × sin ��
Y2 = ��2 × sin
��2 × cos ��
First data 20 Step
data 50 End data 120 First data 50 Step
data 100 End data 180
First, the output data should be saved and loaded for the next step.
Further, Plot the problem above and illustrate into the graph. As the information, the
graph should be drawn in one figure with several information of X.
85
Practice 12
Practice 11 follow the practice 12 to illustrate the graph
Practice 13
1. After finishing you open another new file >> edit
2. Write new matrix in the file.m, namely dataku1.txt
3. nama_file_baca='C:\Users\indra\On eDrive\Documents\MATLAB\dataku 1.txt
88
Practice 14
1. After finishing you open another new file >> edit
2. Write new matrix in the file.m, namely dataku2.txt
3. nama_file_baca='C:\Users\indra\On eDrive\Documents\MATLAB\dataku 2.txt
89
Practice 14
1. After finishing you open another new file >> edit
2. Write new matrix in the file.m, namely dataku2.txt
3. nama_file_baca='C:\Users\indra\On eDrive\Documents\MATLAB\dataku 2.txt
90
Practice 15
Write the content of format file with the
function of fprint, here the syntax command : fprint (fid, format, var) ;
fname1=input('Nama File Output = ');
foutput=fopen(fname1,'w');
fprintf(foutput,'%30s %8.2f\n','Total Volume Kolam = ',vkolam);
fprintf(foutput,'%30s %8.2f\n','Volume Ember Yang digunakan = ',vember);
fprintf(foutput,'---\n’);
15
% Matlab Programming
% By: Indra Komara ST, MT
% --- clear all;
clc;
disp('---'); disp('Practice Program 15');
disp('---'); disp('Menghitung kebutuhan ember');
vkolam=input(' Volume kolam (ltr) = '); vember=input(' Volume ember (ltr) = ');
while vkolam > 0 nember=nember+1;
fprintf(foutput,'%6.0f %8.2f
%8.2f\n',nember,vkolam,vkolam-vember);
vkolam=vkolam-vember;
end;
fprintf(foutput,'---\n');
fprintf(foutput,'%30s %8.2f\n','Jumlah Ember Yang digunakan =',nember);
fclose(foutput);
disp(['Data Output disimpan dalam file -->' fname1]);
91
Please write the command below to write the function by defining another function system.
The Output will be illustrated below >> y=Practice16(12) y =
3310
Practice 16
function y=fungsiku(x)
% --- - % Practice program 15
% Matlab Programming
% By: Indra Komara ST, MT
% --- - %
% Fungsi y=x^3+12x^2- 15x+34 %
% Cara menggunakan :
% y=fungsiku(12.7)
% --- - y=x.^3+12*x.^2-15*x+34;
92
Practice 17beam
This practice is made to support the Practice 18. Several m files are used to make the collaboration. First Practice17beam.m
function [area, content]=beam(p,l,t)
% Practice program 17
% Matlab Programming
% By: Indra Komara ST, MT
% ---
%
% Calculate the area and the content of beam
% area = 2pl + 2pt + 2lt
% content = plt
%
% How to use :
% [ area, content]=beam(length, width, height)
% --- area=2*p.*l+2*p.*t+2*l.*t;
content=p.*l.*t;
93
Practice 17cylinder
Second Practice17cylinder.m
function [area, content]=cylinder(radius,height)
% ---
% Practice program 17
% Matlab Programming
% By: Indra Komara ST, MT
% ---
%
% Calculate the area and the content of cylinder
% area = (2(pi))x r x t) + 2(pi x r^2)
% content = pi x r^2 x t;
%
% How to use :
% [area, content]=cylinder(radius,height)
% --- area=(2*pi*radius*height)+2*(pi*radius^2);
content=pi*radius^2*height;
94
Practice 18
Second Practice17cylinder.m
% --- % Practice program 18
% Matlab Programming
% By: Indra Komara ST, MT
% ---
clear all;
clc;
disp('---'); disp('Practice Program 18');
disp('---'); disp('Choice of
disp('2.Cylinder');
disp(' ');
pilih=input('pilihan anda (1-2) ->'); switch pilih case 1
disp('Calculate the area and content of square');
disp('---');
pjg=input('square length=');
lbr=input('square width=');
tg=input('square height=');
[area,content]=Practice17beam(pjg,lbr,tg);
disp(['square volume=' num2str(content)]);
case 2
disp('Calculate of area and content of cylinder');
disp('---');
r=input('radius of cylinder=');
tg=input('height of cylinder=');
[area,content]=Practice17cylinder(r,tg);
disp(['area of cylinder=' num2str(area)]);
disp(['volume of cylinder=' num2str(content)]);
otherwise
disp('cinta anda ditolak!!!') end;
95
Practice 19
Plot variations. You may plot all of the criteria
% ---
% Practice program 19
% Matlab Programming
% By: Indra Komara ST, MT
% --- clear all;
clc;
disp('---');
disp('Practice Program 19');
disp('---');
sdt=[0:10:180];
y=sin(sdt*pi/180);
plot(sdt,y,'*r');
title('Grafik Sinus');
xlabel('Sudut dalam derajat');
ylabel('Nilai sinus');
96
Practice 19a
% ---
% Matlab Programming
% By: Indra Komara ST, MT
% --- clear all;
clc;
disp('---');
disp('Practice Program 19a');
disp('---');
sdt=[0:10:180];
y=sin(sdt*pi/180);
plot(sdt,y,'oy');
hold on;
plot(sdt,y,'-w');
grid on;
set(gca,'color',[0.5 0.5 0.4],'fontname','arial');
title('grafik sinus','color',[0 0 1],'fontweight','bold','fontsize',16);
xlabel('Sudut dalam derajat','color',[.5 .4
1],'fontweight','bold','fontsize',9); ylabel('Nilai sinus','color',[1 0 .5],'fontweight','bold','fontsize',9);
97
Practice 19b
% --- -
% Practice program 19b
% Matlab Programming
% By: Indra Komara ST, MT
% --- -
clear all;
clc;
disp('--- ');
disp('Practice Program 19b');
disp('--- ');
sdt=[0:10:180];
y1=sin(sdt*pi/180);
y2=sin(sdt*pi/180);
plot(sdt,y1,'*r',sdt,y2,'+b') title('Grafic sinus & cosinus') xlabel('sudut dalam derajat');
ylabel('Nilai Y');
98
Practice 19c
% --- --
% Practice program 19c
% Matlab Programming
% By: Indra Komara ST, MT
% --- --
clear all;
clc;
disp('--- ');
disp('Practice Program 19c');
disp('--- ');
sdt=[0:10:180];
y1=sin(sdt*pi/180);
y2=cos(sdt*pi/180);
plot(sdt,y1,'*r');
hold on;
plot(sdt,y2,'+b');
title('Grafic sinus & cosinus') xlabel('sudut dalam derajat');
ylabel('Nilai Y');
99
Practice 20
% ---
% Practice program 20
% Matlab Programming
% By: Indra Komara ST, MT
% --- clear all;
clc;
disp('--- '); disp('Practice Program 20');
disp('--- ');
sdt=[0:10:180];
y1=sin(sdt*pi/180);
y2=cos(sdt*pi/180);
y3=y1+y2;
subplot(3,1,1);
plot(sdt,y1,'*b');
title('Grafic sinus') ylabel('Nilai Y1');
subplot(3,1,2);
plot(sdt,y2,'+r');
title('Grafic cosinus') ylabel('Nilai Y2');
subplot(3,1,3);
plot(sdt,y3,'ok');
title('Grafic Y3') ylabel('Nilai Y1');
xlabel('sudut dalam
derajat'); ylabel('Nilai Y1+Y2');
100
Visualization of 2D data
Visualisasi 2D (2 dimensi) artinya adalah menempatkan satu atau lebih titik data pada media bidang kartesian maupun polar. Memvisualisasikan data pada bidang pemodelan numerik, terkadang tidak cukup hanya dengan plotting garis atau titik saja. Dalam banyak kesempatan, misalnya presentasi ilmiah, penelitian dan sebagainya, plot garis atau titik dirasakan kurang memadai.
Sampai saat practice sebelumnya kita sudah terbiasa dengan penggunaan fungsi plot. Tapi sebenernya MATLAB menyediakan beberapa fungsi visualisasi yang umum digunakan, yaitu:
Fungsi bar : bar (x,y);
Fungsi stem : stem (x,y);
Fungsi stairs : stairs (x,y);
101
Practice 20a
Bar Function
% ---
% Practice program 20a
% Matlab Programming
% By: Indra Komara ST, MT
% --- clear all;
clc;
disp('---');
disp('Practice Program 20a');
disp('---');
sdt=[0:5:160];
y=sin(sdt*pi/180);
bar(sdt,y)
title('grafik Sinus');
xlabel('Sudut dalam derajat');
ylabel('Nilai sudut');
Practice 20b
Stem Function
% --- -
% Practice program 20b
% Matlab Programming
% By: Indra Komara ST, MT
% --- -
clear all;
clc;
disp('--- ');
disp('Practice Program 20b');
disp('---');
x=[-50:2.5:50];
y1=(x.^2).*sin(x);
stem(x,y1);
103
Practice 20c
Stairs Function
% ---
% Practice program 20c
% Matlab Programming
% By: Indra Komara ST, MT
% --- clear all;
clc;
disp('---');
disp('Practice Program 20c');
disp('---');
x=[-50:5:150];
y1=(x.^2).*cos(x);
stairs(x,y1);
Illustrate another plot by using >> demo
Visualization of 3D data
Visualisasi 3D (3 dimensi) artinya adalah menempatkan satu atau lebih titik data pada media ruang. Teknik visualisasi 3D ini sama berguna dalam bidang permodelan yang berhubungan dengan ruang (x, y, z), dimana z berarti nilai bobot pada titik (x,y) yang berupa data ketinggian, temperature, kecepatan dan lain-lain. Beberapa fungsi yang umum digunakan untuk visualisasi data 3D adalah:
Fungsi plot3 Fungsi mesh Fungsi Surfl Fungsi pcolor Fungsi imagesc Fungsi contour Fungsi contour
105
Practice 21
Plot3 Function
% making data of x y and z
% x y z
t = [5 6 12;
2 3 4;
4 2 9;
4 7 9;
11 10 20;
9 7 7;
5 6 12];
plot3(t(:,1),t(:,2),t(:,3),'ob');
hold on;
plot3(t(:,1),t(:,2),t(:,3),'-r');
grid on;
set (gca,'fontsize',8);
title('Grafik 3D
plot3','fontweight','bold','fontsize',12);
xlabel('Sumbu-x','fontweight','bold');
ylabel('Sumbu-y','fontweight','bold');
zlabel('Sumbu-z','fontweight','bold');
Practice 21a
Mesh function
% making data of x y and z
% x y z
t = [5 6 12;
2 3 4;
4 2 9;
4 7 9;
11 10 20;
9 7 7;
5 6 12];
mesh(t);
hold on;
mesh(t);
grid on;
set (gca,'color',[.7 1 0.4],'fontsize',8);
title('Grafik 3D
plot3','fontweight','bold','fontsize',12);
xlabel('Sumbu-x','fontweight','bold');
ylabel('Sumbu-y','fontweight','bold');
zlabel('Sumbu-z','fontweight','bold');
107
Practice 21a
Mesh function
% making data of x y and z
% x y z
t = [5 6 12;
2 3 4;
4 2 9;
4 7 9;
11 10 20;
9 7 7;
5 6 12];
mesh(t);
hold on;
mesh(t);
grid on;
set (gca,'color',[.7 1 0.4],'fontsize',8);
title('Grafik 3D
plot3','fontweight','bold','fontsize',12);
xlabel('Sumbu-x','fontweight','bold');
ylabel('Sumbu-y','fontweight','bold');
zlabel('Sumbu-z','fontweight','bold');
Practice 21b
Surf function
% making data of x y and z
% x y z
t = [5 6 12;
2 3 4;
4 2 9;
4 7 9;
11 10 20;
9 7 7;
5 6 12];
surf(t);
hold on;
surf(t);
grid on;
set (gca,'color',[.7 1 0.4],'fontsize',8);
title('Grafik 3D
plot3','fontweight','bold','fontsize',12);
xlabel('Sumbu-x','fontweight','bold');
ylabel('Sumbu-y','fontweight','bold');
zlabel('Sumbu-z','fontweight','bold');
109
Practice 21c
Surfl function
% making data of x y and z
% x y z
t = [5 6 12;
2 3 4;
4 2 9;
4 7 9;
11 10 20;
9 7 7;
5 6 12];
surfl(t);
hold on;
surfl(t);
grid on;
set (gca,'color',[.7 1 0.4],'fontsize',8);
title('Grafik 3D
plot3','fontweight','bold','fontsize',12);
xlabel('Sumbu-x','fontweight','bold');
ylabel('Sumbu-y','fontweight','bold');
zlabel('Sumbu-z','fontweight','bold');
Practice 21d
Pcolor function
% making data of x y and z
% x y z
t = [5 6 12;
2 3 4;
4 2 9;
4 7 9;
11 10 20;
9 7 7;
5 6 12];
pcolor(t);
hold on;
pcolor(t);
grid on;
set (gca,'color',[.7 1 0.4],'fontsize',8);
title('Grafik 3D
plot3','fontweight','bold','fontsize',12);
xlabel('Sumbu-x','fontweight','bold');
ylabel('Sumbu-y','fontweight','bold');
zlabel('Sumbu-z','fontweight','bold');
111
Practice 21e
Imagesc function
% making data of x y and z
% x y z
t = [5 6 12;
2 3 4;
4 2 9;
4 7 9;
11 10 20;
9 7 7;
5 6 12];
imagesc(t);
hold on;
imagesc(t);
grid on;
set (gca,'color',[.7 1 0.4],'fontsize',8);
title('Grafik 3D
plot3','fontweight','bold','fontsize',12);
xlabel('Sumbu-x','fontweight','bold');
ylabel('Sumbu-y','fontweight','bold');
zlabel('Sumbu-z','fontweight','bold');
Practice 21f
Contourf function
% making data of x y and z
% x y z
t = [5 6 12;
2 3 4;
4 2 9;
4 7 9;
11 10 20;
9 7 7;
5 6 12];
contourf(t);
hold on;
contourf(t);
grid on;
set (gca,'color',[.7 1 0.4],'fontsize',8);
title('Grafik 3D
plot3','fontweight','bold','fontsize',12);
xlabel('Sumbu-x','fontweight','bold');
ylabel('Sumbu-y','fontweight','bold');
zlabel('Sumbu-z','fontweight','bold');
113
Practice 21g
Contour function
% making data of x y and z
% x y z
t = [5 6 12;
2 3 4;
4 2 9;
4 7 9;
11 10 20;
9 7 7;
5 6 12];
contour(t);
hold on;
contour(t);
grid on;
set (gca,'color',[.7 1 0.4],'fontsize',8);
title('Grafik 3D
plot3','fontweight','bold','fontsize',12);
xlabel('Sumbu-x','fontweight','bold');
ylabel('Sumbu-y','fontweight','bold');
zlabel('Sumbu-z','fontweight','bold');
Practice 22
Contour function
% making data of x y and z
% x y z
t = [5 6 12;
2 3 4;
4 2 9;
4 7 9;
11 10 20;
9 7 7;
5 6 12];
contour(t);
hold on;
contour(t);
grid on;
set (gca,'color',[.7 1 0.4],'fontsize',8);
title('Grafik 3D
plot3','fontweight','bold','fontsize',12);
xlabel('Sumbu-x','fontweight','bold');
ylabel('Sumbu-y','fontweight','bold');
zlabel('Sumbu-z','fontweight','bold');
115
Practice 22
% ---
% Practice program 22
% Matlab Programming
% By: Indra Komara ST, MT
% --- clear all;
clc;
disp('---'); disp('Practice Program 22'); disp('---');
% X Y Z
data= [ 1.0000 1.0000 10; 50.0000 50.0000 -15;
30.0000 25.0000 30;
40.0000 10.0000 -20;
1.0000 50.0000 -4;
50.0000 1.0000 5];
xmin=min(data(:,1));
xmax=min(data(:,1));
ymin=min(data(:,2));
ymax=min(data(:,2));
[XI,YI]=meshgrid(linspace(xmin,xmax,50),linspace(ym in,y max,50));
X=data(:,1);
Y=data(:,2);
Z=data(:,3);
ZI=griddata(X,Y,Z,XI,YI,'cubic');
disp('Pilihan Visualisasi 3D');
disp('---');
disp('1. mesh');
disp('2. surf');
disp('3. surfl');
disp('4. pcolor');
disp('5. imagesc');
disp('6. contour');
disp('7. contourf');
disp(' ')
pilih=input('pilihan anda (1-7) -> ');
Practice 22 - Continued
switch pilih case 1
mesh(ZI);
set(gca,'color',[.7 1
0.4],'fontsize',8); title('Grafik 3D menggunakan
MESH','fontweight','bold','fontsize',12) xlabel('Sumbu-x','fontweight','bold');
ylabel('Sumbu-y','fontweight','bold');
zlabel('Sumbu-z','fontweight','bold');
case 2
surf(ZI);
set(gca,'color',[.7 1
0.4],'fontsize',8); title('Grafik 3D menggunakan
SURF','fontweight','bold','fontsize',12) xlabel('Sumbu-x','fontweight','bold');
ylabel('Sumbu-y','fontweight','bold');
zlabel('Sumbu-z','fontweight','bold');
case 3
surfl(ZI);
set(gca,'color',[.7 1
0.4],'fontsize',8); title('Grafik 3D menggunakan
SURFL','fontweight','bold','fontsize',12) xlabel('Sumbu-x','fontweight','bold');
ylabel('Sumbu-y','fontweight','bold');
zlabel('Sumbu-z','fontweight','bold');
case 4
pcolor(ZI);
set(gca,'color',[.7 1
0.4],'fontsize',8); title('Grafik 3D menggunakan
PCOLOR','fontweight','bold','fontsize',12) xlabel('Sumbu-x','fontweight','bold');
ylabel('Sumbu-y','fontweight','bold');
zlabel('Sumbu-z','fontweight','bold');
117
Practice 22 - Continued
case 5
case 7
imagesc(ZI);
set(gca,'color',[.7 1 0.4],'fontsize',8);
title('Grafik 3D menggunakan
IMAGESC','fontweight','bold','fontsize',12) xlabel('Sumbu-x','fontweight','bold');
ylabel('Sumbu-y','fontweight','bold');
zlabel('Sumbu-z','fontweight','bold'); case 6 cc=contour(ZI);
clabel(cc,'fontsize',10,'color','white');
set(gca,'color',[0 0 0],'fontsize',8);
title('Grafik 3D menggunakan
CONTOUR','fontweight','bold','fontsize',12) xlabel('Sumbu-x','fontweight','bold');
ylabel('Sumbu-y','fontweight','bold');
zlabel('Sumbu-z','fontweight','bold');
cc=contourf(ZI);
clabel(cc,'fontsize',10,'color','white');
set(gca,'color',[0 0 0],'fontsize',8);
title('Grafik 3D menggunakan
CONTOURF','fontweight','bold','fontsize',12) xlabel('Sumbu-x','fontweight','bold');
ylabel('Sumbu-y','fontweight','bold');
zlabel('Sumbu-z','fontweight','bold');
otherwise
disp ('pilihan anda ngawuur!!!') end;
WINDOW PROGRAM PART 1 (GUI)
By : INDRA KOMARA ST., MT
Civil Engineering Department - ITATS
119