<jupyter_start><jupyter_text>EDO2  2nd Order Runge-Kutta Method [References](https://rosettacode.org/wiki/Runge-Kutta_methodPython)  Exemples:  Alternative solution<jupyter_code>def rk4(f, x0, y0, x1, n):
    vx = [0] * (n + 1)
    vy = [0] * (n + 1)
    h = (x1 - x0) / float(n)
    vx[0] = x = x0
    vy[0] = y = y0
    for i in range(1, n + 1):
        k1 = h * f(x, y)
        k2 = h * f(x + 0.5 * h, y + 0.5 * k1)
        k3 = h * f(x + 0.5 * h, y + 0.5 * k2)
        k4 = h * f(x + h, y + k3)
        vx[i] = x = x0 + i * h
        vy[i] = y = y + (k1 + k2 + k2 + k3 + k3 + k4) / 6
    return vx, vy
 
def f(x, y):
    return x * sqrt(y)
 
vx, vy = rk4(f, 0, 1, 10, 100)
for x, y in list(zip(vx, vy))[::10]:
    print("%4.1f %10.5f %+12.4e" % (x, y, y - (4 + x * x)**2 / 16))<jupyter_output>0.0    1.00000  +0.0000e+00
 1.0    1.56250  -1.4572e-07
 2.0    4.00000  -9.1948e-07
 3.0   10.56250  -2.9096e-06
 4.0   24.99999  -6.2349e-06
 5.0   52.56249  -1.0820e-05
 6.0   99.99998  -1.6595e-05
 7.0  175.56248  -2.3518e-05
 8.0  288.99997  -3.1565e-05
 9.0  451.56246  -4.0723e-05
10.0  675.99995  -5.0983e-05<jupyter_text>Using lambda<jupyter_code>def RK4(f):
    return lambda t, y, dt: (
            lambda dy1: (
            lambda dy2: (
            lambda dy3: (
            lambda dy4: (dy1 + 2*dy2 + 2*dy3 + dy4)/6
            )( dt * f( t + dt  , y + dy3   ) )
    )( dt * f( t + dt/2, y + dy2/2 ) )
    )( dt * f( t + dt/2, y + dy1/2 ) )
    )( dt * f( t       , y         ) )
 
def theory(t): return (t**2 + 4)**2 /16
 
from math import sqrt
dy = RK4(lambda t, y: t*sqrt(y))
 
t, y, dt = 0., 1., .1
while t <= 10:
    if abs(round(t) - t) < 1e-5:
        print("y(%2.1f)\t= %4.6f \t error: %4.6g" % ( t, y, abs(y - theory(t))))
    t, y = t + dt, y + dy( t, y, dt )
def rk4(f, x0, y0, h, n):
    vx = [0] * (n + 1)
    vy = [0] * (n + 1)
    vx[0] = x = x0
    vy[0] = y = y0
    for i in range(1, n + 1):
        k1 = h * f(x, y)
        k2 = h * f(x + 0.5 * h, y + 0.5 * k1)
        k3 = h * f(x + 0.5 * h, y + 0.5 * k2)
        k4 = h * f(x + h, y + k3)
        vx[i] = x = x0 + i * h
        vy[i] = y = y + (k1 + k2 + k2 + k3 + k3 + k4) / 6
    return vx, vy<jupyter_output><empty_output><jupyter_text>Slides examples<jupyter_code>def rk2(f, x0, y0, h, n):
    vx = [0] * (n + 1)
    vy = [0] * (n + 1)
    vx[0] = x = x0
    vy[0] = y = y0
    for i in range(1, n + 1):
        k1 = decimal(f(x, y))
        k2 = decimal(f(x + h, y + h * k1))
        print('k1:',k1,'k2:',k2)
        print('',y,'+',h/2,'*','(',k1,'+',k2,')',end='')
        vx[i] = x = decimal(x0 + i * h)
        vy[i] = y = decimal(y + h/2 * (k1 + k2),7)
        print(' =',vy[i])
    return vx, vy
def f(x, y):
    return y
 
vx, vy = rk2(f, 0, 1, 0.5, 2)
for x, y in list(zip(vx, vy))[::1]:
    print("%4.2f %10.5f" % (x, y))
def f(x, y):
    return 2+x-y
 
vx, vy = rk2(f, 0, 2, 0.1, 3)
for x, y in list(zip(vx, vy))[::1]:
    print("%4.2f %10.7f" % (x, y))
def f(x, y):
    return y-2-x
 
vx, vy = rk2(f, 0, 1, 0.5, 2)
for x, y in list(zip(vx, vy))[::1]:
    print("%4.2f %10.5f" % (x, y))<jupyter_output>k1: -1.0 k2: -2.0
 1 + 0.25 * ( -1.0 + -2.0 ) = 0.25
k1: -2.25 k2: -3.875
 0.25 + 0.25 * ( -2.25 + -3.875 ) = -1.28125
0.00    1.00000
0.50    0.25000
1.00   -1.28125<jupyter_text>Class test<jupyter_code>def f(x, y):
    return 2*x-y+3
 
vx, vy = rk2(f, 0, 1, 0.25, 4)
for x, y in list(zip(vx, vy))[::1]:
    print("%4.2f %10.5f" % (x, y))
def f(x, y):
    return 5*x-y+6
 
vx, vy = rk2(f, 0, 1, 0.25, 4)
for x, y in list(zip(vx, vy))[::1]:
    print("%4.2f %10.5f" % (x, y))<jupyter_output>k1: 5.0 k2: 5.0
 1 + 0.125 * ( 5.0 + 5.0 ) = 2.25
k1: 5.0 k2: 5.0
 2.25 + 0.125 * ( 5.0 + 5.0 ) = 3.5
k1: 5.0 k2: 5.0
 3.5 + 0.125 * ( 5.0 + 5.0 ) = 4.75
k1: 5.0 k2: 5.0
 4.75 + 0.125 * ( 5.0 + 5.0 ) = 6.0
0.00    1.00000
0.25    2.25000
0.50    3.50000
0.75    4.75000
1.00    6.00000
{'fraction_non_alphanumeric': 0.22936016511867904, 'fraction_numerical': 0.15557275541795665, 'mean_word_length': 1.7692857142857144, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 14, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '603686', 'license': 'MIT', 'n_tokens_mistral': 2355, 'n_tokens_neox': 2056, 'n_words': 531, 'path': 'module_3/edo2.ipynb', 'repo_name': 'arthurTemporim/numerical_methods', 'size': '3876'}