4 미분 방정식의 결합 시스템-Python
나는 그림에서 4 개의 미분 방정식의 결합 시스템을 얻었습니다. 나는 4 개의 함수 (xG; yG; 감마; 베타)와 그 파생물을 가지고 있습니다. 그것들은 모두 동일한 독립 변수 t의 기능입니다.
나는 odeint로 그것을 해결하려고 노력하고 있습니다. 문제는 그렇게하기 위해서는 각각의 2 차 도함수가 다른 2 차 도함수에 의존하지 않는 방식으로 시스템을 표현해야한다고 생각합니다. 여기에는 어딘가에 오류가 발생할 수있는 많은 수학이 포함됩니다 (내가 시도했습니다!).
내가 어떻게 할 수 있는지 아십니까?
- 이 미분 방정식을있는 그대로 풀나요?
- 또는 파이썬을 통해 2 차 파생물을 분리 할 수 있습니까?
내 테스트 코드를 첨부하고 있습니다.
감사

import numpy
import math
from numpy import loadtxt
from pylab import figure, savefig
import matplotlib.pyplot as plt
# Use ODEINT to solve the differential equations defined by the vector field
from scipy.integrate import odeint
def vectorfield(w, t, p):
"""
Defines the differential equations for the coupled system.
Arguments:
w : vector of the state variables:
w = [Xg, Xg1 Yg, Yg1, Gamma, Gamma1, Beta, Beta1]
t : time
p : vector of the parameters:
p = [m, rAG, Ig,lcavo]
"""
#Xg is position ; Xg1 is the first derivative ; Xg2 is the second derivative (the same for the other functions)
Xg, Xg1, Yg, Yg1, Gamma, Gamma1, Beta, Beta1 = w
Xg2=-(Ig*Gamma2*math.cos(Beta))/(rAG*m*(-math.cos(Gamma)*math.sin(Beta)+math.sin(Gamma)*math.cos(Beta)))
Yg2=-(Ig*Gamma2*math.sin(Beta))/(rAG*m*(-math.cos(Gamma)*math.sin(Beta)+math.sin(Gamma)*math.cos(Beta)))-9.81
Gamma2=((Beta2*lcavo*math.sin(Beta))+(Beta1**2*lcavo*math.cos(Beta))+(Xg2)-(Gamma1**2*rAG*math.cos(Gamma)))/(rAG*math.sin(Gamma))
Beta2=((Yg2)+(Gamma2*rAG*math.cos(Gamma))-(Gamma1**2*rAG*math.sin(Gamma))+(Beta1**2*lcavo*math.sin(Beta)))/(lcavo*math.cos(Beta))
m, rAG, Ig,lcavo, Xg2, Yg2, Gamma2, Beta2 = p
# Create f = (Xg', Xg1' Yg', Yg1', Gamma', Gamma1', Beta', Beta1'):
f = [Xg1,
Xg2,
Yg1,
Yg2,
Gamma1,
Gamma2,
Beta1,
Beta2]
return f
# Parameter values
m=2.722*10**4
rAG=2.622
Ig=3.582*10**5
lcavo=4
# Initial conditions
Xg = 0.0
Xg1 = 0
Yg = 0.0
Yg1 = 0.0
Gamma=-2.52
Gamma1=0
Beta=4.7
Beta1=0
# ODE solver parameters
abserr = 1.0e-8
relerr = 1.0e-6
stoptime = 5.0
numpoints = 250
#create the time values
t = [stoptime * float(i) / (numpoints - 1) for i in range(numpoints)]
Deltat=t[1]
# Pack up the parameters and initial conditions:
p = [m, rAG, Ig,lcavo, Xg2, Yg2, Gamma2, Beta2]
w0 = [Xg, Xg1, Yg, Yg1, Gamma, Gamma1, Beta, Beta1]
# Call the ODE solver.
wsol = odeint(vectorfield, w0, t, args=(p,),
atol=abserr, rtol=relerr)
답변
모든 2 차 도함수를 1 차 1로 다시 작성하고 8 개의 ODE를 함께 풀어야합니다.

그런 다음 모든 파생 상품에 대한 초기 조건이 필요하지만 이미 가지고있는 것 같습니다. 참고로 코드가 실행되지 않습니다 ( line 71: NameError: name 'Xg2' is not defined
). 확인하시기 바랍니다.
또한 자세한 내용은 숫자로 2 차 ODE 풀기를 참조하십시오 .
편집 # 1 : 첫 번째 단계에서 방정식 시스템을 분리해야합니다. 수동으로 해결할 수 있지만 권장하지 않으므로 sympy
모듈을 사용하겠습니다 .
import sympy as sm
from sympy import symbols
# define symbols. I assume all the variables are real-valued, this helps the solver. If not, I believe the result will be the same, but just calculated slower
Ig, gamma, gamma1, gamma2, r, m, beta, beta1, beta2, xg2, yg2, g, l = symbols('I_g, gamma, gamma1, gamma2, r, m, beta, beta1, beta2, xg2, yg2, g, l', real = True)
# define left hand sides as expressions
# 2nd deriv of gamma
g2 = (beta2 * l * sm.sin(beta) + beta1**2 *l *sm.cos(beta) + xg2 - gamma1**2 *r * sm.cos(gamma))/(r*sm.sin(gamma))
# 2nd deriv of beta
b2 = (yg2 + gamma2 * r * sm.cos(gamma) - gamma1**2 *r * sm.sin(gamma) + beta1**2 *l *sm.sin(beta))/(l*sm.cos(beta))
# 2nd deriv of xg
x2 = -Ig*gamma2*sm.cos(beta)/(r*m*(-sm.sin(beta)*sm.cos(gamma) + sm.sin(gamma)*sm.cos(beta)))
# 2nd deriv of yg
y2 = -Ig*gamma2*sm.sin(beta)/(r*m*(-sm.sin(beta)*sm.cos(gamma) + sm.sin(gamma)*sm.cos(beta))) - g
# now let's solve the system of four equations to decouple second order derivs
# gamma2 - g2 means "gamma2 - g2 = 0" to the solver. The g2 contains gamma2 by definition
# one could define these equations the other way, but I prefer this form
result = sm.solve([gamma2-g2,beta2-b2,xg2-x2,yg2-y2],
# this line tells the solver what variables we want to solve to
[gamma2,beta2,xg2,yg2] )
# print the result
# note that it is long and ugly, but you can copy-paste it as python code
for res in result:
print(res, result[res])
이제 우리는 모든 2 차 파생물이 분리되었습니다. 예를 들어에 대한 표현식 beta2
은 다음과 같습니다.

그래서 그것은 (및 다른 모든 2 차 파생물도) 형식을 갖습니다.

xg
또는에 대한 종속성이 없습니다 yg
.
하자 두 개의 새로운 변수를 도입 b
하고 k
:

다음은


풀어야 할 ODE의 전체 시스템은 다음과 같습니다.

이제 모든 ODE는 도함수가 아닌 4 개의 변수에 종속됩니다. 때문에 또한 xg
하고이 yg
퇴화되는 대신하지만 8의 (6) 식도 있는데, 하나는 동일한 방식으로 두 방정식을 다시 작성할 수 gamma
및 beta
8 개 방정식의 전체 시스템을 획득하고, 함께 통합.