I am facing a problem to solve a set of differential equations using odeint. The problem is that the variables that My variables are interdependent. The part of the code that is returning me the error is:
def G(solution,time):
θ1_d, ϕ1_d, θ2_d, ϕ2_d, q_d = solution[0], solution[1], solution[2],solution[3], solution[4]
θ1, ϕ1, θ2, ϕ2, q = solution[5], solution[6], solution[7],solution[8], solution[9]
C_VL = 1/2 * q * C_L0
u = sqrt(1/(2*pi*St) * (1/(2*pi*St) - 2*l*θ1_d - h*θ2_d) + l**2 * (-2*θ1*ϕ1*θ1_d*ϕ1_d + θ1_d**2 + ϕ1_d**2) + l*h*(-θ1*ϕ1*θ1_d*ϕ2_d - θ2*ϕ2*θ2_d*ϕ1_d + θ1_d*θ2_d + ϕ1_d*ϕ2_d) + (h**2/4)*(-2*θ2*ϕ2*θ2_d*ϕ2_d + θ2_d**2 + ϕ2_d**2))
C_VX = (C_VL*2*pi*St*(l*ϕ1_d + (h/2)*ϕ2_d) + C_DM*(1 -2*pi*St*(l*θ1_d + (h/2)*θ2_d)))*u + α*C_VL**2*(1 - 2*pi*St*(l*θ1_d + (h/2)*θ2_d))*abs(1 - 2*pi*St*(l*θ1_d + (h/2)*θ2_d))
C_VY = (C_VL*(1 -2*pi*St*(l*θ1_d + (h/2)*θ2_d)) - C_DM*2*pi*St*(l*ϕ1_d + (h/2)*ϕ2_d))*u
θ1_dd = -h*θ2_dd/(2*l) - 2*ζ1*Ω1*θ1_d - ζ1*Ω1*h*θ2_d/l - Ω1**2*θ1
ϕ1_dd = -h*ϕ2_dd/(2*l) - 2*ζ1*Ω1*ϕ1_d - ζ1*Ω1*h*ϕ2_d/l - Ω1**2*ϕ1
θ2_dd = 3*C_VX/(4*pi**3*St**2*h*(m_r + C_a)) - 3*l*θ1_dd/(2*h) - 3*ζ2*Ω2*θ2_d/2 - 3*ζ2*Ω2*l*θ1_d/h +Ω2**2*θ2
ϕ2_dd = 3*C_VY/(4*pi**3*St**2*h*(m_r + C_a)) - 3*l*ϕ1_dd/(2*h) - 3*ζ2*Ω2*ϕ2_d/2 - 3*ζ2*Ω2*l*ϕ1_d/h +Ω2**2*ϕ2
q_dd = A*(l*ϕ1_dd + (h/2)*ϕ2_dd) - ε*(q**2 - 1)*q_d - q + κ*(l*θ1_dd + (h/2)*θ2_dd)*q
return np.array([θ1_dd, ϕ1_dd, θ2_dd, ϕ2_dd, q_dd, θ1_d, ϕ1_d, θ2_d, ϕ2_d, q_d])
solution=odeint(G,solution0,time)
The problem is that θ1_dd is a function of θ2_dd that is defined latter, and θ2_dd is a function of θ1_dd. So I cannot simply invert the order of the equations to solve that problem.
The error message is: UnboundLocalError: local variable 'θ2_dd' referenced before assignment
Any suggestion of how to solve that problem?