Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

BAB III

LATIHAN 2
1. Definisikan vector dan matriks berikut ini dalam MATLAB:
>> V1=[10 20 30 40]

V1 =

10 20 30 40

>> V2=[-5;-15;-40]

V2 =

-5
-15
-40

>> V3=[1 3 5 0;3 1 3 5;5 3 1 3;0 5 3 1]

V3 =

1 3 5 0
3 1 3 5
5 3 1 3
0 5 3 1

2. Gabungkan matriks A dan B berikut ini:


>> A=[4 8;2 4];B=[1 1;1 -1];
>> A

A=

4 8
2 4

>> B

B=

1 1
1 -1

>> C=[A B]

C=

4 8 1 1
2 4 1 -1

>> W=[B B;B -B]

W=

1 1 1 1
1 -1 1 -1
1 1 -1 -1
1 -1 -1 1

3. Hitunglah:
a. Masing-masing ukuran vector/matriks pada soal no.1 dan no.2 diatas

>> size(V1)

ans =

1 4

>> size(V2)

ans =

3 1

>> size(V3)
ans =

4 4

>> size(A)

ans =

2 2

>> size(B)

ans =

2 2

>> size(C)

ans =

2 4

>> size(W)

ans =4 4

b. Masing-masing jumlah elemen vector/matriks pada soal no.1 dan no.2


diatas.

>> prod(size(V1))
ans =

>> prod(size(V2))

ans =

>> prod(size(V3))

ans =

16

>> prod(size(A))

ans =

>> prod(size(B))

ans =
4

>> prod(size(C))

ans =

>> prod(size(W))

ans =

16

4. Buatlah matriks-matriks berikut dengan command ones, zeros, dan eye:


M1 =

5 0 0 0
0 5 0 0
0 0 5 0
0 0 0 5

M2 =

5 5 0 0
5 5 0 0
-5 0 0 5
0 -5 5 0

>> M1*ones(4)

ans =

5 5 5 5
5 5 5 5
5 5 5 5
5 5 5 5

>> M1*zeros(4)

ans =

0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

>> M1*eye(4)

ans =

5 0 0 0
0 5 0 0
0 0 5 0
0 0 0 5

>> M2*ones(4)

ans =

10 10 10 10
10 10 10 10
0 0 0 0
0 0 0 0

>> M2*zeros(4)

ans =

0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

>> M2*eye(4)

ans =

5 5 0 0
5 5 0 0
-5 0 0 5
0 -5 5 0

5. Buatlah vector berukuran 100 berisi bilangan acak Gaussian dengan mean =
1 dan variasi 0,2
>> mean=1

mean =

>> variasi=0.2

variasi =

0.2000

>> bilangan_acak_gaussian=sqrt(variasi)*randn(1,100)+mean
bilangan_acak_gaussian =

Columns 1 through 10

0.9643 1.4018 1.0822 1.1300 1.0505 1.1968 1.0455 2.2465 0.4783 0.1707

Columns 11 through 20

0.4899 0.5110 0.8061 0.9247 0.9023 1.2421 1.1741 1.3360 1.7953 1.5470

Columns 21 through 30

0.4261 -0.0415 1.4034 0.1791 1.0299 1.0159 1.9960 0.9690 0.7731 1.1055

Columns 31 through 40

1.1099 1.0313 0.7278 0.4532 1.1415 0.3995 0.5384 1.5953 0.8127 0.9372

Columns 41 through 50

1.4024 0.8658 1.4603 0.8457 1.4529 1.2814 0.9047 0.6128 0.5335 0.8792

Columns 51 through 60

0.8041 0.8172 1.4399 0.8669 1.5115 0.7623 1.4349 0.7664 1.0790 1.4341
Columns 61 through 70

0.8149 0.8040 1.8959 1.4253 0.8068 1.2902 0.8390 1.3157 1.6332 0.2824

Columns 71 through 80

1.4601 1.6520 1.0212 1.7809 1.0695 0.4467 0.0190 0.8509 1.3191 1.1419

Columns 81 through 90

1.1850 0.7419 1.0644 0.2672 0.6601 0.6338 1.2324 0.9937 0.4832 0.9957

Columns 91 through 100

0.6915 0.7018 1.3865 1.0507 1.1782 1.3953 1.0806 1.2463 1.3054 1.5235

6. Buatlah matrik M berikut ini:


>> M=[1 5 10 15 20;1 2 4 8 16;-3 0 3 6 9;32 16 8 4 2;5 -5 5 -5 5]

M=

1 5 10 15 20
1 2 4 8 16
-3 0 3 6 9
32 16 8 4 2
5 -5 5 -5 5
>> Baris_pertama=M(1,:)

Baris_pertama =

1 5 10 15 20

>> Kolom_ketiga=M(:,3)

Kolom_ketiga =

10
4
3
8
5

>> Baris_ketiga_hingga_kelima_kolom_kedua_hingga_keempat=M(3:5,2:4)
Baris_ketiga_hingga_kelima_kolom_kedua_hingga_keempat =

0 3 6
16 8 4
-5 5 -5

7. Buatlah deret berikut ini dengan operator titik-dua, linespace, dan logspace:
>> x=-10:1:10

x=

Columns 1 through 17

-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6

Columns 18 through 21

7 8 9 10

>> y=7.5:-0.5:0

y=

Columns 1 through 10

7.5000 7.0000 6.5000 6.0000 5.5000 5.0000 4.5000 4.0000 3.5000 3.0000

Columns 11 through 16

2.5000 2.0000 1.5000 1.0000 0.5000 0

>> z=1:3:100

z=

Columns 1 through 17

1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49

Columns 18 through 34

52 55 58 61 64 67 70 73 76 79 82 85 88 91 94 97 100

>> x=linspace(-10,10,21)
x=

Columns 1 through 17

-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6

Columns 18 through 21

7 8 9 10

>> y=linspace(7.5,0,16)

y=

Columns 1 through 10

7.5000 7.0000 6.5000 6.0000 5.5000 5.0000 4.5000 4.0000 3.5000 3.0000

Columns 11 through 16

2.5000 2.0000 1.5000 1.0000 0.5000 0

>> z=linspace(1,100,34)

z=

Columns 1 through 17

1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49

Columns 18 through 34

52 55 58 61 64 67 70 73 76 79 82 85 88 91 94 97 100

>> logspace(-3,6,10)

ans =

1.0e+06 *

0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0010 0.0100 0.1000 1.0000
8. Buatlah matriks N yang berisi kolom pertama hingga keempat dari matriks
M pada no.6 diatas. Bentuk ulang matriks N terserbut menjadi matriks baru
seperti berikut ini:
>> M=[1 5 10 15 20;1 2 4 8 16;-3 0 3 6 9;32 16 8 4 2;5 -5 5 -5 5]

M=

1 5 10 15 20
1 2 4 8 16
-3 0 3 6 9
32 16 8 4 2
5 -5 5 -5 5

>> N=M(:,1:4)

N=

1 5 10 15
1 2 4 8
-3 0 3 6
32 16 8 4
5 -5 5 -5

>> fliplr(N)

ans =

15 10 5 1
8 4 2 1
6 3 0 -3
4 8 16 32
-5 5 -5 5

>> flipud(N)

ans =

5 -5 5 -5
32 16 8 4
-3 0 3 6
1 2 4 8
1 5 10 15
>> reshape(N,10,2)

ans =

1 10
1 4
-3 3
32 8
5 5
5 15
2 8
0 6
16 4
-5 -5

>> reshape(N,4,5)

ans =

1 5 16 3 8
1 5 -5 8 6
-3 2 10 5 4
32 0 4 15 -5

LATIHAN 3
1. Operasikan matriks M dan N berikut ini :
>> M=[10 20;5 8];N=[-1 1;1 -1];
>> M+N

ans =

9 21
6 7

>> M-N

ans =

11 19
4 9
>> N+9

ans =

8 10
10 8

>> M*N

ans =

10 -10
3 -3

>> N*M

ans =

-5 -12
5 12

2. Hitunglah dot-product dan cross product dari dua vector berikut :


>> a=[0 5 5];b=[1 1 1];
>> dot(a,b)

ans =

10

>> cross(a,b)

ans =

0 5 -5

>> cross(b,a)

ans =

0 -5 5

3. Pecahkanlah persamaan linier tiga variabel berikut ini:


>> x=[1 2 -3;4 5 6;7 8 9];y=[-7;11;17];
>> S=inv(x)*y

S=

1.0000
-1.0000
2.0000

4. Carilah solusi dari persamaan lingkaran berikut ini:


>> x=-5:inkremen:5;
>> clear
>> inkremen=0.05;
>> x=-5:inkremen:5;
>> y=sqrt(25-x.^2);
>> panjang=length(x);
>> titik_tengah=round(panjang/2);
>> x_baru=x(titik_tengah-1:titik_tengah+1)

x_baru =

-0.0500 0 0.0500

>> y_baru=y(titik_tengah-1:titik_tengah+1)

y_baru =

4.9997 5.0000 4.9997

5. Buatlah tabel hiperbolik-trigonometri: sinh, cosh, dan tanh untuk rentang


-5<= x<=5, dengan ikremen x sebesar 0.1
>> inkremen=0.1;
>> x=-5:inkremen:5;
>> x1=sort(x)

x1 =

Columns 1 through 10

-5.0000 -4.9000 -4.8000 -4.7000 -4.6000 -4.5000 -4.4000 -4.3000 -4.2000 -4.1000
Columns 11 through 20

-4.0000 -3.9000 -3.8000 -3.7000 -3.6000 -3.5000 -3.4000 -3.3000 -3.2000 -3.1000

Columns 21 through 30

-3.0000 -2.9000 -2.8000 -2.7000 -2.6000 -2.5000 -2.4000 -2.3000 -2.2000 -2.1000

Columns 31 through 40

-2.0000 -1.9000 -1.8000 -1.7000 -1.6000 -1.5000 -1.4000 -1.3000 -1.2000 -1.1000

Columns 41 through 50

-1.0000 -0.9000 -0.8000 -0.7000 -0.6000 -0.5000 -0.4000 -0.3000 -0.2000 -0.1000

Columns 51 through 60

0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000

Columns 61 through 70

1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000

Columns 71 through 80

2.0000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000

Columns 81 through 90

3.0000 3.1000 3.2000 3.3000 3.4000 3.5000 3.6000 3.7000 3.8000 3.9000

Columns 91 through 100

4.0000 4.1000 4.2000 4.3000 4.4000 4.5000 4.6000 4.7000 4.8000 4.9000

Column 101

5.0000

>> t=x1.*pi/180;
>> y1=sinh(t);y2=cosh(t);y3=tanh(t);
>> tabel=[x;y1;y2;y3]';
>> judul= ' x sinh cosh tanh ';
>> disp(judul),disp(tabel)
x sinh cosh tanh
-5.0000 -0.0874 1.0038 -0.0870
-4.9000 -0.0856 1.0037 -0.0853
-4.8000 -0.0839 1.0035 -0.0836
-4.7000 -0.0821 1.0034 -0.0818
-4.6000 -0.0804 1.0032 -0.0801
-4.5000 -0.0786 1.0031 -0.0784
-4.4000 -0.0769 1.0030 -0.0766
-4.3000 -0.0751 1.0028 -0.0749
-4.2000 -0.0734 1.0027 -0.0732
-4.1000 -0.0716 1.0026 -0.0714
-4.0000 -0.0699 1.0024 -0.0697
-3.9000 -0.0681 1.0023 -0.0680
-3.8000 -0.0664 1.0022 -0.0662
-3.7000 -0.0646 1.0021 -0.0645
-3.6000 -0.0629 1.0020 -0.0627
-3.5000 -0.0611 1.0019 -0.0610
-3.4000 -0.0594 1.0018 -0.0593
-3.3000 -0.0576 1.0017 -0.0575
-3.2000 -0.0559 1.0016 -0.0558
-3.1000 -0.0541 1.0015 -0.0541
-3.0000 -0.0524 1.0014 -0.0523
-2.9000 -0.0506 1.0013 -0.0506
-2.8000 -0.0489 1.0012 -0.0488
-2.7000 -0.0471 1.0011 -0.0471
-2.6000 -0.0454 1.0010 -0.0453
-2.5000 -0.0436 1.0010 -0.0436
-2.4000 -0.0419 1.0009 -0.0419
-2.3000 -0.0402 1.0008 -0.0401
-2.2000 -0.0384 1.0007 -0.0384
-2.1000 -0.0367 1.0007 -0.0366
-2.0000 -0.0349 1.0006 -0.0349
-1.9000 -0.0332 1.0005 -0.0331
-1.8000 -0.0314 1.0005 -0.0314
-1.7000 -0.0297 1.0004 -0.0297
-1.6000 -0.0279 1.0004 -0.0279
-1.5000 -0.0262 1.0003 -0.0262
-1.4000 -0.0244 1.0003 -0.0244
-1.3000 -0.0227 1.0003 -0.0227
-1.2000 -0.0209 1.0002 -0.0209
-1.1000 -0.0192 1.0002 -0.0192
-1.0000 -0.0175 1.0002 -0.0175
-0.9000 -0.0157 1.0001 -0.0157
-0.8000 -0.0140 1.0001 -0.0140
-0.7000 -0.0122 1.0001 -0.0122
-0.6000 -0.0105 1.0001 -0.0105
-0.5000 -0.0087 1.0000 -0.0087
-0.4000 -0.0070 1.0000 -0.0070
-0.3000 -0.0052 1.0000 -0.0052
-0.2000 -0.0035 1.0000 -0.0035
-0.1000 -0.0017 1.0000 -0.0017
0 0 1.0000 0
0.1000 0.0017 1.0000 0.0017
0.2000 0.0035 1.0000 0.0035
0.3000 0.0052 1.0000 0.0052
0.4000 0.0070 1.0000 0.0070
0.5000 0.0087 1.0000 0.0087
0.6000 0.0105 1.0001 0.0105
0.7000 0.0122 1.0001 0.0122
0.8000 0.0140 1.0001 0.0140
0.9000 0.0157 1.0001 0.0157
1.0000 0.0175 1.0002 0.0175
1.1000 0.0192 1.0002 0.0192
1.2000 0.0209 1.0002 0.0209
1.3000 0.0227 1.0003 0.0227
1.4000 0.0244 1.0003 0.0244
1.5000 0.0262 1.0003 0.0262
1.6000 0.0279 1.0004 0.0279
1.7000 0.0297 1.0004 0.0297
1.8000 0.0314 1.0005 0.0314
1.9000 0.0332 1.0005 0.0331
2.0000 0.0349 1.0006 0.0349
2.1000 0.0367 1.0007 0.0366
2.2000 0.0384 1.0007 0.0384
2.3000 0.0402 1.0008 0.0401
2.4000 0.0419 1.0009 0.0419
2.5000 0.0436 1.0010 0.0436
2.6000 0.0454 1.0010 0.0453
2.7000 0.0471 1.0011 0.0471
2.8000 0.0489 1.0012 0.0488
2.9000 0.0506 1.0013 0.0506
3.0000 0.0524 1.0014 0.0523
3.1000 0.0541 1.0015 0.0541
3.2000 0.0559 1.0016 0.0558
3.3000 0.0576 1.0017 0.0575
3.4000 0.0594 1.0018 0.0593
3.5000 0.0611 1.0019 0.0610
3.6000 0.0629 1.0020 0.0627
3.7000 0.0646 1.0021 0.0645
3.8000 0.0664 1.0022 0.0662
3.9000 0.0681 1.0023 0.0680
4.0000 0.0699 1.0024 0.0697
4.1000 0.0716 1.0026 0.0714
4.2000 0.0734 1.0027 0.0732
4.3000 0.0751 1.0028 0.0749
4.4000 0.0769 1.0030 0.0766
4.5000 0.0786 1.0031 0.0784
4.6000 0.0804 1.0032 0.0801
4.7000 0.0821 1.0034 0.0818
4.8000 0.0839 1.0035 0.0836
4.9000 0.0856 1.0037 0.0853
5.0000 0.0874 1.0038 0.0870

You might also like