Método de Euler

Método de Euler hacia adelante

El método de Euler hacia adelante o método de Euler es un método explícito

\begin{equation*} y_{n+1} = y_{n} + h f(t_{n}, y_{n}) \end{equation*}

Ejemplo

Resolver

\begin{align*} y' &= 1 + x^{2} + x^{3} \\ y(2) &= 5 \\ y(5) &= ? \end{align*}

La solución analítica es

\begin{align*} y &= x + \frac{x^{3}}{3} + \frac{x^{4}}{4} - \frac{11}{3} \\ y(5) &= 5 + \frac{5^{3}}{3} + \frac{5^{4}}{4} - \frac{11}{3} = 199.25 \end{align*}

En este caso la derivada está en función de $x$

\begin{equation*} y_{n+1} = y_{n} + h f(x_{n}) \end{equation*}

Para la solución numérica elegimos $n = 3$

\begin{equation*} h = \frac{x_{f} - x_{i}}{n} = \frac{5 - 2}{3} = 1 \end{equation*}

Iteracion 0

\begin{align*} y_{0} &= 5 \\ x_{0} &= 2 \end{align*}

Iteración 1

\begin{align*} y_{1} &= y_{0} + h f(x_{0}) = 5 + 1(1 + 2^{2} + 2^{3}) = 18 \\ x_{1} &= x_{0} + h = 2 + 1 = 3 \end{align*}

Iteración 2

\begin{align*} y_{2} &= y_{1} + h f(x_{1}) = 18 + 1(1 + 3^{2} + 3^{3}) = 55 \\ x_{2} &= x_{1} + h = 3 + 1 = 4 \end{align*}

Iteración 3

\begin{align*} y_{3} &= y_{2} + h f(x_{2}) = 55 + 1(1 + 4^{2} + 4^{3}) = 136 \\ x_{3} &= x_{2} + h = 4 + 1 = 5 \end{align*}

Seudocódigo

xi = 2
yi = 5
xf = 5
n = 3

h = (xf - xi)/n
y = yi
x = xi

for i=1 to n do
    y = y + h*(1 + x^2 + x^3)
    x = x + h
end for

Implementación

Ecuación diferencial


In [1]:
function f(x)
    1 + x^2 + x^3
end


Out[1]:
f (generic function with 1 method)

Función Euler de un solo paso


In [2]:
function MétodoEulerAdelante(x, y , h)
    y = y + h*f(x)
    x = x + h
    return x, y
end


Out[2]:
MétodoEulerAdelante (generic function with 1 method)

Programa principal


In [3]:
xi = 2
yi = 5
xf = 5
n = 50

h = (xf - xi) / n 
y = yi
x = xi
@printf("%s \t %s \t %17s\n", "i", "x", "y")
@printf("%s \t %s \t %17s\n", 0, x, y)

for i= 1:n
    x, y = MétodoEulerAdelante(x, y, h)
    @printf("%d \t %0.15f \t %0.15f\n", i, x, y)
end


i 	 x 	                 y
0 	 2 	                 5
1 	 2.060000000000000 	 5.780000000000000
2 	 2.120000000000000 	 6.619124960000001
3 	 2.180000000000000 	 7.520476640000001
4 	 2.240000000000000 	 8.487234560000001
5 	 2.300000000000000 	 9.522656000000001
6 	 2.360000000000000 	 10.630076000000003
7 	 2.420000000000000 	 11.812907360000002
8 	 2.480000000000000 	 13.074640640000002
9 	 2.540000000000000 	 14.418844160000003
10 	 2.600000000000001 	 15.849164000000004
11 	 2.660000000000001 	 17.369324000000006
12 	 2.720000000000001 	 18.983125760000007
13 	 2.780000000000001 	 20.694448640000008
14 	 2.840000000000001 	 22.507249760000008
15 	 2.900000000000001 	 24.425564000000008
16 	 2.960000000000001 	 26.453504000000009
17 	 3.020000000000001 	 28.595260160000009
18 	 3.080000000000001 	 30.855100640000011
19 	 3.140000000000001 	 33.237371360000012
20 	 3.200000000000001 	 35.746496000000015
21 	 3.260000000000001 	 38.386976000000018
22 	 3.320000000000001 	 41.163390560000018
23 	 3.380000000000001 	 44.080396640000018
24 	 3.440000000000001 	 47.142728960000021
25 	 3.500000000000001 	 50.355200000000025
26 	 3.560000000000001 	 53.722700000000032
27 	 3.620000000000001 	 57.250196960000032
28 	 3.680000000000001 	 60.942736640000035
29 	 3.740000000000002 	 64.805442560000046
30 	 3.800000000000002 	 68.843516000000051
31 	 3.860000000000002 	 73.062236000000055
32 	 3.920000000000002 	 77.466959360000061
33 	 3.980000000000002 	 82.063120640000065
34 	 4.040000000000002 	 86.856232160000076
35 	 4.100000000000001 	 91.851884000000084
36 	 4.160000000000001 	 97.055744000000089
37 	 4.220000000000001 	 102.473557760000091
38 	 4.280000000000000 	 108.111148640000096
39 	 4.340000000000000 	 113.974417760000094
40 	 4.399999999999999 	 120.069344000000086
41 	 4.459999999999999 	 126.401984000000084
42 	 4.519999999999999 	 132.978472160000081
43 	 4.579999999999998 	 139.805020640000066
44 	 4.639999999999998 	 146.887919360000069
45 	 4.699999999999998 	 154.233536000000072
46 	 4.759999999999997 	 161.848316000000068
47 	 4.819999999999997 	 169.738782560000061
48 	 4.879999999999996 	 177.911536640000037
49 	 4.939999999999996 	 186.373256960000020
50 	 4.999999999999996 	 195.130699999999990

Método de Euler modificado

El método de Euler modificado o método del punto medio es un método implícito

\begin{equation*} y_{n+1} = y_{n} + h f \left( t_{n} + \frac{h}{2}, \frac{h}{2} (y_{n} + y_{n+1}) \right) \end{equation*}

se puede aproximar con

\begin{align*} \tilde{y}_{n + \frac{1}{2}} &= y_{n} + \frac{h}{2} f(t_{n}, y_{n}) \\ y_{n+1} &= y_{n} + h f \left( t_{n + \frac{1}{2}} , \tilde{y}_{n + \frac{1}{2}} \right) \end{align*}

Ejemplo

Resolver

\begin{align*} y' &= \frac{5 - 2 y}{3 y^{2} + 2 x} \\ y(0) &= 2 \\ y(3) &= ? \end{align*}

La solución analítica es:

\begin{align*} F(x,y) &= y^{3} + 2 x y - 5 x - 8 \\ y(3) &\approx 2.158 \end{align*}

Para la solución numérica elegimos $n = 3$

\begin{equation*} h = \frac{x_{f} - x_{i}}{n} = \frac{3 - 0}{3} = 1 \end{equation*}

Iteracion 0

\begin{align*} y_{0} &= 2 \\ x_{0} &= 0 \end{align*}

Iteración 1

\begin{align*} \tilde{y}_{0.5} &= y_{0} + \frac{h}{2} f(x_{0}, y_{0}) = 2 + \frac{1}{2} \left[ \frac{5 - 2 (2)}{3 (2)^{2} + 2 (0)} \right] = 2.042 \\ x_{0.5} &= x_{0} + \frac{h}{2} = 0 + \frac{1}{2} = 0.5 \\ y_{1} &= y_{0} + h f(x_{0.5}, \tilde{y}_{0.5}) = 2 + 1 \left[ \frac{5 - 2 (2.042)}{3 (2.042)^{2} + 2 (0.5)} \right] = 2.068 \\ x_{1} &= x_{0} + h = 0 + 1 = 1 \end{align*}

Iteración 2

\begin{align*} \tilde{y}_{1.5} &= y_{1} + \frac{h}{2} f(x_{1}, y_{1}) = 2.068 + \frac{1}{2} \left[ \frac{5 - 2 (2.068)}{3 (2.068)^{2} + 2 (1)} \right] = 2.097 \\ x_{1.5} &= x_{1} + \frac{h}{2} = 1 + \frac{1}{2} = 1.5 \\ y_{2} &= y_{1} + h f(x_{1.5}, \tilde{y}_{1.5}) = 2.068 + 1 \left[ \frac{5 - 2 (2.097)}{3 (2.097)^{2} + 2 (1.5)} \right] = 2.118 \\ x_{2} &= x_{1} + h = 1 + 1 = 2 \end{align*}

Iteración 3

\begin{align*} \tilde{y}_{2.5} &= y_{2} + \frac{h}{2} f(x_{2}, y_{2}) = 2.118 + \frac{1}{2} \left[ \frac{5 - 2 (2.118)}{3 (2.118)^{2} + 2 (2)} \right] = 2.140 \\ x_{2.5} &= x_{2} + \frac{h}{2} = 2 + \frac{1}{2} = 2.5 \\ y_{3} &= y_{2} + h f(x_{2.5}, \tilde{y}_{2.5}) = 2.118 + 1 \left[ \frac{5 - 2 (2.140)}{3 (2.140)^{2} + 2 (2.5)} \right] = 2.156 \\ x_{3} &= x_{2} + h = 2 + 1 = 3 \end{align*}

In [4]:
function dydx(x, y)
    (5 - 2*y)/((3*y^2) + (2*x))
end


Out[4]:
dydx (generic function with 1 method)

In [5]:
function PuntoMedio(xi, yi , h)
    ym = yi + (h/2)*dydx(xi, yi)
    xm = xi + (h/2)
    y = yi + h*dydx(xm, ym)
    x = xi + h
    return x, y
end


Out[5]:
PuntoMedio (generic function with 1 method)

In [6]:
xi = 0
yi = 2
xf = 3
n = 50

h = (xf - xi) / n 
y = yi
x = xi
@printf("%s \t %s \t %17s\n", "i", "x", "y")
@printf("%s \t %s \t %17s\n", 0, x, y)

for i= 1:n
    x, y = PuntoMedio(x, y, h)
    @printf("%d \t %0.15f \t %0.15f\n", i, x, y)
end


i 	 x 	                 y
0 	 0 	                 2
1 	 0.060000000000000 	 2.004937957602423
2 	 0.120000000000000 	 2.009756304550151
3 	 0.180000000000000 	 2.014460082479771
4 	 0.240000000000000 	 2.019054021653052
5 	 0.300000000000000 	 2.023542566380461
6 	 0.360000000000000 	 2.027929897872840
7 	 0.420000000000000 	 2.032219954830004
8 	 0.480000000000000 	 2.036416452032334
9 	 0.540000000000000 	 2.040522897165393
10 	 0.600000000000000 	 2.044542606077051
11 	 0.660000000000000 	 2.048478716640663
12 	 0.720000000000000 	 2.052334201375646
13 	 0.780000000000000 	 2.056111878957881
14 	 0.840000000000000 	 2.059814424736064
15 	 0.900000000000000 	 2.063444380356105
16 	 0.960000000000000 	 2.067004162583574
17 	 1.020000000000000 	 2.070496071403710
18 	 1.080000000000001 	 2.073922297469388
19 	 1.140000000000001 	 2.077284928959526
20 	 1.200000000000001 	 2.080585957903488
21 	 1.260000000000001 	 2.083827286020988
22 	 1.320000000000001 	 2.087010730121698
23 	 1.380000000000001 	 2.090138027104097
24 	 1.440000000000001 	 2.093210838588989
25 	 1.500000000000001 	 2.096230755219501
26 	 1.560000000000001 	 2.099199300656159
27 	 1.620000000000001 	 2.102117935292821
28 	 1.680000000000001 	 2.104988059716689
29 	 1.740000000000001 	 2.107811017933426
30 	 1.800000000000001 	 2.110588100376380
31 	 1.860000000000001 	 2.113320546717133
32 	 1.920000000000001 	 2.116009548493030
33 	 1.980000000000001 	 2.118656251565879
34 	 2.040000000000001 	 2.121261758424771
35 	 2.100000000000001 	 2.123827130344792
36 	 2.160000000000001 	 2.126353389412389
37 	 2.220000000000002 	 2.128841520427210
38 	 2.280000000000002 	 2.131292472689402
39 	 2.340000000000002 	 2.133707161680593
40 	 2.400000000000002 	 2.136086470646109
41 	 2.460000000000002 	 2.138431252085333
42 	 2.520000000000002 	 2.140742329156580
43 	 2.580000000000002 	 2.143020497002329
44 	 2.640000000000002 	 2.145266524000206
45 	 2.700000000000002 	 2.147481152944668
46 	 2.760000000000002 	 2.149665102163974
47 	 2.820000000000002 	 2.151819066576682
48 	 2.880000000000002 	 2.153943718691549
49 	 2.940000000000002 	 2.156039709554484
50 	 3.000000000000002 	 2.158107669645875

Método de Euler hacia atrás

El método de Euler hacia atrás es un método implícito

\begin{equation*} y_{n+1} = y_{n} + h f(t_{n+1}, y_{n+1}) \end{equation*}

Puede hacerse una aproximación con

\begin{align*} \tilde{y}_{n+1} &= y_{n} + h f(t_{n}, y_{n}) \\ y_{n+1} &= y_{n} + h f(t_{n+1}, \tilde{y}_{n+1}) \end{align*}

Ejemplo

Resolver

\begin{align*} y' &= y + 2 x e^{2 x} \\ y(0) &= 1 \\ y(3) &= ? \end{align*}

La solución analítica es

\begin{align*} y &= 3 e^{x} + 2 (x - 1) e^{2 x} \\ y(3) &= 3 e^{3} + 2 (3 - 1) e^{2 (3)} = 1673.972 \end{align*}

Para la solución numérica elegimos $n = 3$

\begin{equation*} h = \frac{x_{f} - x_{i}}{n} = \frac{3 - 0}{3} = 1 \end{equation*}

Iteracion 0

\begin{align*} y_{0} &= 1 \\ x_{0} &= 0 \end{align*}

Iteración 1

\begin{align*} \tilde{y}_{1} &= y_{0} + h f(x_{0}, y_{0}) = 1 + 1[1 + 2 (0) e^{2 (0)}] = 2 \\ x_{1} &= x_{0} + h = 0 + 1 = 1 \\ y_{1} &= y_{0} + h f(x_{1}, \tilde{y}_{1}) = 1 + 1[2 + 2 (1) e^{2 (1)}] = 17.778 \end{align*}

Iteración 2

\begin{align*} \tilde{y}_{2} &= y_{1} + h f(x_{1}, y_{1}) = 17.778 + 1[17.778 + 2 (1) e^{2 (1)}] = 50.334 \\ x_{2} &= x_{1} + h = 1 + 1 = 2 \\ y_{2} &= y_{1} + h f(x_{2}, \tilde{y}_{2}) = 17.778 + 1[50.334 + 2 (2) e^{2 (2)}] = 286.505 \end{align*}

Iteración 3

\begin{align*} \tilde{y}_{3} &= y_{2} + h f(x_{2}, y_{2}) = 286.505 + 1[286.505 + 2 (2) e^{2 (2)}] = 791.403 \\ x_{3} &= x_{2} + h = 2 + 1 = 3 \\ y_{3} &= y_{2} + h f(x_{3}, \tilde{y}_{3}) = 286.505 + 1[791.403 + 2 (3) e^{2 (3)}] = 3498.481 \end{align*}

In [7]:
function dydx(x, y)
    y + 2*x*exp(2*x)
end


Out[7]:
dydx (generic function with 1 method)

In [8]:
function MétodoEulerAtrás(xi, yi , h)
    yf = yi + h*dydx(xi, yi)
    x = xi + h
    y = yi + h*dydx(x, yf)
    return x, y
end


Out[8]:
MétodoEulerAtrás (generic function with 1 method)

In [9]:
xi = 0
yi = 1
xf = 3
n = 50

h = (xf - xi) / n 
y = yi
x = xi
@printf("%s \t %s \t %17s\n", "i", "x", "y")
@printf("%s \t %s \t %17s\n", 0, x, y)

for i= 1:n
    x, y = MétodoEulerAtrás(x, y, h)
    @printf("%d \t %0.15f \t %0.15f\n", i, x, y)
end


i 	 x 	                 y
0 	 0 	                 1
1 	 0.060000000000000 	 1.071717977331371
2 	 0.120000000000000 	 1.158672307094157
3 	 0.180000000000000 	 1.264422140445727
4 	 0.240000000000000 	 1.393239926282500
5 	 0.300000000000000 	 1.550238838975115
6 	 0.360000000000000 	 1.741521320442592
7 	 0.420000000000000 	 1.974352062934496
8 	 0.480000000000000 	 2.257359264744088
9 	 0.540000000000000 	 2.600768571903008
10 	 0.600000000000000 	 3.016674785607614
11 	 0.660000000000000 	 3.519357180157541
12 	 0.720000000000000 	 4.125645153788806
13 	 0.780000000000000 	 4.855341941229987
14 	 0.840000000000000 	 5.731715270694004
15 	 0.900000000000000 	 6.782065170580088
16 	 0.960000000000000 	 8.038380646660674
17 	 1.020000000000000 	 9.538098686670821
18 	 1.080000000000001 	 11.324981037636519
19 	 1.140000000000001 	 13.450126478085970
20 	 1.200000000000001 	 15.973138913706045
21 	 1.260000000000001 	 18.963474608079419
22 	 1.320000000000001 	 22.501995273550254
23 	 1.380000000000001 	 26.682757652267878
24 	 1.440000000000001 	 31.615074683923027
25 	 1.500000000000001 	 37.425888464320536
26 	 1.560000000000001 	 44.262501038624194
27 	 1.620000000000001 	 52.295715748517985
28 	 1.680000000000001 	 61.723449481840177
29 	 1.740000000000001 	 72.774884891200912
30 	 1.800000000000001 	 85.715241608282184
31 	 1.860000000000001 	 100.851256858000951
32 	 1.920000000000001 	 118.537478870999792
33 	 1.980000000000001 	 139.183491331357004
34 	 2.040000000000001 	 163.262204038012896
35 	 2.100000000000001 	 191.319364298298780
36 	 2.160000000000001 	 223.984465646301828
37 	 2.220000000000002 	 261.983255670512051
38 	 2.280000000000002 	 306.152073480377339
39 	 2.340000000000002 	 357.454280136669070
40 	 2.400000000000002 	 416.999082781439938
41 	 2.460000000000002 	 486.063095873709074
42 	 2.520000000000002 	 566.115031599865233
43 	 2.580000000000002 	 658.843967017588966
44 	 2.640000000000002 	 766.191698758678172
45 	 2.700000000000002 	 890.389768240574085
46 	 2.760000000000002 	 1034.001822548910923
47 	 2.820000000000002 	 1199.972069854090478
48 	 2.880000000000002 	 1391.680695006742553
49 	 2.940000000000002 	 1613.007222632495768
50 	 3.000000000000002 	 1868.402953677800951

Método de Heun

El método de Heun es un método implícito

\begin{equation*} y_{n+1} = y_{n} + \frac{h}{2} [f(t_{n}, y_{n}) + f(t_{n+1}, y_{n+1})] \end{equation*}

Puede hacerse una aproximación en forma iterativa con el corrector

\begin{align*} \tilde{y}_{n+1} &= y_{n} + h f(t_{n}, y_{n}) & \mbox{predictor} \\ y_{n+1} &= y_{n} + \frac{h}{2} [f(t_{n}, y_{n}) + f(t_{n+1}, \tilde{y}_{n+1})] & \mbox{corrector} \end{align*}

Ejemplo

Resolver

\begin{align*} y' &= 4 e^{0.8 x} - 0.5 y \\ y(0) &= 2 \\ y(4) &= ? \end{align*}

La solución analítica es

\begin{align*} y &= \frac{4}{1.3} (e^{0.8 x} - e^{-0.5 x}) + 2 e^{-0.5 x} \\ y(4) &= \frac{4}{1.3} [e^{0.8 (4)} - e^{-0.5 (4)}] + 2 e^{-0.5 (4)} = 75.33 \end{align*}

Para la solución numérica elegimos $n = 4$

\begin{equation*} h = \frac{x_{f} - x_{i}}{n} = \frac{4 - 0}{4} = 1 \end{equation*}

Iteracion 0

\begin{align*} y_{0} &= 2 \\ x_{0} &= 0 \end{align*}

Iteración 1

Se calculará una iteración con el corrector

\begin{align*} \tilde{y}_{1} &= y_{0} + h f(x_{0}, y_{0}) = 2 + 1[4 e^{0.8 (0)} - 0.5 (2)] = 5 \\ x_{1} &= x_{0} + h = 0 + 1 = 1 \\ y_{1} &= y_{0} + \frac{h}{2} [f(x_{0}, y_{0}) + f(x_{1}, \tilde{y}_{1})] = 2 + \frac{1}{2} [4 e^{0.8 (0)} - 0.5 (2) + 4 e^{0.8 (1)} - 0.5 (5)] = 6.701 \end{align*}

Iteración 2

\begin{align*} \tilde{y}_{2} &= y_{1} + h f(x_{1}, y_{1}) = 6.701 + 1[4 e^{0.8 (1)} - 0.5 (6.701)] = 12.253 \\ x_{2} &= x_{1} + h = 1 + 1 = 2 \\ y_{2} &= y_{1} + \frac{h}{2} [f(x_{1}, y_{1}) + f(x_{2}, \tilde{y}_{2})] = 6.701 + \frac{1}{2} [4 e^{0.8 (1)} - 0.5 (6.701) + 4 e^{0.8 (2)} - 0.5 (12.253)] = 16.320 \end{align*}

Iteración 3

\begin{align*} \tilde{y}_{3} &= y_{2} + h f(x_{2}, y_{2}) = 16.320 + 1[4 e^{0.8 (2)} - 0.5 (16.320)] = 27.972 \\ x_{3} &= x_{2} + h = 2 + 1 = 3 \\ y_{3} &= y_{2} + \frac{h}{2} [f(x_{2}, y_{2}) + f(x_{3}, \tilde{y}_{3})] = 16.320 + \frac{1}{2} [4 e^{0.8 (2)} - 0.5 (16.320) + 4 e^{0.8 (3)} - 0.5 (27.972)] = 37.199 \end{align*}

Iteración 4

\begin{align*} \tilde{y}_{4} &= y_{3} + h f(x_{3}, y_{3}) = 37.199 + 1[4 e^{0.8 (3)} - 0.5 (37.199)] = 62.692 \\ x_{4} &= x_{3} + h = 3 + 1 = 4 \\ y_{4} &= y_{3} + \frac{h}{2} [f(x_{3}, y_{3}) + f(x_{4}, \tilde{y}_{4})] = 37.199 + \frac{1}{2} [4 e^{0.8 (3)} - 0.5 (37.199) + 4 e^{0.8 (4)} - 0.5 (62.692)] = 83.338 \end{align*}

Seudocódigo

xi = 0
yi = 2
xf = 4
n = 4

h = (xf - xi)/n
y = yi
x = xi

for i=1 to n do
    ya = y + h*(4*exp(0.8*x) - 0.5*y)
    xa = x + h
    y = y + (h/2)*(4*exp(0.8*x) - 0.5*y + 4*exp(0.8*xa) - 0.5*ya)
    x = xa
end for

Implementación


In [10]:
function g(x, y)
    4*exp(0.8*x) - 0.5*y
end


Out[10]:
g (generic function with 1 method)

In [11]:
function MétodoHeun(xi, yi, h)
    yf = yi + h*g(xi, yi)
    x = xi + h
    y = yi + (h/2)*(g(xi, yi) + g(x, yf))
    return x, y
end


Out[11]:
MétodoHeun (generic function with 1 method)

In [12]:
xi = 0
yi = 2
xf = 4
n = 50

h = (xf - xi) / n
y = yi
x = xi
@printf("%s \t %s \t %17s\n", "i", "x", "y")
@printf("%s \t %s \t %17s\n", 0, x, y)

for i= 1:n
    x, y = MétodoHeun(x, y, h)
    @printf("%d \t %0.15f \t %0.15f\n",i , x, y)
end


i 	 x 	                 y
0 	 0 	                 2
1 	 0.080000000000000 	 2.245774783801841
2 	 0.160000000000000 	 2.503340685158105
3 	 0.240000000000000 	 2.773651554228560
4 	 0.320000000000000 	 3.057717441178962
5 	 0.400000000000000 	 3.356608578790039
6 	 0.480000000000000 	 3.671459617772590
7 	 0.560000000000000 	 4.003474131902822
8 	 0.640000000000000 	 4.353929411206959
9 	 0.720000000000000 	 4.724181562613496
10 	 0.800000000000000 	 5.115670938759900
11 	 0.880000000000000 	 5.529927916993519
12 	 0.960000000000000 	 5.968579052049326
13 	 1.040000000000000 	 6.433353627425923
14 	 1.120000000000000 	 6.926090632122263
15 	 1.200000000000000 	 7.448746191147498
16 	 1.280000000000000 	 8.003401480082484
17 	 1.360000000000000 	 8.592271155961400
18 	 1.440000000000000 	 9.217712338863757
19 	 1.520000000000000 	 9.882234180869657
20 	 1.600000000000000 	 10.588508061443616
21 	 1.680000000000000 	 11.339378450884560
22 	 1.760000000000000 	 12.137874486222342
23 	 1.840000000000001 	 12.987222306865421
24 	 1.920000000000001 	 13.890858200422342
25 	 2.000000000000000 	 14.852442612443983
26 	 2.080000000000001 	 15.875875077377975
27 	 2.160000000000001 	 16.965310131805623
28 	 2.240000000000001 	 18.125174275060736
29 	 2.320000000000001 	 19.360184046625317
30 	 2.400000000000001 	 20.675365294276887
31 	 2.480000000000001 	 22.076073711844899
32 	 2.560000000000001 	 23.568016730639457
33 	 2.640000000000001 	 25.157276854165467
34 	 2.720000000000001 	 26.850336531652445
35 	 2.800000000000001 	 28.654104672238510
36 	 2.880000000000001 	 30.575944908372595
37 	 2.960000000000001 	 32.623705724169042
38 	 3.040000000000001 	 34.805752572093120
39 	 3.120000000000001 	 37.131002109505609
40 	 3.200000000000002 	 39.608958695283206
41 	 3.280000000000002 	 42.249753295994395
42 	 3.360000000000002 	 45.064184960985713
43 	 3.440000000000002 	 48.063765036261614
44 	 3.520000000000002 	 51.260764298265208
45 	 3.600000000000002 	 54.668263200633277
46 	 3.680000000000002 	 58.300205439756176
47 	 3.760000000000002 	 62.171455058573692
48 	 3.840000000000002 	 66.297857322537212
49 	 3.920000000000002 	 70.696303617126731
50 	 4.000000000000002 	 75.384800632790643

Implementación iterativa del corrector:


In [13]:
function MétodoHeunIterativo(xi, yi, h)
    es = 0.01
    y = yi + h*g(xi, yi)
    x = xi + h
    while true
        y_anterior = y
        y = yi + (h/2)*(g(xi, yi) + g(x, y_anterior))
        ea = ((y - y_anterior) / y) * 100
        if ea <= es
            break
        end
    end
    return x, y
end


Out[13]:
MétodoHeunIterativo (generic function with 1 method)

In [14]:
xi = 0
yi = 2
xf = 4
n = 50

h = (xf - xi) / n
y = yi
x = xi
@printf("%s \t %s \t %17s\n", "i", "x", "y")
@printf("%s \t %s \t %17s\n", 0, x, y)

for i= 1:n
    x, y = MétodoHeunIterativo(x, y, h)
    @printf("%d \t %0.15f \t %0.15f\n", i, x, y)
end


i 	 x 	                 y
0 	 0 	                 2
1 	 0.080000000000000 	 2.245659288125804
2 	 0.160000000000000 	 2.503104772258407
3 	 0.240000000000000 	 2.773289942176862
4 	 0.320000000000000 	 3.057224462431668
5 	 0.400000000000000 	 3.355978153450947
6 	 0.480000000000000 	 3.670685225242664
7 	 0.560000000000000 	 4.002548780800291
8 	 0.640000000000000 	 4.352845607433842
9 	 0.720000000000000 	 4.722931275436943
10 	 0.800000000000000 	 5.114245564768579
11 	 0.880000000000000 	 5.528318241780513
12 	 0.960000000000000 	 5.966775209463633
13 	 1.040000000000000 	 6.431345056224662
14 	 1.120000000000000 	 6.923866029844989
15 	 1.200000000000000 	 7.446293465022624
16 	 1.280000000000000 	 8.000707694763614
17 	 1.360000000000000 	 8.589322477878358
18 	 1.440000000000000 	 9.214493976959218
19 	 1.520000000000000 	 9.878730323477448
20 	 1.600000000000000 	 10.584701809048932
21 	 1.680000000000000 	 11.335251744489456
22 	 1.760000000000000 	 12.133408031021849
23 	 1.840000000000001 	 12.982395490920409
24 	 1.920000000000001 	 13.885649007994747
25 	 2.000000000000000 	 14.846827531638148
26 	 2.080000000000001 	 15.869829001708514
27 	 2.160000000000001 	 16.958806255287346
28 	 2.240000000000001 	 18.118183980389627
29 	 2.320000000000001 	 19.352676785991239
30 	 2.400000000000001 	 20.667308462318534
31 	 2.480000000000001 	 22.067432510225302
32 	 2.560000000000001 	 23.558754023686031
33 	 2.640000000000001 	 25.147353014981977
34 	 2.720000000000001 	 26.839709278071265
35 	 2.800000000000001 	 28.642728891939893
36 	 2.880000000000001 	 30.563772472453330
37 	 2.960000000000001 	 32.610685288395544
38 	 3.040000000000001 	 34.791829365023553
39 	 3.120000000000001 	 37.116117706611860
40 	 3.200000000000002 	 39.593050778146157
41 	 3.280000000000002 	 42.232755395584817
42 	 3.360000000000002 	 45.046026183977929
43 	 3.440000000000002 	 48.044369773257522
44 	 3.520000000000002 	 51.240051912732163
45 	 3.600000000000002 	 54.646147697280405
46 	 3.680000000000002 	 58.276595110989348
47 	 3.760000000000002 	 62.146252107579599
48 	 3.840000000000002 	 66.270957461451360
49 	 3.920000000000002 	 70.667595638637934
50 	 4.000000000000002 	 75.354165953425863