根据论文,他们告诉我可以使用RK第四顺序以数字方式解决它。
正如你所看到的,两个最后的方程是耦合的,我构造了一个矩阵(概率),它关联xn和yn,其中n = 1 ..(例如,N对数,这里N等于4): vector([x1,x2,...,xn,y1,y2,...,yn])'= Probability.dot(vector([x1,x2,...,xn,y1,y2,... ,yn])),其中素数是时间差异。但是在每个步骤的另一方面,我有额外的总和(un * xn和yn相同),这是我遇到的第一个问题,并且不知道如何处理它。
我写了一个代码和很多我无法管理的错误。
虽然我试图自己应对,但我会非常感谢它的任何帮助。
上面显示了我的代码:
导入库
import numpy as np
import math
import scipy.constants as sc
from scipy.sparse import diags
from scipy.integrate import ode
import matplotlib.pyplot as plt
from matplotlib import mlab
初始数据和常量
dimen_paramT0 = [0,0,0,0]
step = 0.00001
Mn = 1e-21 # mass of pairs
thau = 1e-15 # character time
wn_sqr = 1e-2 # to 10e-6
wn_prime = 3e-2 # to 10e-5
n = 4 #count of repetition; forinstance 4
gamma_n = 3e-9
Dn = 4e-2
an = 4.45
alphaPrime_n = 0.13
Volt = 0.4
hn = 0
hn_nInc = 1.276 #hn,n+1
hn_nDec = 1.276 #hn,n-1
Un = thau * alphaPrime_n / Mn
ksi_n = an * Un
Omega_n = 2 * Dn * an * (thau ** 2) / (Mn * Un)
*构造具有dif / eq /概率的关联矩阵*
k = np.array([hn_nInc*np.ones(n-1),hn*np.ones(n),hn_nDec*np.ones(n-1)])
offset = [-1,0,1]
Probability = diags(k,offset).toarray() # bn(tk)=xn(tk)+iyn(tk)
xt0_list = [0] * n
yt0_list = [0] * n
* dif的右侧。 EQ。 *
# dimen_param = [un,vn,zn,vzn] [tn]
# x_list = [x1,...,xn] [tn]
# y_list = [y1,...,yn] [tn]
def fun(dimen_param, x_list, y_list):
return dimen_param[1]
def fvn(dimen_param, x_list, y_list):
return -(x_list[len(x_list)-1]**2 + y_list[len(y_list)-1]**2)- wn_prime*dimen_param[1] + Omega_n * (1-np.e ** (-ksi_n * dimen_param[0]))*np.e ** (-ksi_n * dimen_param[0])
def fzn(dimen_param, x_list, y_list):
return dimen_param[3]
def fvzn(dimen_param, x_list, y_list):
return -wn_prime * dimen_param[3]-(wn_sqr ** 2) * dimen_param[2] - 1
def fxn(dimen_param, x_list, y_list):
return Probability.dot(y_list)
def fyn(dimen_param, x_list, y_list):
return -Probability.dot(x_list)
#xv = [dimen_param, x_list, y_list]
def f(xv):
k_d = xv[0:4]
k_x = xv[4:4+len(xt0_list)]
k_y = xv[4+len(xt0_list):4+len(xt0_list)+len(yt0_list)]
return ([fun(k_d, k_x, k_y),fvn(k_d, k_x, k_y),fzn(k_d, k_x, k_y),fvzn(k_d, k_x, k_y),fxn(k_d, k_x, k_y),fyn(k_d, k_x, k_y)])
*实现龙格 - 库塔四阶法*
def RK4(f, dimen_paramT0, xt0_list, yt0_list):
T = np.linspace(0, 1. / step, 1. / step +1)
xvinit = np.concatenate([dimen_paramT0, xt0_list, yt0_list])
xv = np.zeros( (len(T), len(xvinit)) )
xv[0] = xvinit
for i in range(int(1. / step)):
k1 = f(xv[i])
k2 = f(xv[i] + step/2.0*k1)
k3 = f(xv[i] + step/2.0*k2)
k4 = f(xv[i] + step*k3)
xv[i+1] = xv[i] + step/6.0 *( k1 + 2*k2 + 2*k3 + k4)
return T, xv
*正在运行*
print RK4(f, dimen_paramT0, xt0_list, yt0_list)
此刻的问题是:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-104-be8ed2e50d37> in <module>()
----> 1 RK4(f, dimen_paramT0, xt0_list, yt0_list)
<ipython-input-103-8c48cf5efe73> in RK4(f, dimen_paramT0, xt0_list, yt0_list)
7 for i in range(int(1. / step)):
8 k1 = f(xv[i])
----> 9 k2 = f(xv[i] + step/2.0*k1)
10 k3 = f(xv[i] + step/2.0*k2)
11 k4 = f(xv[i] + step*k3)
TypeError: can't multiply sequence by non-int of type 'float'
答案 0 :(得分:3)
k1
是一个python list
,其中multiply表示“重复”,所以
[1,2] * 3 == [1,2,1,2,1,2]
显然这对花车来说没有意义
[1,2,3]*2.0
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-118-7d3451361a53> in <module>()
----> 1 [1,2,3]*2.0
TypeError: can't multiply sequence by non-int of type 'float'
您希望numpy.ndarray
的矢量行为在哪里
np.array([1,2])*2.0 == np.array([2.0, 4.0])
因此请确保f
的return语句为:
return np.asarray([
fun(k_d, k_x, k_y),
fvn(k_d, k_x, k_y),
fzn(k_d, k_x, k_y),
fvzn(k_d, k_x, k_y),
fxn(k_d, k_x, k_y),
fyn(k_d, k_x, k_y)
])
对于记录,scipy
已经有RK4 solver,无需自行实施。