際際滷

際際滷Share a Scribd company logo
[PERULANGAN DALAM MATLAB] March 31, 2015
Febri Arianti |Pemrograman Komputer Lanjut 1
A. PERULANGAN (LOOP)
 Perulangan for  untuk mengulangi perintah (sekelompok perintah)
secara pasti (tertentu) dimana banyaknya perulangan biasanya sudah
ditentukan terlebih dahulu.
 Perulangan while  untuk mengulangi printah (sekelompok perintah)
dimana banyaknya perulangan belum dapat diketahui.
 PERULANGAN FOR
Sintak:
Contoh:
>> for x=-3:1
y=x^2+3*x-1
z=-2*x+1
end
y =
-1
z =
7
y =
-3
z =
5
 = 3: 1   = 3, 2, 1,0,1
 = 2
+ 3  1  = 2 + 1
 = 9  9  1 = 1  = 6 + 1 = 7
 = 4  6  1 = 3  = 4 + 1 = 5
 = 1  3  1 = 3  = 2 + 1 = 3
 = 0 + 0  1 = 1  = 0 + 1 = 1
 = 1 + 3  1 = 3  = 2 + 1 = 1
[PERULANGAN DALAM MATLAB] March 31, 2015
Febri Arianti |Pemrograman Komputer Lanjut 2
y =
-3
z =
3
y =
-1
z =
1
y =
3
z =
-1
 PERULANGAN FOR (SECARA TERSARANG)
Sintak:
[PERULANGAN DALAM MATLAB] March 31, 2015
Febri Arianti |Pemrograman Komputer Lanjut 3
Contoh:
>> for x=1:3
A=2*x-3
for y=3:-1:1
B(x,y)=x^2-x*y+y^2
end
end
A =
-1
B =
0 0 7
B =
0 3 7
B =
1 3 7
A =
1
B =
1 3 7
 = 1: 3  1,2,3
A= 2  3  1,1,3
 = 3: 1: 1 dari 3 sd 1 dengan selisih 1
 = 3,2,1
 ,  = 2
 ヰ + 2
 1,2 = 1  2 + 4 = 3
 1,1 = 1  1 + 1 = 1
 1,3 = 1  3 + 9 = 7 untuk  = 1
 ,  = 2
 ヰ + 2
 2,2 = 4  4 + 4 = 4
 2,1 = 4  2 + 1 = 3
 2,3 = 4  6 + 9 = 7 untuk  = 2
 ,  = 2
 ヰ + 2
 3,2 = 9  6 + 4 = 7
 3,1 = 9  3 + 1 = 7
 3,3 = 9  9 + 9 = 9 untuk  = 3
[PERULANGAN DALAM MATLAB] March 31, 2015
Febri Arianti |Pemrograman Komputer Lanjut 4
0 0 7
B =
1 3 7
0 4 7
B =
1 3 7
3 4 7
A =
3
B =
1 3 7
3 4 7
0 0 9
B =
1 3 7
3 4 7
0 7 9
B =
[PERULANGAN DALAM MATLAB] March 31, 2015
Febri Arianti |Pemrograman Komputer Lanjut 5
1 3 7
3 4 7
7 7 9
 PERULANGAN WHILE
Sintak:
Contoh:
>> x=0;
>> while x 4
y=-x+3
x=x+2
end
y =
3
x =
2
y =
1
x =
4
  = 0 + 2 = 2
X=0 While x<4 maksudnya nilai x yang disubstitusi
harus 0 dan tidak boleh lebih dari 4 sehingga saat
disubstitusi nilai x maksimal bernilai 4. Jadi nilai x
yang mungkin hanya (0,1,2,3) akan tetapi yang
disubstitusi hanya 0 & 2.
Untuk  = 0   = 0 + 3 = 3
Untuk  = 1   = 3     = 3  1 = 2
Substitusi  = 2   = 2 + 2 = 4
Contoh lainnya:
>> x=0; berarti nilai x yang mungkin
while x<5 adalah 0,1,2,3,4, karna polanya
y=-x+3 adalah suku yang genap, maka
x=x+2 nilai x yang disubstitusi hanya 0,2
dan 4.
[PERULANGAN DALAM MATLAB] March 31, 2015
Febri Arianti |Pemrograman Komputer Lanjut 6
 STRUKTUR IF-AND
Sintak:
Contoh:
>> harga=500
harga =
500
>> harga=500;
>> pembelian=10;
>> if pembelian >7
diskon =0.25*harga;
bayar=(harga-diskon)*pembelian;
end
>> bayar
bayar =
3750
 STRUKTUR IF-ELSE-END
Sintak:
Contoh:
>> harga=500;
>> pembelian=5;
>> if pembelian > 7
disskon=0.25*harga;
bayar=(harga-diskon)*pembelian;
[PERULANGAN DALAM MATLAB] March 31, 2015
Febri Arianti |Pemrograman Komputer Lanjut 7
else
bayar=harga*pembelian;
end
>> bayar
bayar =
2500
 STRUKTUR IF-ELSE-END
Sintak:
Contoh:
>> IP=2.65;
>> if IP<1.50
SKS='maksimum 12 sks'
elseif IP<2.25 & IP>=1.50
SKS='maksimum 15 sks'
elseif IP<2.75 & IP>=2.25
SKS='maksimum 16 sks'
elseif IP<3.25 & IP>=2.75
SKS='maksimum 21 sks'
else
SKS='maksimum 24 sks'
end
SKS =
maksimum 16 sks
[PERULANGAN DALAM MATLAB] March 31, 2015
Febri Arianti |Pemrograman Komputer Lanjut 8
 FUNGSI M-FILE
Function {variabel output}=nama_fungsi(variabel input)
blok_perintah
Contoh :
Faktorial  permutasi kombinasi

More Related Content

PERULANGAN DALAM MATLAB

  • 1. [PERULANGAN DALAM MATLAB] March 31, 2015 Febri Arianti |Pemrograman Komputer Lanjut 1 A. PERULANGAN (LOOP) Perulangan for untuk mengulangi perintah (sekelompok perintah) secara pasti (tertentu) dimana banyaknya perulangan biasanya sudah ditentukan terlebih dahulu. Perulangan while untuk mengulangi printah (sekelompok perintah) dimana banyaknya perulangan belum dapat diketahui. PERULANGAN FOR Sintak: Contoh: >> for x=-3:1 y=x^2+3*x-1 z=-2*x+1 end y = -1 z = 7 y = -3 z = 5 = 3: 1 = 3, 2, 1,0,1 = 2 + 3 1 = 2 + 1 = 9 9 1 = 1 = 6 + 1 = 7 = 4 6 1 = 3 = 4 + 1 = 5 = 1 3 1 = 3 = 2 + 1 = 3 = 0 + 0 1 = 1 = 0 + 1 = 1 = 1 + 3 1 = 3 = 2 + 1 = 1
  • 2. [PERULANGAN DALAM MATLAB] March 31, 2015 Febri Arianti |Pemrograman Komputer Lanjut 2 y = -3 z = 3 y = -1 z = 1 y = 3 z = -1 PERULANGAN FOR (SECARA TERSARANG) Sintak:
  • 3. [PERULANGAN DALAM MATLAB] March 31, 2015 Febri Arianti |Pemrograman Komputer Lanjut 3 Contoh: >> for x=1:3 A=2*x-3 for y=3:-1:1 B(x,y)=x^2-x*y+y^2 end end A = -1 B = 0 0 7 B = 0 3 7 B = 1 3 7 A = 1 B = 1 3 7 = 1: 3 1,2,3 A= 2 3 1,1,3 = 3: 1: 1 dari 3 sd 1 dengan selisih 1 = 3,2,1 , = 2 ヰ + 2 1,2 = 1 2 + 4 = 3 1,1 = 1 1 + 1 = 1 1,3 = 1 3 + 9 = 7 untuk = 1 , = 2 ヰ + 2 2,2 = 4 4 + 4 = 4 2,1 = 4 2 + 1 = 3 2,3 = 4 6 + 9 = 7 untuk = 2 , = 2 ヰ + 2 3,2 = 9 6 + 4 = 7 3,1 = 9 3 + 1 = 7 3,3 = 9 9 + 9 = 9 untuk = 3
  • 4. [PERULANGAN DALAM MATLAB] March 31, 2015 Febri Arianti |Pemrograman Komputer Lanjut 4 0 0 7 B = 1 3 7 0 4 7 B = 1 3 7 3 4 7 A = 3 B = 1 3 7 3 4 7 0 0 9 B = 1 3 7 3 4 7 0 7 9 B =
  • 5. [PERULANGAN DALAM MATLAB] March 31, 2015 Febri Arianti |Pemrograman Komputer Lanjut 5 1 3 7 3 4 7 7 7 9 PERULANGAN WHILE Sintak: Contoh: >> x=0; >> while x 4 y=-x+3 x=x+2 end y = 3 x = 2 y = 1 x = 4 = 0 + 2 = 2 X=0 While x<4 maksudnya nilai x yang disubstitusi harus 0 dan tidak boleh lebih dari 4 sehingga saat disubstitusi nilai x maksimal bernilai 4. Jadi nilai x yang mungkin hanya (0,1,2,3) akan tetapi yang disubstitusi hanya 0 & 2. Untuk = 0 = 0 + 3 = 3 Untuk = 1 = 3 = 3 1 = 2 Substitusi = 2 = 2 + 2 = 4 Contoh lainnya: >> x=0; berarti nilai x yang mungkin while x<5 adalah 0,1,2,3,4, karna polanya y=-x+3 adalah suku yang genap, maka x=x+2 nilai x yang disubstitusi hanya 0,2 dan 4.
  • 6. [PERULANGAN DALAM MATLAB] March 31, 2015 Febri Arianti |Pemrograman Komputer Lanjut 6 STRUKTUR IF-AND Sintak: Contoh: >> harga=500 harga = 500 >> harga=500; >> pembelian=10; >> if pembelian >7 diskon =0.25*harga; bayar=(harga-diskon)*pembelian; end >> bayar bayar = 3750 STRUKTUR IF-ELSE-END Sintak: Contoh: >> harga=500; >> pembelian=5; >> if pembelian > 7 disskon=0.25*harga; bayar=(harga-diskon)*pembelian;
  • 7. [PERULANGAN DALAM MATLAB] March 31, 2015 Febri Arianti |Pemrograman Komputer Lanjut 7 else bayar=harga*pembelian; end >> bayar bayar = 2500 STRUKTUR IF-ELSE-END Sintak: Contoh: >> IP=2.65; >> if IP<1.50 SKS='maksimum 12 sks' elseif IP<2.25 & IP>=1.50 SKS='maksimum 15 sks' elseif IP<2.75 & IP>=2.25 SKS='maksimum 16 sks' elseif IP<3.25 & IP>=2.75 SKS='maksimum 21 sks' else SKS='maksimum 24 sks' end SKS = maksimum 16 sks
  • 8. [PERULANGAN DALAM MATLAB] March 31, 2015 Febri Arianti |Pemrograman Komputer Lanjut 8 FUNGSI M-FILE Function {variabel output}=nama_fungsi(variabel input) blok_perintah Contoh : Faktorial permutasi kombinasi