Anda di halaman 1dari 26

5.

1 Metode Trapezoida

Integral terhadap suatu fungsi, f(x), yang dievaluasi dari a hingga b dapat dinyatakan oleh rumus
berikut ini

(5.1)
Pendekatan numerik yang paling dasar dalam memecahkan masalah integral adalah metode
Trapezoida, yang dirumuskan sebagai berikut

(5.2)

dimana x0 = a, x1 = b dan h = b − a. Akan tetapi, suku yang terakhir pada ruas kanan dimana terdapat
faktor turunan ke-2, f′′, seringkali diabaikan dengan tujuan agar persamaan (5.2) menjadi lebih
sederhana.

(5.3)

Akibatnya pendekatan Trapezoida hanya bekerja efektif pada fungsi-fungsi yang turunan keduanya
bernilai nol (f′′ = 0). Gambar (5.1) memperlihatkan prinsip metode trapezoida dalam bentuk grafik.
Sementara, script berikut ini dibuat berdasarkan persamaan (5.3).

63

f(x)
f(x1) f(x)

f(x0)

x0=a x1=b x0=a x1=b

Gambar 5.1: Metode Trapezoida. Gambar sebelah kiri menunjukkan kurva fungsi f(x) dengan batas bawah
integral adalah a dan batas atas b. Gambar sebelah kanan menunjukan cara metode Trapesoida menghitung
integral dengan cara menghitung luas area integrasi, dimana luas area integrasi sama dengan luas trapesium di
bawah kurva f(x) dalam batas-batas a dan b. Jika anda perhatikan dengan teliti, ada area kecil dibawah garis
kurva dan diatas garis miring yang berada diluar bidang trapesium. Metode Trapesoida tidak menghitung luas
area kecil tersebut. Disinilah letak kelemahan metode trapezoida.

1 clear all
2 clc
3

4 a = ... %batas bawah integral;


5 b = ... %batas atas integral;
6

7 x0 = a;
8 x1 = b;
9 h = b-a;
10

11 % -- metode trapezoida --
12 Int_trapezoida = h/2*(f(x0)+f(x1))

Dengan fungsi eksternal fungsi f(x) adalah

1 function y = f(x)
2 y = ... % rumus fungsi yang di-integralkan;

5.2 Metode Simpson

Metode pendekatan yang lebih baik dibanding metode Trapezoida dalam integral numerik adalah
metode Simpson yang diformulasikan sebagai berikut

(5.4)

dengan x1 = a, x3 = b, dan x2 = a + h dimana h = (b − a)/2. Jika suku terakhir diabaikan, maka

(5.5)
Gambar (5.2) memperlihatkan prinsip metode trapezoida dalam bentuk grafik. Sementara, script
berikut ini dibuat berdasarkan persamaan (5.5).
5.2. METODESIMPSON

f(x)
f(x)2 f(x)
f(x)1

f(x)0
h
h

x0=a x1=b x=a


0 x1 x=b
2

Gambar 5.2: Metode Simpson. Gambar sebelah kiri menunjukkan kurva fungsi f(x) dengan batas bawah
integral adalah a dan batas atas b. Gambar sebelah kanan menunjukan cara metode Simpson menghitung luas
area integrasi, dimana area integrasi di bawah kurva f(x) dibagi 2 dalam batas interval a − x1 dan x1 − b dengan
lebar masing-masing adalah h

1 clc
2 clear all
3

4 a = ... %batas bawah integrasi ;


5 b = ... %batas atas integrasi ;
6

7 x1 = a;
8 x3 = b;
9 h = (b-a)/2;
10 x1 = a + h;
11

12 % -- metode simpson --
13 Int_simpson = h/3*(f(x1)+4*f(x2)+f(x3))

Contoh
Metode Trapezoida untuk fungsi f pada interval [0,2] adalah

dimana x0 = 0, x1 = 2 dan h = 2 − 0 = 2. Sedangkan metode Simpson untuk fungsi f pada interval [0,2]

adalah

dengan x0 = 0, x2 = 2, dan x1 = a + h = 1 dimana h = (b − a)/2 = 1.

Tabel berikut ini memperlihatkan evaluasi integral numerik terhadap beberapa fungsi dalam interval
[0,2] beserta solusi exact-nya. Jelas terlihat, metode Simpson lebih baik dibanding Trapezoida. Karena
hasil intergral numerik metode Simpson lebih mendekati nilai exact

f (x ) x2 x4 1/(x +1) 1+ x 2 sin x ex
Nilai exact 2,667 6,400 1,099 2,958 1,416 6,389
4
Trapezoida 4,000 16,000 1,333 3,236 0,909 8,389 Simpson 2,667 6,667 1,111
2,964 1,425 6,421

5.3 Peran faktor pembagi, n

Kalau diamati lebih teliti, akan kita dapatkan bahwa interval [0,2] telah dibagi 2 pada metode
Simpson, sementara pada metode Trapesoida tidak dibagi sama sekali. Sebenarnya dengan membagi
interval lebih kecil lagi, maka error-nya akan semakin kecil. Misalnya, banyaknya pembagian interval
dinyatakan dengan n ketika n = 1: Trapesioda

(5.6)

ketika n = 2: Simpson

(5.7)

ketika n = 3: Simpson tiga-per-delapan

(5.8)

ketika n = 4:

(5.9)

5.3.1 Source code metode integrasi

Source code untuk persamaan (5.8) disajikan sebagai berikut

1 clc
2 clear all
3

4 % -- batas integrasi --
5 a = 0;
6 b = 2;
7

8 x0 = a;
9 x3 = b;
10 h = (b-a)/3;
11 x1 = a + h;
12 x2 = a + 2*h;
13 % ---------------------
14

15 % -- metode simpson 3/8 --


16 Int_38 = 3*h/8*(f(x0)+3*f(x1)+3*f(x2)+f(x3))

Sementara, source code untuk persamaan (5.9) disajikan sebagai berikut

1 clc
2 clear all
5
3

5.4. METODECOMPOSITE-SIMPSON

4 % -- batas integrasi --
5 a = 0;
6 b = 2;
7

8 x0 = a;
9 x4 = b;
10 h = (b-a)/4;
11 x1 = a + h;
12 x2 = a + 2*h;
13 x3 = a + 3*h;
14 % ---------------------
15

16 % -- metode simpson n=4 --


17 Int_n4 = 2*h/45*(7*f(x0)+32*f(x1)+12*f(x2)+32*f(x3)+7*f(x4))

Perbandingan tingkat akurasi hasil perhitungan seluruh metode integral numerik yang sudah
dibahas adalah sebagai berikut

f (x ) x2 x4 1/(x +1) 1+ x 2 sin x ex
Nilai exact 2,667 6,400 1,099 2,958 1,416 6,389
Trapezoida 4,000 16,000 1,333 3,236 0,909 8,389
Simpson n=2 2,667 6,667 1,111 2,964 1,425 6,421
Simpson n=3 2,667 6,519 1,105 2,960 1,420 6,403
Simpson n=4 2,667 6,400 1,099 2,958 1,416 6,389

Keempat bentuk persamaan integral numerik di atas dikenal dengan closed Newton-Cotes
formulas. Keterbatasan metode Newton-Cotes terlihat dari jumlah pembagian interval. Di atas tadi
pembagian interval baru sampai pada n = 4. Bagaimana bila interval evaluasinya dipersempit supaya
solusi numeriknya lebih mendekati solusi exact? Atau dengan kata lain n > 4.

5.4 Metode Composite-Simpson

Persamaan (5.9) terlihat lebih rumit dibandingkan persamaan-persamaan sebelumnya. Bisakah anda
bayangkan bentuk formulasi untuk n = 5 atau n = 6 dan seterusnya? Pasti akan lebih kompleks
dibandingkan persamaan (5.9).
Metode Composite Simpson menawarkan cara mudah menghitung intergal numerik ketika nilai n
> 4. Perhatikan contoh berikut, tentukan solusi numerik dari . Metode Simpson dengan h = 2
(atau interval evaluasi integral dibagi 2 , n = 2) memberikan hasil
6
4 0
Padahal solusi exact dari integral tersebut adalah e − e = 53,59815, artinya terdapat error sebesar

3,17143 yang dinilai masih terlampau besar untuk ditolerir. Bandingkan dengan metode

f(x)

x0=a x1 x2 x3 x4 x5 x6 x7 xn=b

Gambar 5.3: Metode Composite Simpson. Kurva fungsi f(x) dengan batas bawah integral adalah a dan batas

atas b. Luas area integrasi dipecah menjadi 8 area kecil dengan lebar masing-masing adalah h. yang sama

namun dengan h = 1 (atau interval evaluasi integral dibagi 4 , n = 4)

Hasil ini memperlihatkan error yang makin kecil, yaitu menjadi 0,26570. Jadi dengan memperkecil h,
error menjadi semakin kecil dan itu artinya solusi integral numerik semakin mendekati solusi exact.
Sekarang kita coba kecilkan lagi nilai h menjadi (atau interval evaluasi integral dibagi 8 , n = 8),

dan seperti yang sudah kita duga, error-nya semakin kecil menjadi 0,01807.

Prosedur ini dapat digeneralisir menjadi suatu formula sebagai berikut


7

(5.10)

5.5. ADAPTIVEQUARDRATURE

dimana h = (b−a)/n dan xj = a+jh, untuk j = 1,...,n/2, dengan x0 = a dan xn = b. Formula ini dapat

direduksi menjadi

Formula ini dikenal sebagai metode Composite Simpson.

5.5 Adaptive Quardrature

Metode composite mensyaratkan luas area integrasi dibagi menjadi sejumlah region dengan jarak
interval yang seragam yaitu sebesar nilai h. Akibatnya, bila metode composite diterapkan pada fungsi
yang memiliki variasi yang tinggi dan rendah sekaligus, maka interval h yang kecil menjadi kurang
efektif, sementara interval h yang besar mengundang error yang besar pula. Metode Adaptive
Quadrature muncul untuk mendapatkan langkah yang paling efektif dimana nilai interval h tidak
dibuat seragam, melainkan mampu beradaptasi sesuai dengan tingkat variasi kurva fungsinya.

Misalnya kita bermaksud mencari solusi numerik dari integral dengan toleransi ǫ > 0.
Sebagai langkah awal adalah menerapkan metode Simpson dimana step size h = (b−a)/2

(5.12)

dengan

Langkah berikutnya adalah men

(5.13)

5.6 Gaussian Quadrature

Suatu integral dapat ditransformasi kedalam bentuk Gaussian quadrature melalui formulasi berikut
8
(5.14)

dimana perubahan variabel memenuhi

(5.15)
Berikut adalah table polinomial Legendre untuk penyelesaian Gaussian quadrature
Tabel 5.1: Polinomial Legendre untuk n=2,3,4 dan 5
n Akar rn,i Koefisien cn,i
2 0,5773502692 1,0000000000
-0,5773502692 1,0000000000

3 0,7745966692 0,5555555556
0,0000000000 0,8888888889

-0,7745966692 0,5555555556

4 0,8611363116 0,3478548451
0,3399810436 0,6521451549

-0,3399810436 0,6521451549

-0,8611363116 0,3478548451

5 0,9061798459 0,2369268850
0,5384693101 0,4786286705

0,0000000000 0,5688888889

-0,5384693101 0,4786286705

-0,9061798459 0,2369268850

5.6.1 Contoh

Selesaikan integrasi berikut ini

(5.16)
(Solusi exact integral diatas adalah: 0.1093643) jawab:

Pertama, integral tersebut ditransformasikan kedalam Gaussian quadrature melalui persamaan


(5.14)

(5.17)
Kedua, Gaussian quadrature dihitung menggunakan konstanta-konstanta yang tercantum pada tabel
polinomial Legendre. Untuk n = 2

Untuk n = 3
9
Z 1,5 −x2
e dx

≈ 1[(0,5555555556)
e(−(0,7745966692+5)2/16) + (0,8888888889)e(−(5)2/16)
1 4
+ (0,5555555556)e(−(−0,7745966692+5)2/16)] = 0,1093642

6.1Metode Euler
Suatu persamaan diferensial ( ) dinyatakan dalam fungsi f(t,y), dimana y(t) adalah persamaan
asalnya

(6.1)
Nilai t dibatasi dari a hingga ke b. Sementara, syarat awal telah diketahui yaitu pada saat t = a maka y
bernilai α. Akan tetapi kita sama sekali tidak tahu bentuk formulasi persamaan asalnya y(t). Gambar
6.1 memperlihatkan kurva persamaan asal y(t) yang tidak diketahui bentuk formulasinya.
Tantangannya adalah bagaimana kita bisa mendapatkan solusi persamaan diferensial untuk setiap
nilai y(t) yang t-nya terletak diantara a dan b ?
Tahap awal solusi pendekatan numerik adalah dengan menentukan point-point dalam jarak yang
sama di dalam interval [a,b]. Jarak antar point dirumuskan sebagai

(6.2)

dengan N adalah bilangan integer positif. Nilai h ini juga dikenal dengan nama stepsize. Selanjutnya
nilai t diantara a dan b ditentukan berdasarkan

ti = a + ih, i = 0,1,2,...,N (6.3)

73
y y

y(t)=
N y()b y(t) y’=f(t,y) y(t)
y(a)= a
y(t)2 y’=f(t,y)
y(a)= a
y(t)1 w1 y’(a)=f(a, )a
y(t)=
0
a a
h h

t0= a t1 t2 ..... tN=b t t0= a t1 t2 ..... tN =b t

Gambar 6.1: Kiri: Kurva y(t) dengan pasangan titik absis dan ordinat dimana jarak titik absis sebesar h.
Pasangan t1 adalah y(t1), pasangan t2 adalah y(t2), begitu seterusnya. Kanan: Garis singgung yang menyinggung
kurva y(t) pada t=a, kemudian berdasarkan garis singgung tersebut, ditentukan pasangan t1 sebagai w1.
Perhatikan gambar itu sekali lagi! w1 dan y(t1) beda tipis alias tidak sama persis.
10
Metode Euler diturunkan dari deret Taylor. Misalnya, fungsi y(t) adalah fungsi yang kontinyu dan
memiliki turunan dalam interval [a,b]. Dalam deret Taylor, fungsi y(t) tersebut dirumuskan sebagai

(6.4)
dengan memasukkan h = (ti+1 − ti), maka

(6.5)

dan, karena y(t) memenuhi persamaan diferensial (6.1), dimana y′(ti) tak lain adalah fungsi turunan
f(ti,y(ti)), maka

(6.6)

Metode Euler dibangun dengan pendekatan bahwa suku terakhir dari persamaan (6.6), yang
memuat turunan kedua, dapat diabaikan. Disamping itu, pada umumnya, notasi penulisan bagi y(ti)
diganti dengan wi. Sehingga metode Euler diformulasikan sebagai

wi+1 = wi + hf(ti,wi) dengan syarat awal w0 = α (6.7)

dimana i = 0,1,2,..,N − 1.

Contoh
Diketahui persamaan diferensial

y ′ = y − t2 + 1 batas interval: 0≤t≤2 syarat awal: y(0) = 0,5 (6.8)

dimana N = 10. Disini terlihat bahwa batas awal interval, a = 0; dan batas akhir b = 2.
Dalam penerapan metode euler, pertama kali yang harus dilakukan adalah menghitung step-

6.1. METODEEULER

size (h), caranya

kemudian dilanjutkan dengan menentukan posisi titik-titik ti berdasarkan rumus

ti = a + ih = 0 + i(0,2) sehingga ti = 0,2i

serta menetapkan nilai w0 yang diambil dari syarat awal y(0) = 0,5

w0 = 0,5

Dengan demikian persamaan euler dapat dinyatakan sebagai


wi+1 = wi + h(wi − t2i + 1)
11
2
= wi + 0,2(wi − 0,04i + 1)

= 1,2wi − 0,008i2 + 0,2


dimana i = 0,1,2,...,N − 1. Karena N = 10, maka i = 0,1,2,...,9.

Pada saat i = 0 dan dari syarat awal diketahui w0 = 0,5, kita bisa menghitung w1

w1 = 1,2w0 − 0,008(0)2 + 0,2 = 0,8000000

Pada saat i = 1 w2 = 1,2w1 − 0,008(1)2 + 0,2 = 1,1520000

Pada saat i = 2 w3 = 1,2w2 − 0,008(2)2 + 0,2 = 1,5504000

Demikian seterusnya, hingga mencapai i = 9

w10 = 1,2w9 − 0,008(9)2 + 0,2 = 4,8657845

Berikut ini adalah script matlab untuk menghitung w1, w2, sampai w10

1 clear all
2 clc
3

4 format long
5

6 b=2; %batas akhir interval


7 a=0; %batas awal interval
8 N=10; % bilangan interger positif
9 h=(b-a)/N; % nilai step-size
10 w0=0.5; % nilai w awal
11 t0=0; % nilai t awal
12

13 % perubahan t sesuai step-size h adalah:


t1=a+1
14 *h;
t2=a+2
15 *h;
t3=a+3
16 *h;
t4=a+4
17 *h;
t5=a+5
18 *h;
t6=a+6
19 *h;
t7=a+7
20 *h;
t8=a+8
21 *h;
t9=a+9
22 *h;
23 t10=a+10*h;
24

25 % solusinya:
26 w1=w0+h*(w0-t0^2+1)
27 w2=w1+h*(w1-t1^2+1)
28 w3=w2+h*(w2-t2^2+1)
29 w4=w3+h*(w3-t3^2+1)
30 w5=w4+h*(w4-t4^2+1)
31 w6=w5+h*(w5-t5^2+1)
12
32 w7=w6+h*(w6-t6^2+1)
33 w8=w7+h*(w7-t7^2+1)
34 w9=w8+h*(w8-t8^2+1)
35 w10=w9+h*(w9-t9^2+1)

Atau bisa dipersingkat sebagai berikut

1 clear all
2 clc
3

4 format long
5

6 b=2; %batas akhir interval


7 a=0; %batas awal interval
8 N=10; % bilangan interger positif
9 h=(b-a)/N; % nilai step-size
10 w0=0.5; % nilai w awal
11 t0=0; % nilai t awal
12

13 % perubahan t sesuai step-size h adalah:


14 for i=1:N
15 t(i)=a+(i*h);
16 end
17

18 % solusinya:
19 w(1)=w0+h*(w0-t0^2+1);
20 for i=2:N 21 k=i-1;
22 w(i)=w(k)+h*(w(k)-t(k)^2+1);
23 end
24 w

Disisi lain, solusi exact persamaan diferensial (6.8) adalah

y(t) = (t + 1)2 − 0,5et (6.9)

Script matlab untuk mendapatkan solusi exact ini adalah:

1 clear all
2 clc
6.1. METODEEULER

4 format long
5

6 b=2; %batas akhir interval


7 a=0; %batas awal interval
8 N=10; % bilangan interger positif
9 h=(b-a)/N; % nilai step-size
10

11 % perubahan t sesuai step-size h adalah:


12 for i=1:N
13 t(i)=a+(i*h);
14 end
13
15

16 % solusi exact:
17 for i=1:N
18 y(i)=(t(i)+1)^2-0.5*exp(t(i));
19 end
20 y

Tabel 6.1: Solusi yang ditawarkan oleh metode euler wi dan solusi exact y(ti) serta selisih antara
keduanya
i ti wi yi = y(ti) |wi − yi|
0 0,0 0,5000000 0,5000000 0,0000000
1 0,2 0,8000000 0,8292986 0,0292986
2 0,4 1,1520000 1,2140877 0,0620877
3 0,6 1,5504000 1,6489406 0,0985406
4 0,8 1,9884800 2,1272295 0,1387495
5 1,0 2,4581760 2,6408591 0,1826831
6 1,2 2,9498112 3,1799415 0,2301303
7 1,4 3,4517734 3,7324000 0,2806266
8 1,6 3,9501281 4,2834838 0,3333557
9 1,8 4,4281538 4,8151763 0,3870225
10 2,0 4,8657845 5,3054720 0,4396874
Coba anda perhatikan sejenak bagian kolom selisih |wi − yi|. Terlihat angkanya tumbuh semakin besar
seiring dengan bertambahnya ti. Artinya, ketika ti membesar, akurasi metode euler justru berkurang.
Untuk lebih jelasnya, mari kita plot hasil-hasil ini dalam suatu gambar.
Gambar (6.2) memperlihatkan sebaran titik-titik merah yang merupakan hasil perhitungan
metode euler (wi). Sementara solusi exact y(ti) diwakili oleh titik-titik biru. Tampak jelas bahwa titik-
titik biru dan titik-titik merah –pada nilai t yang sama– tidak ada yang berhimpit alias ada jarak yang
memisahkan mereka. Bahkan semakin ke kanan, jarak itu semakin melebar. Adanya jarak, tak lain
menunjukkan keberadaan error (kesalahan). Hasil perhitungan metode euler yang diwakili oleh titik-
titik merah ternyata menghadirkan tingkat kesalahan yang semakin membesar ketika menuju ke- N
atau ketika ti bertambah. Untuk mengatasi hal ini, salah satu pemecahannya adalah dengan
menerapkan metode Runge-Kutta orde-4. Namun sebelum masuk ke pembahasan tersebut, ada
baiknya kita memodifikasi script matlab yang terakhir tadi.
Saya kira tidak ada salahnya untuk mengantisipasi kesalahan pengetikan fungsi turunan yang
terdapat dalam script sebelumnya yaitu,
14
5.5

4.5

3.5
y(t)

2.5

1.5

0.5
0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
t

Gambar 6.2: Kurva biru adalah solusi exact, dimana lingkaran-lingkaran kecil warna biru pada kurva

menunjukkan posisi pasangan absis t dan ordinat y(t) yang dihitung oleh Persamaan (6.9). Sedangkan titik-titik

merah mengacu pada hasil perhitungan metode euler, yaitu nilai wi. w(1)=w0+h*(w0-t0^2+1); dan

w(i)=w(k)+h*(w(k)-t(k)^2+1);

Ketika fungsi turunan memiliki formulasi yang berbeda dengan contoh di atas, bisa jadi kita akan
lupa untuk mengetikkan formulasi yang baru di kedua baris tersebut. Oleh karena itu, lebih baik
fungsi turunan tersebut dipindahkan kedalam satu file terpisah. Di lingkungan matlab, file tersebut
disebut file function. Jadi, isi file function untuk contoh yang sedang kita bahas ini adalah
function y = futur(t,w)
y = w - t^2 + 1;

File function ini mesti di-save dengan nama file yang sama persis dengan nama fungsinya, dalam
contoh ini nama file function tersebut harus bernama futur.m. Kemudian file ini harus disimpan
dalam folder yang sama dimana disana juga terdapat file untuk memproses metode euler. Setelah
itu, script metode euler dimodifikasi menjadi seperti ini

1 clear all
2 clc
3

4 format long
5

6 b=2; %batas akhir interval


15
7 a=0; %batas awal interval
8 N=10; % bilangan interger positif
9 h=(b-a)/N; % nilai step-size
10 w0=0.5; % nilai w awal
11 t0=0; % nilai t awal
12

13 % perubahan t sesuai step-size h adalah:


14 for i=1:N
15 t(i)=a+(i*h);
16 end
17

18 % solusinya:
19 w(1)=w0+h*futur(t0,w0);
20 for i=2:N
21 k=i-1;
22 w(i)=w(k)+h*futur(t(k),w(k));
23 end
24 w

Mulai dari baris ke-13 sampai dengan baris ke-24, tidak perlu diubah-ubah lagi. Artinya, jika ada perubahan
formulasi fungsi turunan, maka itu cukup dilakukan pada file futur.m saja.
Ok. Sekarang mari kita membahas metode Runge Kutta.

6.2 Metode Runge Kutta

Pada saat membahas metode Euler untuk penyelesaian persamaan diferensial, kita telah sampai pada
kesimpulan bahwa truncationerror metode Euler terus membesar seiring dengan bertambahnya iterasi ( ti).
Dikaitkan dengan hal tersebut, metode Runge-Kutta Orde-4 menawarkan penyelesaian persamaan diferensial
dengan pertumbuhan truncationerror yang jauh lebih kecil. Persamaan-persamaan yang menyusun metode
Runge-Kutta Orde-4 adalah
w0 = α
k1 = hf(ti,wi) (6.10)

k2 = (6.11)

k3 = (6.12)
k4 = hf(ti+1,wi + k3) (6.13)
wi+1 = (6.14)
dimana fungsi f(t,w) adalah fungsi turunan.
Contoh
Saya ambilkan contoh yang sama seperti contoh yang sudah kita bahas pada metode Euler. Diketahui persamaan
diferensial

y′ = y − t2 + 1, 0 ≤ t ≤ 2, y(0) = 0,5
Jika N = 10, maka step-size bisa dihitung terlebih dahulu

dan
16
ti = a + ih = 0 + i(0,2) → ti = 0,2i

serta
w0 = 0,5

Sekarang mari kita terapkan metode Runge-Kutta Orde-4 ini. Untuk menghitung w1, tahaptahap perhitungannya
dimulai dari menghitung k1
k1 = hf(t0,w0)
=

= 0,2((0,5) − (0,0)2 + 1)
= 0,3

lalu menghitung k2
h k
k2 =

=
= 0,328

dilanjutkan dengan k3

k3 =
=
0, 328 0, 2 2
= 0, 2[(0, 5+ ) − (0, 0+ ) +1)]
2 2
= 0,3308

kemudian k4
k4 = hf(t1,w0 + k3)

= h[(w0 + k3) − t21 + 1]

= 0,2[(0,5 + 0,3308) − (0,2)2 + 1]

= 0,35816
17
akhirnya diperoleh w1

1
w1 = w0 + (k1 + 2k2 + 2k3 + k4)
6
1
= 0,5 + (0,3 + 2(0,328) + 2(0,3308) + 0,35816)
6
1
= 0,5 + (0,3 + 0,656 + 0,6616 + 0,35816)
6
= 0,8292933

Dengan cara yang sama, w2,w3,w4 dan seterusnya dapat dihitung dengan program komputer.
Script matlab-nya sebagai berikut1:

1 clear all
2 clc
3

4 format long
5

6 b=2; % batas akhir interval


7 a=0; % batas awal interval
8 N=10; % bilangan interger positif
9 h=(b-a)/N; % nilai step-size
10 w0=0.5; % nilai w awal
11 t0=0; % nilai t awal
12

13 % perubahan t sesuai step-size h adalah:


14 for i=1:N
15 t(i)=a+(i*h);
16 end
17

18 % solusinya:
19 k1=h*futur(t0,w0);
20 k2=h*futur(t0+h/2,w0+k1/2);
21 k3=h*futur(t0+h/2,w0+k2/2);
22 k4=h*futur(t(1),w0+k3);
23 w(1)=w0+1/6*(k1+2*k2+2*k3+k4);
24

25 for i=2:N
26 k=i-1;
27 k1=h*futur(t(k),w(k));
28 k2=h*futur(t(k)+h/2,w(k)+k1/2);
29 k3=h*futur(t(k)+h/2,w(k)+k2/2);
30 k4=h*futur(t(i),w(k)+k3);
31 w(i)=w(k)+1/6*(k1+2*k2+2*k3+k4);
32 end
33 w

Dibandingkan dengan metode Euler, tingkat pertumbuhan truncationerror, pada kolom |wi−yi| (lihat Tabel 6.2),
jauh lebih rendah sehingga metode Runge-Kutta Orde Empat lebih disukai untuk membantu menyelesaikan
persamaan-diferensial-biasa.

1 Jangan lupa, file futur.m mesti berada dalam satu folder dengan file Runge Kutta nya!
18
Contoh tadi tampaknya dapat memberikan gambaran yang jelas bahwa metode Runge-Kutta
Orde Empat dapat menyelesaikan persamaan diferensial biasa dengan tingkat akurasi yang lebih Tabel 6.2:
Solusi yang ditawarkan oleh metode Runge Kutta orde 4 (wi) dan solusi exact y(ti)
serta selisih antara keduanya
i ti wi yi = y(ti) wi yi
| − |
0 0,0 0,5000000 0,5000000 0,0000000
1 0,2 0,8292933 0,8292986 0,0000053
2 0,4 1,2140762 1,2140877 0,0000114
3 0,6 1,6489220 1,6489406 0,0000186
4 0,8 2,1272027 2,1272295 0,0000269
5 1,0 2,6408227 2,6408591 0,0000364
6 1,2 3,1798942 3,1799415 0,0000474
7 1,4 3,7323401 3,7324000 0,0000599
8 1,6 4,2834095 4,2834838 0,0000743
9 1 ,8 4,8150857 4,8151763 0,0000906
10 2,0 5,3053630 5,3054720 0,0001089

5.5

4.5

3.5
y(t)

2.5

1.5

0.5
0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
t

Vectors and matrices

We have already seen how to define a vector and assign a variable name to it. Often it is useful to define vectors
(and matrices) that contain equally spaced entries. This can be done by specifying the first entry, an increment, and
the last entry. MATLAB will automatically figure out how many entries you need and their values. For example, to
create a vector whose entries are 0, 1, 2, 3, …, 7, 8, you can type

» u = [0:8]
u =
0 1 2 3 4 5 6 7 8
19
Here we specified the first entry 0 and the last entry 8, separated by a colon ( : ). MATLAB automatically filled-in the
(omitted) entries using the (default) increment 1. You could also specify an increment as is done in the next
example.

To obtain a vector whose entries are 0, 2, 4, 6, and 8, you can type in the following line:

» v = [0:2:8]
v =
0 2 4 6 8

Here we specified the first entry 0, the increment value 2, and the last entry 8. The two colons ( : ) “tell” MATLAB to
fill in the (omitted) entries using the specified increment value.

MATLAB will allow you to look at specific parts of the vector. If you want, for example, to only look at the first 3
entries in the vector v, you can use the same notation you used to create the vector:

» v(1:3) ans =
0 2 4

Note that we used parentheses, instead of brackets, to refer to the entries of the vector. Since we omitted the
increment value, MATLAB automatically assumes that the increment is 1. The following command lists the first 4
entries of the vector v, using the increment value 2 :

» v(1:2:4)
ans =

0 4

Defining a matrix is similar to defining a vector. To define a matrix A, you can treat it like a column of row vectors.
That is, you enter each row of the matrix as a row vector (remember to separate the entries either by commas or
spaces) and you separate the rows by semicolons ( ; ).

» A = [1 2 3; 3 4 5; 6 7 8]

A =
1 2 3
3 4 5
6 7 8

We can avoid separating each row with a semicolon if we use a carriage return instead. In other words, we could
have defined A as follows

» A = [
1 2 3
3 4 5
6 7 8]

A =
1 2 3
3 4 5
6 7 8

which is perhaps closer to the way we would have defined A by hand using the linear algebra notation.
20

You can refer to a particular entry in a matrix by using parentheses. For example, the number 5 lies in the 2 nd row,
3rd column of A, thus

» A(2,3) ans =
5
The order of rows and columns follows the convention adopted in the linear algebra notation. This means that
A(2,3) refers to the number 5 in the above example and A(3,2) refers to the number 7, which is in the 3 rd row,
2nd column.

Note MATLAB’s response when we ask for the entry in the 4 th row, 1st column.

» A(4,1)
??? Index exceeds matrix dimensions.

As expected, we get an error message. Since A is a 3-by-3 matrix, there is no 4 th row and MATLAB realizes that. The
error messages that we get from MATLAB can be quite informative when trying to find out what went wrong. In
this case MATLAB told us exactly what the problem was.

We can “extract” submatrices using a similar notation as above. For example to obtain the submatrix that consists
of the first two rows and last two columns of A we type

» A(1:2,2:3)
ans = 2 3
4 5

We could even extract an entire row or column of a matrix, using the colon ( : ) as follows.
Suppose we want to get the 2 nd column of A. We basically want the elements [A(1,2) A(2,2) A(3,2)]. We
type

» A(:,2) ans =
2
4
7

where the colon was used to tell MATLAB that all the rows are to be used. The same can be done when we want to
extract an entire row, say the 3rd one.
Built-in functions

There are numerous built-in functions (i.e. commands) in MATLAB. We will mention a few of them in this
section by separating them into categories.

Scalar Functions

Certain MATLAB functions are essentially used on scalars, but operate element-wise when applied to a
matrix (or vector). They are summarized in the table below.

sin trigonometric sine


cos trigonometric cosine
tan trigonometric tangent
asin trigonometric inverse sine (arcsine)
21
acos trigonometric inverse cosine (arccosine)
atan trigonometric inverse tangent
(arctangent)
exp exponential
log natural logarithm
abs absolute value
sqrt square root
rem remainder
round round towards nearest integer
floor round towards negative infinity
ceil round towards positive infinity

Even though we will illustrate some of the above commands in what follows, it is strongly recommended
to get help on all of them to find out exactly how they are used.

The trigonometric functions take as input radians. Since MATLAB uses pi for the number π = 3.1415…

» sin(pi/2)
ans = 1

» cos(pi/2)
ans =
6.1230e-017

The sine of π/2 is indeed 1 but we expected the cosine of π/2 to be 0. Well, remember that MATLAB is a
numerical package and the answer we got (in scientific notation) is very close to
0 ( 6.1230e-017 = 6.1230×10 –17 ≈ 0).

Since the exp and log commands are straight forward to use, let us illustrate some of the other
commands. The rem command gives the remainder of a division. So the remainder of 12 divided by 4 is
zero

» rem(12,4)
ans = 0

and the remainder of 12 divided by 5 is 2.

» rem(12,5)
ans = 2

The floor, ceil and round commands are illustrated below.

» floor(1.4)
ans = 1

» ceil(1.4)
ans = 2
» round(1.4)
ans =
1
22

Vector Functions

Other MATLAB functions operate essentially on vectors returning a scalar value. Some of these functions
are given in the table below.

max largest component


min smallest component length length of a
vector sort sort in ascending order sum sum of
elements prod product of elements median
median value mean mean value std
standard deviation

Once again, it is strongly suggested to get help on all the above commands. Some are illustrated below.

Let z be the following row vector.

» z = [0.9347,0.3835,0.5194,0.8310]
z =
0.9347 0.3835 0.5194 0.8310

Then

» max(z) ans =
0.9347

» min(z) ans =
0.3835

» sort(z) ans =
0.3835 0.5194 0.8310 0.9347

» sum(z) ans =
2.6686

» mean(z) ans =
0.6671

The above (vector) commands can also be applied to a matrix. In this case, they act in a columnby-column
fashion to produce a row vector containing the results of their application to each column. The example
below illustrates the use of the above (vector) commands on matrices.

Suppose we wanted to find the maximum element in the following matrix.

» M = [
0.7012,0.2625,0.3282
0.9103,0.0475,0.6326
0.7622,0.7361,0.7564];

If we used the max command on M, we will get the row in which the maximum element lies
(remember the vector functions act on matrices in a column-by-column fashion).
23

» max(M) ans =
0.9103 0.7361 0.7564

To isolate the largest element, we must use the max command on the above row vector. Taking
advantage of the fact that MATLAB assigns the variable name ans to the answer we obtained, we can
simply type

» max(ans) ans =
0.9103

The two steps above can be combined into one in the following.

» max(max(M))
ans = 0.9103

Combining MATLAB commands can be very useful when programming complex algorithms where we do
not wish to see or access intermediate results. More on this, and other programming features of MATLAB
in Section 3 ahead.

Matrix Functions

Much of MATLAB’s power comes from its matrix functions. These can be further separated into two sub-
categories. The first one consists of convenient matrix building functions, some of which are given in the
table below.

eye identity matrix zeros matrix of


zeros ones matrix of ones
diag extract diagonal of a matrix or create diagonal matrices triu upper
triangular part of a matrix tril lower triangular part of a matrix rand
randomly generated matrix
Make sure you ask for help on
all the above commands.

To create the identity matrix of size 4 (i.e. a square 4-by-4 matrix with ones on the main diagonal and
zeros everywhere else) we use the command eye.

» eye(4,4)
ans =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

The numbers in parenthesis indicates the size of the matrix. When creating square matrices, we can
specify only one input referring to size of the matrix. For example, we could have obtained the above
identity matrix by simply typing eye(4). The same is true for the matrix building functions below.

Similarly, the command zeros creates a matrix of zeros and the command ones creates a matrix of ones.
24
» zeros(2,3)
ans =
0 0 0
1 0 0

» ones(2) ans =
1 1
2 1

We can create a randomly generated matrix using the rand command. (The entries will be uniformly
distributed between 0 and 1.)

» C = rand(5,4)

C =
0.2190 0.3835 0.5297 0.4175
0.0470 0.5194 0.6711 0.6868
0.6789 0.8310 0.0077 0.5890
0.6793 0.0346 0.3834 0.9304
0.9347 0.0535 0.0668 0.8462

The commands triu and tril, extract the upper and lower part of a matrix, respectively. Let us try
them on the matrix C defined above.

» triu(C) ans =

0.2190 0.3835 0.5297 0.4175


0 0.5194 0.6711 0.6868
0 0 0.0077 0.5890
0 0 0 0.9304
0 0 0 0

» tril(C) ans =
0.2190 0 0 0
0.0470 0.5194 0 0
0.6789 0.8310 0.0077 0
0.6793 0.0346 0.3834 0.9304
0.9347 0.0535 0.0668 0.8462

Once the extraction took place, the “empty” positions in the new matrices are automatically filled with
zeros.

As mentioned earlier, the command diag has two uses. The first use is to extract a diagonal of a matrix,
e.g. the main diagonal. Suppose D is the matrix given below. Then, diag(D) produces a column vector,
whose components are the elements of D that lie on its main diagonal.

» D = [
0.9092 0.5045 0.9866
0.0606 0.5163 0.4940
0.9047,0.3190,0.2661];

» diag(D) ans =
0.9092
25
0.5163
0.2661

The second use is to create diagonal matrices. For example,

» diag([0.9092;0.5163;0.2661])
ans =
0.9092 0 0
0 0.5163 0
0 0 0.2661

creates a diagonal matrix whose non-zero entries are specified by the vector given as input. (A short cut
to the above construction is diag(diag(D)) ).
This command is not restricted to the main diagonal of a matrix; it works on off diagonals as well. See
help diag for more information.

Let us now summarize some of the commands in the second sub-category of matrix functions.

size size of a matrix det determinant of a


square matrix inv inverse of a matrix rank rank
of a matrix rref reduced row echelon form eig
eigenvalues and eigenvectors poly
characteristic polynomial
norm norm of matrix (1-norm, 2-norm, ∞ -norm) cond condition
number in the 2-norm
lu LU factorization qr QR factorization chol
Cholesky decomposition svd singular value
decomposition

Don’t forget to get help on the above commands. To illustrate a few of them, define the following
matrix.

» A = [9,7,0;0,8,6;7,1,-6]

A =
9 7 0
0 8 6
7 1 -6

» size(A) ans =
3 3

» det(A) ans =
-192
Since the determinant is not zero, the matrix is invertible.

» inv(A) ans =
0.2812 -0.2187 -0.2187
-0.2187 0.2812 0.2812
0.2917 -0.2083 -0.3750
26

We can check our result by verifying that AA–1 = I and A–1A = I .

» A*inv(A) ans =
1.0000 0.0000 0.0000
0.0000 1.0000 0.0000
0.0000 0.0000 1.0000

» inv(A)*A ans =
1.0000 0.0000 0
0.0000 1.0000 0
0.0000 0 1.0000

Let us comment on why MATLAB uses both 0’s and 0.0000’s in the answer above. Recall that we are
dealing with a numerical package that uses numerical algorithms to perform the operations we ask for.
Hence, the use of floating point (vs. exact) arithmetic causes the “discrepancy” in the results. From a
practical point of view, 0 and 0.0000 are the same.

The eigenvalues and eigenvectors of A (i.e. the numbers λ and vectors x that satisfy Ax = λx ) can
be obtained through the eig command.

» eig(A) ans =
12.6462
3.1594 -4.8055

produces a column vector with the eigenvalues and

» [X,D]=eig(A)

X =
-0.8351 -0.6821 0.2103
-0.4350 0.5691 -0.4148
-0.3368 -0.4592 0.8853

D =
12.6462 0 0
0 3.1594 0
0 0 -4.8055

produces a diagonal matrix D with the eigenvalues on the main diagonal, and a full matrix X whose
columns are the corresponding eigenvectors.

Anda mungkin juga menyukai