Systems of differential equations in Python

1

I have problems solving the following system of equations:

import numpy as np
import scipy as sp
from scipy.integrate import odeint
import matplotlib.pyplot as plt

R1=8
R2=3
L1=1 
L2=1 

def RLC(I,t):
  di1_t=(-(R1+R2)/L2)*I[0] + (R2/L2)*I[1] + (100*np.sin(t))/L2 + di2_t

  di2_t=(R2/L1)*I[0] - (R2/L1)*I[1]  

  return di1_t,di2_t


i0 = 0,0

t = np.linspace(0,2,100)

sol=odeint(RLC, i0, t)

Because the equations are co-dependent, what would be a possible solution for it?

    
asked by juan lopez 23.11.2017 в 03:33
source

1 answer

1

You do not have to add anything else, the problem is that you have not separated the equations correctly, for example a serious solution to replace di2_t in di1_t :

def RLC(I,t): 
    di1_t=(-(R1+R2)/L2)*I[0] + (R2/L2)*I[1] + (100*np.sin(t))/L2 + (R2/L1)*I[0] - (R2/L1)*I[1]
    di2_t=(R2/L1)*I[0] - (R2/L1)*I[1] 
    return di1_t, di2_t

Or simply change the order of the instructions:

def RLC(I,t):
    di2_t=(R2/L1)*I[0] - (R2/L1)*I[1]  
    di1_t=(-(R1+R2)/L2)*I[0] + (R2/L2)*I[1] + (100*np.sin(t))/L2 + di2_t
    return di1_t, di2_t

In addition to making your code more legible you can unpack the tuple using i1, i2 = I and replacing I[0] e I[1] with i1 e i2 , respectively. Executing the following code we obtain:

R1=8
R2=3
L1=1 
L2=1 

def RLC(I,t):
    i1, i2 = I
    di2_t=(R2/L1)*i1 - (R2/L1)*i2  
    di1_t=(-(R1+R2)/L2)*i1 + (R2/L2)*i2 + (100*np.sin(t))/L2 + di2_t
    return di1_t, di2_t

i0 = 0,0

t = np.linspace(0,2,100)

sol=odeint(RLC, i0, t)
plt.plot(t, sol[:, 0], label="i1_t")
plt.plot(t, sol[:, 1], label="i2_t")
plt.legend()
plt.show()

    
answered by 23.11.2017 в 04:00