Runge-Kutta metodu Runge-Kutta Metodu dy dx = f(x, y), y(0) = y 0 (1) bicimindeki birinci dereceden adi diferansiyel denklemleri numerik olarak cozmekte kullanilan bir metottur. Runge-Kutta metodunu kullanabilmek icin oncelikle verilen bir birinci dereceden adi diferansiyel denklemi yukaridaki formatta yazmak gerekir. Ornegin dy dx - y2 = sin(x) olarak verilen bir diferansiyel denklem dy dx = y2 + sin(x) bicimine donusturulur. Burada f(x, y) = y 2 + sin(x) oldugu gorlumektedir. Runge-Kutta metodu, kullandigi denklemlerin yapisina bagli olarak derecelerine gore isimlendir. Ikinci, ucuncu, dorduncu... derece metod olarak adlandirilir. Burada Runge-Kutta 2. derece metottan yola cikarak bu metodun calisma prensibini anlamaya calisacagiz ve sonrasinda daha cok kullanilan Runge- Kutta 4. derece metot uzerinde duracagiz. Runge-Kutta 2. Derece Metodu: Runge-Kutta metoduna baslarken Euler metodundan yola cikalim. Euler metodu y i+1 = y i + f(x i, y i ) h (2) olarak verilir. Burada x 0 = 0 ve y 0 = y(x 0 ) = y(0) baslangic degerleri ve h = x i+1 - x i dir. Aslinda yukaridaki denklem h degerinin yerine yazilmasi ve bazi cebirsel islemlerden sonra asagidaki hali alir: y i+1 - y i x i+1 - x i = f(x i, y i ) analitik geometri bilgilerimizden f(x i, y i ) nin aslinda bir egrinin egimine denk geldigini soyleyebiliriz. Ayrica (1) denkleminin de yine bu formatta oldugu acikca gorulmektedir. Bunu grafiksel olarak gosterecek olursak: y i +1 f(x i, y i ) y i x i x i +1 mavi dogrunun egrinin egimine karsilik gelen f(x,y) oldugunu goruruz. Simdi (2) denklemini Taylor seri acilimindan turetmeye calisalim: y i+1 = y i + dy dx (x i+1 - x i ) + 1 d 2 y x i,y i 2! dx (x 2 i+1 - x i ) 2 + 1 d 3 y (x x i,y i 3! dx 3 i+1 - x i ) 3 x i,y i
2 Ders-5-1.nb dy dx = f(x, y) ve h = x i+1 - x i oldugunu goz onune alirsak Taylor serisinin ilk iki teriminin (2) denklemi oldugunu goruruz. Bu durumda Euler metodunun aslinda Runge-Kutta 1. derece metoduna karsilik geldigini soyleyebiliriz. O halde Taylor seri acilimindaki 3. terimi de ise katarak Runge-Kutta 2. derece metodunu elde etmeye calisalim. Serini ilk uc terimi f ve h degerlerinin kullanilmasi ile asagidaki gibi yazilabilir. y i + dy dx (x i+1 - x i ) + 1 d 2 y x i,y i 2! dx (x 2 i+1 - x i ) 2 = y i + f(x i, y i ) h + 1 x i,y i 2! f ' (x i, y i ) h 2 y i+1 = y i + f(x i, y i ) h + 1 2! f ' (x i, y i ) h 2 (3) (3) denklemindeki f in turevini almak her zaman istenilecek birsey degildir. Bunun icin Runge ve Kutta bu ifadenin y i+1 = y i + (a 1 k 1 + a 2 k 2 ) h olarak yazilabilecegini gostermislerdir. Burada k 1 = f(x i, y i ) ve k 2 = f(x i + p 1 h, y i + q 11 k 1 h) dir. a 1, a 2, p 1, q 11 bilinmeyenlerinin elde edilme islemleri burda yapilmayacaktir (Bilmek isteyenler bana ulasabilir). Degerler biraz islemden sonra a 1 + a 2 = 1, a 2 p 1 = 1 2, (4) a 2 q 11 = 1 2 olarak bulunur. (4) denklemlerinde goruldugu uzere 3 denklem ve 4 bilinmeyen mevcuttur. Bu durumda bir bilinmeyenin keyfi olarak secilmesi ile digerleri elde edilebilir. Genelde a 2 keyfi olarak secilip diger bilinmeyenler belirlenir. a 2 = 1 2 secilmesi durumu Heun Metodu, a 2 = 1 secilmesi durumu Orta Nokta (midpoint) Metodu, a 2 = 2 3 secilmesi durumu Ralston Metodu olarak bilinir. Heun Metodu: a 2 = 1 2 durumudur. Boylece a 1 = 1 2, p 1 = 1 ve q 11 = 1 olur. Ve Heun Metodunda y i+1 = y i + 1 2 k 1 + 1 2 k 2 h k 1 = f(x i, y i ) (5) k 2 = f(x i + h, y i + k 1 h) halini alirlar. Orta Nokta (midpoint) Metodu: a 2 = 1 durumudur. Boylece a 1 = 0, p 1 = 1 2 ve q 11 = 1 2 olur. Ve Orta Nokta (midpoint) Metodunda y i+1 = y i + k 2 h k 1 = f(x i, y i ) (6) k 2 = f x i + 1 2 h, y i + 1 2 k 1 h halini alirlar. Ralston Metodu:
Ders-5-1.nb 3 a 2 = 2 3 durumudur. Boylece a 1 = 1 3, p 1 = 3 4 ve q 11 = 3 olur. Ve Ralston Metodunda 4 y i+1 = y i + 1 3 k 1 + 2 3 k 2 h k 1 = f(x i, y i ) (7) k 2 = f x i + 3 4 h, y i + 3 4 k 1 h halini alirlar.
4 Ders-5-1.nb Simdiye kadar elde ettiklerimizin uygulamasini bir diferansiyel denklem uzerinden gorelim. Oncelikle bir f(x,y) fonksiyonu tanimlayalim. ClearAll["Global`* "] In[107]:= f[x_, y_] := y * x^2 - y; Bu fonksiyonu denklem (1) biciminde bir diferansiyel denklem halinde yazip Mathematica nin kendi DSolve komutuyla cozdurelim. In[108]:= DSolve[y'[x] - f[x, y[x]] 0, y[x], x] Out[108]= x3 - x+ y[x] e 3 C[1] Goruldugu gibi sonuc genel cozum olarak bir keyfi sabit cinsinden elde edildi. Simdi bu diferansiyel denklemi numerik olarak Mathematica daki NDSolve komutunu kullanarak cozelim. Numerik cozum icin baslangic degerimiz y(0)=1 olsun ve sonucu x in {0,2} araliginda arayalim: In[109]:= ncozum = NDSolve[{y'[x] - f[x, y[x]] 0, y[0] 1}, y[x], {x, 0, 2}] Out[109]= y[x] InterpolatingFunction Domain: {{0., 2.}} Output : scalar [x] Elde ettigimiz sonucun grafigi asagidaki gibidir: In[110]:= gr = Plot[y[x] /. ncozum, {x, 0, 2}, PlotStyle {Red, Dashed}] Out[110]= 0.5 1.5
Ders-5-1.nb 5 Simdi bu diferansiyel denklemi Runge-Kutta 2. derece metodu olan Heun Metodunu kullanarak cozmeye calisalim. Yukarida numerik cozum icin sectigimiz x araligi ve y baslangic degerini burda kullanirsak x i ve x i+1 e karsilik gelen degerlerimiz {0,2} ve y(0) a karsilik gelen ifademiz de 1 olur. In[200]:= x0 = 0; xs = 2; y0 = 1; Denklemin cozumune 5 adimda ulasmayi deneyelim. Bu durumda h ye karsilik gelen her bir adim sayisi h = x i+1 - x i ifadesini kullanarak bulunur: In[242]:= h = (xs - x0) / 5. Out[242]= 0.4 Ilk adim icin x 1, k 1, k 2 ve y 1 degerleri (6) denklemlerinin yardimiyla asagidaki sekilde elde edilirler. In[163]:= x1 = x0 + h Out[163]= 0.4 In[164]:= k1 = f[x0, y0] k2 = f[x0 + h, y0 + k1 * h] Out[164]= - 1 Out[165]= - 0.504 In[166]:= y1 = y0 + h / 2 * (k1 + k2) Out[166]= 992 Elde ettigimiz degerlerin sonucunu grafik olarak gormek icin ListPlot i kullanalim. In[204]:= hn1 = ListLinePlot[{{x0, y0}, {x1, y1}}, PlotStyle Green] 0 0.95 0.90 Out[204]= 5 0 0.75 0.70 0.1 0.2 0.3 0.4 Simdi birinci adim sonucunda elde ettigimiz deger ile NDSolve komutunu kullanarak elde ettigimiz sonucu bir arada gorelim:
6 Ders-5-1.nb In[168]:= Show[gr, hn1] Out[168]= 0.5 1.5 Yukaridaki birlestirilmis grafikte {x, 0, 0.4} araliginda elde edilen ilk adimin sonucunun asil cozume ne kadar yaklastigi gorulmekte. Simdi ayni islemleri ikinci adim icin tekrarlayip sonuca bakalim: In[169]:= x2 = x1 + h Out[169]= In[170]:= k1 = f[x1, y1] k2 = f[x1 + h, y1 + k1 * h] Out[170]= - 0.587328 Out[171]= - 0.167137 In[172]:= y2 = y1 + h / 2 * (k1 + k2) Out[172]= 0.548307 Ilk iki adimin birlikte grafigi: In[205]:= hn2 = ListLinePlot[{{x0, y0}, {x1, y1}, {x2, y2}}, PlotStyle Green] 0.9 Out[205]= 0.7 0.2 0.4 Ve asil cozum ile {x, 0, } araliginda elde edilen cozumu birlestirirsek:
Ders-5-1.nb 7 In[174]:= Show[gr, hn2] Out[174]= 0.5 1.5 Ayni islemleri ucuncu adim icin tekrarlayalim: In[175]:= x3 = x2 + h Out[175]= In[176]:= k1 = f[x2, y2] k2 = f[x2 + h, y2 + k1 * h] Out[176]= - 0.197391 Out[177]= 0.206514 In[178]:= y3 = y2 + h / 2 * (k1 + k2) Out[178]= 0.550132 In[206]:= hn3 = ListLinePlot[{{x0, y0}, {x1, y1}, {x2, y2}, {x3, y3}}, PlotStyle Green] 0.9 Out[206]= 0.7 0.2 0.4
8 Ders-5-1.nb In[180]:= Show[gr, hn3] Out[180]= 0.5 1.5 4. adim: In[181]:= x4 = x3 + h Out[181]= In[182]:= k1 = f[x3, y3] k2 = f[x3 + h, y3 + k1 * h] Out[182]= 0.242058 Out[183]= 0925 In[184]:= y4 = y3 + h / 2 * (k1 + k2) Out[184]= 00393 In[207]:= hn4 = ListLinePlot[{{x0, y0}, {x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}}, PlotStyle Green] 0.9 Out[207]= 0.7 0.5 1.5
Ders-5-1.nb 9 In[186]:= Show[gr, hn4] Out[186]= Ve son olarak besinci adim: 0.5 1.5 In[187]:= x5 = x4 + h Out[187]= 2. In[188]:= k1 = f[x4, y4] k2 = f[x4 + h, y4 + k1 * h] Out[188]= 4861 Out[189]= 3.89952 In[190]:= y5 = y4 + h / 2 * (k1 + k2) Out[190]= 3002 In[208]:= hn5 = ListLinePlot[ {{x0, y0}, {x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}, {x5, y5}}, PlotStyle Green] 1.5 Out[208]= 0.5 0.5 1.5
10 Ders-5-1.nb In[192]:= Show[gr, hn5] Out[192]= 0.5 1.5 Sonuc olarak Runge-Kutta 2. derece metodu ile elde edilen sonucun grafigini gormekteyiz. Grafik her ne kadar asil cozumun grafigine yaklassa da elde edilen sonucun pek tatmin edici olmadigi acik bir sekilde gorulmekte.
Ders-5-1.nb 11 Runge-Kutta 4. Derece Metodu: Runge-Kutta 4. derece metodu, 2. derece metodunun turetildigi gibi turetilir. Bu sefer Taylor seri acilimindan fazladan 2 terim daha gelir. Butun islemlerin tekrarlanmasi sonucunda asagidaki denklemlere ulasilir: y i+1 = y i + 1 6 (k 1 + 2 k 2 + 2 k 3 + k 4 ) h k 1 = f(x i, y i ) k 2 = f x i + 1 2 h, y i + 1 2 k 1 h (8) k 3 = f x i + 1 2 h, y i + 1 2 k 2 h k 4 = f(x i + h, y i + k 3 h) Simdi ayni diferansiyel denklemi Runge-Kutta 4. derece metodu ile cozelim. Yine 5 adimdan olusacak olan cozumde baslangic degeri ve x araligi ayni olsun. In[252]:= x0 = 0; xs = 2; y0 = 1; In[255]:= h = (xs - x0) / 5. Out[255]= 0.4 (8) denklemlerinin yardimiyla ilk adim icin x 1, k 1, k 2 ve y 1 degerleri asagidaki sekilde elde edilirler. In[256]:= x1 = x0 + h Out[256]= 0.4 In[257]:= k1 = f[x0, y0] k2 = f[x0 + 1 / 2 * h, y0 + 1 / 2 * k1 * h] k3 = f[x0 + 1 / 2 * h, y0 + 1 / 2 * k2 * h] k4 = f[x0 + h, y0 + k3 * h] Out[257]= - 1 Out[258]= - 0.768 Out[259]= - 12544 Out[260]= - 0.566985 In[261]:= y1 = y0 + h / 6 * (k1 + 2 * k2 + 2 * k3 + k4) Out[261]= 84795 Ilk adim icin grafik:
12 Ders-5-1.nb In[262]:= rk1 = ListLinePlot[{{x0, y0}, {x1, y1}}, PlotStyle Blue] 0 0.95 0.90 Out[262]= 5 0 0.75 0.70 0.1 0.2 0.3 0.4 NDSolve ile elde edilen sonuc ile Runge-Kutta 4. derece metot ile elde edilen ilk adim sonucunu ayni grafikte gosterelim: In[263]:= Show[gr, rk1] Out[263]= 0.5 1.5 Ayni islemleri ikinci adim icin yapalim: In[264]:= x2 = x1 + h Out[264]= In[265]:= k1 = f[x1, y1] k2 = f[x1 + 1 / 2 * h, y1 + 1 / 2 * k1 * h] k3 = f[x1 + 1 / 2 * h, y1 + 1 / 2 * k2 * h] k4 = f[x1 + h, y1 + k3 * h] Out[265]= - 0.575228 Out[266]= - 0.36464 Out[267]= - 0.391595 Out[268]= - 0.190137
Ders-5-1.nb 13 In[269]:= y2 = y1 + h / 6 * (k1 + 2 * k2 + 2 * k3 + k4) Out[269]= 0.53294 In[270]:= rk2 = ListLinePlot[{{x0, y0}, {x1, y1}, {x2, y2}}, PlotStyle Blue] 0.9 Out[270]= 0.7 0.2 0.4 In[271]:= Show[gr, rk2] Out[271]= 0.5 1.5 Ucuncu adim: In[272]:= x3 = x2 + h Out[272]= In[273]:= k1 = f[x2, y2] k2 = f[x2 + 1 / 2 * h, y2 + 1 / 2 * k1 * h] k3 = f[x2 + 1 / 2 * h, y2 + 1 / 2 * k2 * h] k4 = f[x2 + h, y2 + k3 * h] Out[273]= - 0.191858 Out[274]= 0. Out[275]= 0. Out[276]= 0.234493
14 Ders-5-1.nb In[277]:= y3 = y2 + h / 6 * (k1 + 2 * k2 + 2 * k3 + k4) Out[277]= 0.535782 In[278]:= rk3 = ListLinePlot[{{x0, y0}, {x1, y1}, {x2, y2}, {x3, y3}}, PlotStyle Blue] 0.9 Out[278]= 0.7 0.2 0.4 In[279]:= Show[gr, rk3] Out[279]= 0.5 1.5 4. adim: In[280]:= x4 = x3 + h Out[280]= In[281]:= k1 = f[x3, y3] k2 = f[x3 + 1 / 2 * h, y3 + 1 / 2 * k1 * h] k3 = f[x3 + 1 / 2 * h, y3 + 1 / 2 * k2 * h] k4 = f[x3 + h, y3 + k3 * h] Out[281]= 0.235744 Out[282]= 0.559613 Out[283]= 21796 Out[284]= 2382
Ders-5-1.nb 15 In[285]:= y4 = y3 + h / 6 * (k1 + 2 * k2 + 2 * k3 + k4) Out[285]= 0.790607 In[286]:= rk4 = ListLinePlot[{{x0, y0}, {x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}}, PlotStyle Blue] 0.9 Out[286]= 0.7 0.5 1.5 In[287]:= Show[gr, rk4] Out[287]= 0.5 1.5 5. adim: In[288]:= x5 = x4 + h Out[288]= 2. In[289]:= k1 = f[x4, y4] k2 = f[x4 + 1 / 2 * h, y4 + 1 / 2 * k1 * h] k3 = f[x4 + 1 / 2 * h, y4 + 1 / 2 * k2 * h] k4 = f[x4 + h, y4 + k3 * h] Out[289]= 3335 Out[290]= 2.3235 Out[291]= 2.81189 Out[292]= 5.74609
16 Ders-5-1.nb In[293]:= y5 = y4 + h / 6 * (k1 + 2 * k2 + 2 * k3 + k4) Out[293]= 1.94062 In[294]:= rk5 = ListLinePlot[ {{x0, y0}, {x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}, {x5, y5}}, PlotStyle Blue] 1.5 Out[294]= 0.5 0.5 1.5 In[295]:= Show[gr, rk5] Out[295]= 0.5 1.5 Simdi Runge-Kutta 2. derece ve 4. derece metot ile elde ettigimiz sonuclari ayni grafikte gorerek karsilastiralim:
Ders-5-1.nb 17 In[296]:= Show[gr, hn5, rk5] Out[296]= 0.5 1.5 Grafiklerden goruldugu gibi 4. derece sonuclari 2. derece sonuclarindan daha cok asil cozume yakinlar. Fakat bu sonuclar yine de asil cozumden cok uzaklar. Bu sonuclari iyilestirmenin yolu adim sayisini arttirmaktir. Simdiye kadar sadece 5 adim ile sonuclari elde ettik fakat bu bile yeterince yorucuydu. Cok daha yuksek adimlara cikmak icin cozumu her defasinda elle yapmak yerine, yazacagimiz bir kod ile bunu Mathematica nin otomatik olarak yapmasini saglayabiliriz. Bunu Do, For, While dongulerinin biriyle yapabiliriz.