使用SageMath的DE系统

时间:2020-06-24 09:59:19

标签: python python-3.x sage

我正在尝试使用SageMath求解一阶非线性微分方程组。它正在为特定的初始条件返回所需的输出,但是我无法模拟数据数组。这是我的代码段

var('a x1 x2 x3 x10 x20 x30')
x10 = -8 ; x20 = 5  ; x30 = 2.6    

de_x1 = (diff(x1,a) == -4 -3*x1+2*x3 -x1*x3 +x1*x1)
de_x2 = (diff(x2,a) == 8 - 4 *x3 + x2*x1 - 2 *x2*x3 + 4*x2)
de_x3 = (diff(x1,a) == -8 +8*x3 -2*x3*x3 )
end_points = 5000
stepsize = 1.0
steps = end_points/stepsize
ics = [0,x10,x20,x30]

des = [de_x1.rhs(), de_x2.rhs(), de_x3.rhs()]
sol = desolve_system_rk4(des,[x1,x2,x3],ics,ivar=a,end_points=end_points,step=stepsize)
sol_x1=[]; sol_x2=[]; sol_x3=[]

for i in range(steps):
    sol_x1.append(sol[i][0])
    sol_x2.append(sol[i][1])
    sol_x3.append(sol[i][2])
a = plot([],figsize=[6,4])
a.axes_labels(['x1','x2'])
show(a)

在执行for循环时,我收到一条错误消息

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-3ed005c9d4d9> in <module>()
     14 sol_x1=[]; sol_x2=[]; sol_x3=[]
     15 
---> 16 for i in range(steps):
     17     sol_x1.append(sol[i][Integer(0)])

TypeError: 'sage.rings.real_mpfr.RealNumber' object cannot be interpreted as an integer

1 个答案:

答案 0 :(得分:0)

问题来自您传递给range的参数不是Python int的事实。计算时将其转换为int:

steps = int(end_points/stepsize)

或将其传递给range时:

for i in range(int(steps)):