-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path4th order runge-kutta.py
29 lines (25 loc) · 1.63 KB
/
4th order runge-kutta.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from sympy import *
def runge_method(function_expression, x_0, y_0, x, h):
first_variable = var("x")
second_variable = var("y")
function = sympify(function_expression)
iteration_count = int((x - x_0) / h)
y = y_0
for i in range(1, iteration_count + 1):
k1 = h * function.subs([(first_variable, x_0), (second_variable, y)])
k2 = h * function.subs([(first_variable, x_0 + 0.5 * h), (second_variable, y + 0.5 + k1)])
k3 = h * function.subs([(first_variable, x_0 + 0.5 * h), (second_variable, y + 0.5 + k2)])
k4 = h * function.subs([(first_variable, x_0 + h), (second_variable, y + k3)])
y = y + (1.0 / 6.0) * (k1 + 2 * k2 + 2 * k3 + k4)
x_0 = x_0 + h
return y
'''standardized differential equation means that the derivative section stands on the left side of the equation and the
other terms of the equation stands on the right side of the equation. for example: dy/dx = 2 * x ** 3 + 4 * y'''
''' REMARK: just enter the right side of the equation as an input. as in the example above you just need to enter
2 * x ** 3 + 4 * y to the input.'''
given_equation = input("please enter right side of an standardized differential equation: ")
step_length = float(input("please enter the step length(h): "))
initial_x_value = float(input("please enter an initial x value:"))
initial_y_value = float(input("please enter an initial value y at initial x value: "))
given_x = float(input("please enter a value x at which you want to evaluate y: "))
print("evaluated value of y is: ", runge_method(given_equation, initial_x_value, initial_y_value, given_x, step_length))