Below you will find complete solutions for the Example given the previous post – you can use this to test your project.

Example.  Given the differential equation dy/dx=x^2-0.5xy and initial condition y(1.5)=2.2, approximate the value of y(2.5) using step size h=0.05

ACTUAL ANSWER: y(2.5) = 3.49201…

Euler’s Method
i h x_i y_i k = f(x_i,y_i) y_(i+1)
0 0.05 1.5 2.2 0.6 2.23
1 0.05 1.55 2.23 0.67425 2.2637125
2 0.05 1.6 2.2637125 0.74903 2.301164
3 0.05 1.65 2.301164 0.8240397 2.342365985
4 0.05 1.7 2.342365985 0.8989889128 2.387315431
5 0.05 1.75 2.387315431 0.9735989982 2.435995381
6 0.05 1.8 2.435995381 1.047604158 2.488375588
7 0.05 1.85 2.488375588 1.120752581 2.544413217
8 0.05 1.9 2.544413217 1.192807443 2.60405359
9 0.05 1.95 2.60405359 1.26354775 2.667230977
10 0.05 2 2.667230977 1.332769023 2.733869428
11 0.05 2.05 2.733869428 1.400283836 2.80388362
12 0.05 2.1 2.80388362 1.465922199 2.87717973
13 0.05 2.15 2.87717973 1.52953179 2.95365632
14 0.05 2.2 2.95365632 1.590978049 3.033205222
15 0.05 2.25 3.033205222 1.650144125 3.115712428
16 0.05 2.3 3.115712428 1.706930708 3.201058964
17 0.05 2.35 3.201058964 1.761255718 3.289121749
18 0.05 2.4 3.289121749 1.813053901 3.379774445
19 0.05 2.45 3.379774445 1.862276305 3.47288826
20 0.05 2.5 y(2.5) = 3.47288826

 

 

 

 

 

Improved Euler’s Method
i h x_i y_i k1 z_(i+1) k2 y_(i+1)
0 0.05 1.5 2.2 0.6 2.23 0.67425 2.23185625
1 0.05 1.55 2.23185625 0.6728114063 2.26549682 0.7476025438 2.267366599
2 0.05 1.6 2.267366599 0.746106721 2.304671935 0.8211456538 2.306547908
3 0.05 1.65 2.306547908 0.8195979758 2.347527807 0.8946013641 2.349402892
4 0.05 1.7 2.349402892 0.8930075421 2.394053269 0.9677033899 2.395920665
5 0.05 1.75 2.395920665 0.9660694182 2.444224136 1.040198278 2.446077357
6 0.05 1.8 2.446077357 1.038530378 2.498003876 1.111846414 2.499836777
7 0.05 1.85 2.499836777 1.110150981 2.555344326 1.18242289 2.557151124
8 0.05 1.9 2.557151124 1.180706432 2.616186446 1.251718216 2.61796174
9 0.05 1.95 2.61796174 1.249987303 2.680461105 1.319538895 2.682199895
10 0.05 2 2.682199895 1.317800105 2.7480899 1.385707852 2.749787594
11 0.05 2.05 2.749787594 1.383967716 2.81898598 1.450064721 2.820638405
12 0.05 2.1 2.820638405 1.448329675 2.893054889 1.512465995 2.894658297
13 0.05 2.15 2.894658297 1.510742331 2.970195413 1.572785045 2.971746481
14 0.05 2.2 2.971746481 1.571078871 3.050300425 1.630912022 3.051796253
15 0.05 2.25 3.051796253 1.629229215 3.133257714 1.686753629 3.134695825
16 0.05 2.3 3.134695825 1.685099802 3.218950815 1.740232793 3.220329139
17 0.05 2.35 3.220329139 1.738613261 3.307259802 1.791288237 3.308576677
18 0.05 2.4 3.308576677 1.789707988 3.398062076 1.839873957 3.399316225
19 0.05 2.45 3.399316225 1.838337624 3.491233107 1.885958617 3.492423631
20 0.05 2.5 y(2) = 3.492423631

 

 

 

 

Runge-Kutta
i h x_i y_i k1 = f(x_i,y_i) k2 = f(x_i+.5h,y_i+.5hk1) k3 = f(x_i+.5h, y_i+.5hk2) k4 = f(x+h,y+hk3) Runge-Kutta y_(i+1) = y_i + h*(k1+2k2+2k3+k4)/6
0 0.05 1.5 2.2 0.6 0.6366875 0.6359881445 0.6728554594 2.23181839
1 0.05 1.55 2.23181839 0.6728407481 0.709821466 0.7090934081 0.746181552 2.267292157
2 0.05 1.6 2.267292157 0.7461662747 0.7832936203 0.7825394711 0.8197042176 2.306438296
3 0.05 1.65 2.306438296 0.8196884061 0.8568207014 0.856043244 0.8931456109 2.349259645
4 0.05 1.7 2.349259645 0.8931293019 0.9301304558 0.9293326184 0.9662395087 2.395745436
5 0.05 1.75 2.395745436 0.9662227434 1.002962858 1.002147687 1.038732462 2.445871905
6 0.05 1.8 2.445871905 1.038715285 1.075071194 1.074241825 1.110384803 2.499602956
7 0.05 1.85 2.499602956 1.110367265 1.146222996 1.145382627 1.180971517 2.556890873
8 0.05 1.9 2.556890873 1.18095367 1.216200837 1.215352702 1.250282954 2.617677071
9 0.05 1.95 2.617677071 1.250264856 1.284802979 1.283950319 1.318125413 2.681892878
10 0.05 2 2.681892878 1.318107122 1.351843875 1.350989913 1.384321567 2.749460347
11 0.05 2.05 2.749460347 1.384303145 1.417154527 1.416302445 1.448710757 2.820293079
12 0.05 2.1 2.820293079 1.448692267 1.480582715 1.479735625 1.51114915 2.894297063
13 0.05 2.15 2.894297063 1.511130657 1.541993079 1.541154007 1.57150976 2.971371518
14 0.05 2.2 2.971371518 1.57149133 1.601267084 1.600438946 1.629682352 3.051409732
15 0.05 2.25 3.051409732 1.629664051 1.658302858 1.657488442 1.685573222 3.134299898
16 0.05 2.3 3.134299898 1.685555117 1.713014923 1.712216872 1.739104879 3.219925928
17 0.05 2.35 3.219925928 1.739087035 1.765333814 1.764554613 1.79021561 3.308168257
18 0.05 2.4 3.308168257 1.790198091 1.815205609 1.814447568 1.838858971 3.398904619
19 0.05 2.45 3.398904619 1.838841842 1.862591365 1.861856614 1.885003188 3.492010794
20 0.05 2.5 y(2.5) = 3.492010794