<jupyter_start><jupyter_text><NAME> (RK) fourth-order method for solving ordinary differential equations RK4 can be used to find solutions for differential equations such as $\frac{dy}{dx} = x^2 - \frac{1}{y} $ with initial condition $y = y_0$ at $x = x_0$. The point at which we wish to know the curve between the initial values and those of $x_n$ and $y_n$. And where n represent the number of step taken between $x_0$ to $x_n$  Function to be solved<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
def f(x,y)->float: # function to be solved
    '''
    this corresponds to the differential
    equation dy/dx = x^2 - 1/y
    '''
    return x**2 - 1/y
# or
# f = lambda x: x+y<jupyter_output><empty_output><jupyter_text>RK-4 Method<jupyter_code>def rk4(f,x0,y0,xn,n):
    '''
       f = function to evalutate
       (x0,y0) = initial conditions
       x0 = initial x coordinate
       y0 = initial y coordinate
       xn = evaluation point to find yn
       n = number of steps to estimate xn
    '''
    del_x=(xn-x0)/n  # Calculating step size
    coordinates=[]
    for i in range(n):
        k1 = del_x*(f(x0, y0))
        k2 = del_x*(f((x0+del_x/2),(y0+k1/2)))
        k3 = del_x*(f((x0+del_x/2),(y0+k2/2)))
        k4 = del_x*(f((x0+del_x),(y0+k3)))
        k = (k1+2*k2+2*k3+k4)/6
        yn = y0+k
        y0 = yn
        x0 = x0+del_x
        coordinates.append([x0,y0])
    return coordinates
rk4(f,1,2,4,10)
rk4_array = rk4(f,1,2,4,10)
xx=[]
yy=[]
for i in rk4_array:
    xx.append(i[0])
    yy.append(i[1])
plt.plot(xx,yy)
plt.xlabel('x')
plt.ylabel('y')
plt.show()
def f(x,y):
    '''
    dy/dt=exp(x**2)'''
    return np.exp(x)
rk4_array = rk4(f,1,2,4,10)
xx=[]
yy=[]
for i in rk4_array:
    xx.append(i[0])
    yy.append(i[1])
plt.plot(xx,yy)
plt.xlabel('x')
plt.ylabel('y')
plt.show()<jupyter_output><empty_output><jupyter_text>Plot Solutions Curves With Differnt Values of n to show Effect of The Number of Steps Taken<jupyter_code>yy1=[]
xx1=[]
yy2=[]
xx2=[]
yy3=[]
xx3=[]

steps5 = rk4(f,0,0,10,5)
for i in steps5:
    xx1.append(i[0])
    yy1.append(i[1])

steps10 = rk4(f,0,0,10,10)
for i in steps10:
    xx2.append(i[0])
    yy2.append(i[1])

steps50 = rk4(f,0,0,10,100)
for i in steps50:
    xx3.append(i[0])
    yy3.append(i[1])



# plot results
plt.plot(xx1,yy1,'r:.',linewidth=2,label='n=3')
plt.plot(xx2,yy2,'b:',linewidth=2,label='n=10')
plt.plot(xx3,yy3,'g:',linewidth=2,label='n=100')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17348154029376736, 'fraction_numerical': 0.05637157602223104, 'mean_word_length': 3.5652173913043477, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 15, 'https://': 0, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '208032', 'license': 'MIT', 'n_tokens_mistral': 1144, 'n_tokens_neox': 1091, 'n_words': 246, 'path': 'Mathematics/Numerical Methods/Runge-Kutta Method.ipynb', 'repo_name': 'okara83/Becoming-a-Data-Scientist', 'size': '2524'}