I am trying to solve the system of the second-order differential equations of the coupled oscillatory circuit using Runge-Kutta 4th order method in python.
And the initial conditions are:
Condition | Value |
---|---|
L_1 =L_2 | 2,5E-04 H |
C_1 | 2,5Е-11 F |
C_2 | 5,0Е-11 F |
C_св | 1,0Е-09 F |
I_10 | 0 A |
I_20 | 0 A |
q_10 | 2,5Е-10 C |
q_20 | 0 C |
t_0 | 0 s |
t_max | 3,0Е-04 s |
My python code is below:
import numpy as np
L = 2.5e-04
C = 1.0e-09
C_1 = 2.5e-11
C_2 = 5.0e-11
i_1_0 = 0
i_2_0 = 0
q_1_0 = 2.5e-10
q_2_0 = 0
t_0 = 0
t_max = 3.0e-04
h = (t_max - t_0) / 100
t = np.arange(t_0, t_max, h)
def f1(q_1, q_2):
return -q_1 / (L * C_1) + (q_2 - q_1) / (L * C)
def f2(q_1, q_2):
return -q_2 / (L * C_2) - (q_2 - q_1) / (L * C)
def runge_kutta(q_1, i_1, q_2, i_2, t, h):
q_1_list = [q_1]
i_1_list = [i_1]
q_2_list = [q_2]
i_2_list = [i_2]
for _ in t:
m_1_1 = h * i_1
k_1_1 = h * f1(q_1, q_2)
m_2_1 = h * i_2
k_2_1 = h * f2(q_1, q_2)
m_1_2 = h * (i_1 + 0.5 * k_1_1)
k_1_2 = h * f1(q_1 + 0.5 * m_1_1, q_2 + 0.5 * m_2_1)
m_2_2 = h * (i_2 + 0.5 * k_2_1)
k_2_2 = h * f2(q_1 + 0.5 * m_1_1, q_2 + 0.5 * m_2_1)
m_1_3 = h * (i_1 + 0.5 * k_1_2)
k_1_3 = h * f1(q_1 + 0.5 * m_1_2, q_2 + 0.5 * m_2_2)
m_2_3 = h * (i_2 + 0.5 * k_2_2)
k_2_3 = h * f2(q_1 + 0.5 * m_1_2, q_2 + 0.5 * m_2_2)
m_1_4 = h * (i_1 + k_1_3)
k_1_4 = h * f1(q_1 + m_1_3, q_2 + m_2_3)
m_2_4 = h * (i_2 + k_2_3)
k_2_4 = h * f2(q_1 + m_1_3, q_2 + m_2_3)
q_1 += (1.0 / 6.0) * (m_1_1 + 2 * m_1_2 + 2 * m_1_3 + m_1_4)
i_1 += (1.0 / 6.0) * (k_1_1 + 2 * k_1_2 + 2 * k_1_3 + k_1_4)
q_2 += (1.0 / 6.0) * (m_2_1 + 2 * m_2_2 + 2 * m_2_3 + m_2_4)
i_2 += (1.0 / 6.0) * (k_2_1 + 2 * k_2_2 + 2 * k_2_3 + k_2_4)
q_1_list.append(q_1)
i_1_list.append(i_1)
q_2_list.append(q_2)
i_2_list.append(i_2)
return q_1_list, i_1_list, q_2_list, i_2_list
q_1_points, i_1_points, q_2_points, i_2_points = runge_kutta(q_1_0, i_1_0, q_2_0, i_2_0, t, h)
print(q_1_points)
print(i_1_points)
print(q_2_points)
print(i_2_points)
The code is running but the results I get look suspicious. All quantities quickly acquire huge powers and go to NaN.