You are on page 1of 50

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
Simulate the matrix data
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
Looping data analysis:

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
Evaluate the grade of each student
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
Analysis of the conventional iteration
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
82

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.
84

EVALUATION

Evaluate the step of the calculation


First data 20 Step
First data 0
X = [ first : step : end] data 50 End data 120
Step data 1 First data 50 Step
End data 10 Y1 = ��2 × sin �� data 100 End data
Y2 = ��2 × sin
180
�� × cos ��
2

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
87

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
fprintf(foutput,'%30s %8.2f\n','Total Volume
function of fprint, here the syntax command : Kolam = ',vkolam);
fprintf(foutput,'%30s %8.2f\n','Volume Ember
Yang digunakan = ',vember);
fprint (fid, format, var) ; fprintf(foutput,'------------------------\n’);
fname1=input('Nama File Output = ');
foutput=fopen(fname1,'w');
% ------------------------- nember=0; % Practice program
15
% Matlab Programming while vkolam > 0
% By: Indra Komara ST, MT nember=nember+1;
% ------------------------- fprintf(foutput,'%6.0f %8.2f
%8.2f\n',nember,vkolam,vkolam-vember);
clear all; vkolam=vkolam-vember;
clc; end;
disp('--------------------------'); disp('Practice fprintf(foutput,'-----------------------\n');
Program 15'); fprintf(foutput,'%30s %8.2f\n','Jumlah Ember Yang
disp('--------------------------'); disp('Menghitung digunakan =',nember);
kebutuhan ember'); fclose(foutput);
vkolam=input(' Volume kolam (ltr) = '); vember=input(' disp(['Data Output disimpan dalam file -->' fname1]);
Volume ember (ltr) = ');

91

The Output will be illustrated


Please write the command below to
write the function by defining another below >> y=Practice16(12)
function system.
y=
3310 % By: Indra Komara ST, MT
% ------------------------
Practice 16 - %
% Fungsi y=x^3+12x^2-
15x+34 %
function y=fungsiku(x) % Cara menggunakan :
% ------------------------ % y=fungsiku(12.7)
- % Practice program 15 % ------------------------
% Matlab Programming - 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 clear all;
program 18 clc;
% Matlab Programming
% By: Indra Komara ST, MT disp('--------------------------'); disp('Practice
% --------------------------------- Program 18');
disp('--------------------------'); disp('Choice of
calculation formula'); disp('1.Square'); disp(['square area=' num2str(area)]);
disp('2.Cylinder'); disp(['square volume=' num2str(content)]);
disp(' '); case 2
disp('Calculate of area and content of cylinder');
pilih=input('pilihan anda (1-2) ->'); switch pilih disp('-----------------------------------------');
r=input('radius of cylinder=');
case 1 tg=input('height of cylinder=');
[area,content]=Practice17cylinder(r,tg);
disp('Calculate the area and content of square'); disp(['area of cylinder=' num2str(area)]);
disp('----------------------------------------'); disp(['volume of cylinder=' num2str(content)]);
pjg=input('square length='); otherwise
lbr=input('square width='); disp('cinta anda ditolak!!!')
tg=input('square height='); end;
[area,content]=Practice17beam(pjg,lbr,tg);

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
% ---------------------------------
% Practice program 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 plot(sdt,y3,'ok');
% By: Indra Komara ST, MT title('Grafic Y3')
% --------------------------------- ylabel('Nilai Y1');
xlabel('sudut dalam
clear all; derajat'); ylabel('Nilai
clc; Y1+Y2');

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);
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');
102

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
104

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');
106

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');
108

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');
110

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');
112

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');
114

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
[XI,YI]=meshgrid(linspace(xmin,xmax,50),linspace(ym
% By: Indra Komara ST, MT
in,y max,50));
% ---------------------------------
X=data(:,1);
clear all;
Y=data(:,2);
clc;
Z=data(:,3);
ZI=griddata(X,Y,Z,XI,YI,'cubic');
disp('--------------------------'); disp('Practice
Program 22'); disp('--------------------------');
disp('Pilihan Visualisasi 3D');
disp('------------------------------');
% X Y Z
disp('1. mesh');
data= [ 1.0000 1.0000 10; 50.0000 50.0000 -15;
disp('2. surf');
30.0000 25.0000 30;
disp('3. surfl');
40.0000 10.0000 -20;
disp('4. pcolor');
1.0000 50.0000 -4;
disp('5. imagesc');
50.0000 1.0000 5];
disp('6. contour');
disp('7. contourf');
xmin=min(data(:,1));
disp(' ')
xmax=min(data(:,1));
pilih=input('pilihan anda (1-7) -> ');
ymin=min(data(:,2));
ymax=min(data(:,2));
116

Practice 22 - Continued

switch pilih
case 1 case 3
mesh(ZI); surfl(ZI);
set(gca,'color',[.7 1 set(gca,'color',[.7 1
0.4],'fontsize',8); title('Grafik 3D 0.4],'fontsize',8); title('Grafik 3D
menggunakan menggunakan
MESH','fontweight','bold','fontsize',12) SURFL','fontweight','bold','fontsize',12)
xlabel('Sumbu-x','fontweight','bold'); xlabel('Sumbu-x','fontweight','bold');
ylabel('Sumbu-y','fontweight','bold'); ylabel('Sumbu-y','fontweight','bold');
zlabel('Sumbu-z','fontweight','bold'); zlabel('Sumbu-z','fontweight','bold');
case 2 case 4
surf(ZI); pcolor(ZI);
set(gca,'color',[.7 1 set(gca,'color',[.7 1
0.4],'fontsize',8); title('Grafik 3D 0.4],'fontsize',8); title('Grafik 3D
menggunakan menggunakan
SURF','fontweight','bold','fontsize',12) PCOLOR','fontweight','bold','fontsize',12)
xlabel('Sumbu-x','fontweight','bold'); xlabel('Sumbu-x','fontweight','bold');
ylabel('Sumbu-y','fontweight','bold'); ylabel('Sumbu-y','fontweight','bold');
zlabel('Sumbu-z','fontweight','bold'); zlabel('Sumbu-z','fontweight','bold');
117
clabel(cc,'fontsize',10,'color','white');
Practice 22 - Continued set(gca,'color',[0 0 0],'fontsize',8);
title('Grafik 3D menggunakan
CONTOUR','fontweight','bold','fontsize',12)
xlabel('Sumbu-x','fontweight','bold');
case 5
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);
case 7 title('Grafik 3D menggunakan
imagesc(ZI); CONTOURF','fontweight','bold','fontsize',12)
set(gca,'color',[.7 1 0.4],'fontsize',8); xlabel('Sumbu-x','fontweight','bold');
title('Grafik 3D menggunakan ylabel('Sumbu-y','fontweight','bold');
IMAGESC','fontweight','bold','fontsize',12) zlabel('Sumbu-z','fontweight','bold');
xlabel('Sumbu-x','fontweight','bold'); otherwise
ylabel('Sumbu-y','fontweight','bold'); disp ('pilihan anda ngawuur!!!')
zlabel('Sumbu-z','fontweight','bold'); case 6 end;
cc=contour(ZI);
118

WINDOW PROGRAM
PART 1 (GUI)
By : INDRA KOMARA ST., MT
Civil Engineering Department - ITATS
119

You might also like