Python:ValueError:使用序列设置数组元素。解决简单ode时出错
我已经编写了一个简单的python代码来模拟自适应模糊滑模控制。我的问题是无论我做什么,我都会犯这个错误。我尝试将列表转换为数组等,但没有效果。请帮我解决这个问题。我想做的是,我创建了一个返回53个元素的函数,然后scipy的ode方法将其集成 这是我的密码。PFA-Python:ValueError:使用序列设置数组元素。解决简单ode时出错,python,ode,differential-equations,Python,Ode,Differential Equations,我已经编写了一个简单的python代码来模拟自适应模糊滑模控制。我的问题是无论我做什么,我都会犯这个错误。我尝试将列表转换为数组等,但没有效果。请帮我解决这个问题。我想做的是,我创建了一个返回53个元素的函数,然后scipy的ode方法将其集成 这是我的密码。PFA- import numpy as np import matplotlib.pyplot as plt from scipy.integrate import ode from numpy import exp,sin,cos,co
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import ode
from numpy import exp,sin,cos,concatenate,transpose,pi,tanh
from mpl_toolkits.mplot3d import Axes3D
c,eta,k,gamma1,gamma2,gamma3,gamma4,gamma5,gamma21,gamma22,gamma23,gamma24,gamma25=2,1.5,2,1,2,3,4,5,6,7,8,9,10
x=np.linspace(0,10,100)
xdot=np.linspace(0,10,100)
def member(x,z):
return exp(-((x+z)/(pi/24))**2)
m=[]
b=[pi/6,pi/12,0,-pi/12,-pi/6]
def f(t,Y):
x1,x2,x3,eta1,eta2,eta3,eta4,eta5,eta6,eta7,eta8,eta9,eta10,eta11,eta12,eta13,eta14,eta15,eta16,eta17,eta18,eta19,eta20,eta21,eta22,eta23,eta24,eta25,geta1,geta2,geta3,geta4,geta5,geta6,geta7,geta8,geta9,geta10,geta11,geta12,geta13,geta14,geta15,geta16,geta17,geta18,geta19,geta20,geta21,geta22,geta23,geta24,geta25=Y
eta=np.array([eta1,eta2,eta3,eta4,eta5,eta6,eta7,eta8,eta9,eta10,eta11,eta12,eta13,eta14,eta15,eta16,eta17,eta18,eta19,eta20,eta21,eta22,eta23,eta24,eta25])
geta=np.array([geta1,geta2,geta3,geta4,geta5,geta6,geta7,geta8,geta9,geta10,geta11,geta12,geta13,geta14,geta15,geta16,geta17,geta18,geta19,geta20,geta21,geta22,geta23,geta24,geta25])
m,z=[],[]
for i in range(5):
for j in range(5):
m.append(member(x1,b[i])*member(x2,b[j]))
for i in range(25):
z.append(m[i]/sum(m))
z=np.array(z)
e,de=x1-sin(t),x2-cos(t)
s=de+c**2*x3+2*c*e
f=np.dot(eta,z)
g=np.dot(geta,z)
u=(1./g)*(-f-c**2*e-2*c*de-sin(t)-eta*tanh(s)-k*s)
x1dot=x2
x2dot=f+g*u
x3dot=e
eta1dot,eta2dot,eta3dot,eta4dot,eta5dot,eta6dot,eta7dot,eta8dot,eta9dot,eta10dot,eta11dot,eta12dot,eta13dot,eta14dot,eta15dot,eta16dot,eta17dot,eta18dot,eta19dot,eta20dot,eta21dot,eta22dot,eta23dot,eta24dot,eta25dot,geta1dot,geta2dot,geta3dot,geta4dot,geta5dot,geta6dot,geta7dot,geta8dot,geta9dot,geta10dot,geta11dot,geta12dot,geta13dot,geta14dot,geta15dot,geta16dot,geta17dot,geta18dot,geta19dot,geta20dot,geta21dot,geta22dot,geta23dot,geta24dot,geta25dot=-2*s*z[0],-2*s*z[1],-2*s*z[2],-2*s*z[3],-2*s*z[4],-2*s*z[5],-2*s*z[6],-2*s*z[7],-2*s*z[8],-2*s*z[9],-2*s*z[10],-2*s*z[11],-2*s*z[12],-2*s*z[13],-2*s*z[14],-2*s*z[15],-2*s*z[16],-2*s*z[17],-2*s*z[18],-2*s*z[19],-2*s*z[20],-2*s*z[21],-2*s*z[22],-2*s*z[23],-2*s*z[24],-3*s*z[0]*u,-3*s*z[1]*u,-3*s*z[2]*u,-3*s*z[3]*u,-3*s*z[4]*u,-3*s*z[5]*u,-3*s*z[6]*u,-3*s*z[7]*u,-3*s*z[8]*u,-3*s*z[9]*u,-3*s*z[10]*u,-3*s*z[11]*u,-3*s*z[12]*u,-3*s*z[13]*u,-3*s*z[14]*u,-3*s*z[15]*u,-3*s*z[16]*u,-3*s*z[17]*u,-3*s*z[18]*u,-3*s*z[19]*u,-3*s*z[20]*u,-3*s*z[21]*u,-3*s*z[22]*u,-3*s*z[23]*u,-3*s*z[24]*u
return[x1dot,x2dot,x3dot,eta1dot,eta2dot,eta3dot,eta4dot,eta5dot,eta6dot,eta7dot,eta8dot,eta9dot,eta10dot,eta11dot,eta12dot,eta13dot,eta14dot,eta15dot,eta16dot,eta17dot,eta18dot,eta19dot,eta20dot,eta21dot,eta22dot,eta23dot,eta24dot,eta25dot,geta1dot,geta2dot,geta3dot,geta4dot,geta5dot,geta6dot,geta7dot,geta8dot,geta9dot,geta10dot,geta11dot,geta12dot,geta13dot,geta14dot,geta15dot,geta16dot,geta17dot,geta18dot,geta19dot,geta20dot,geta21dot,geta22dot,geta23dot,geta24dot,geta25dot]
Y,t=[],[]
for i in range(50):
Y.append(0.1)
#Y=np.array(Y)
y0=[0,0,0]
y0=y0+Y
t0=0
def int_amar():
t1,dt=10,.01
x=[]
for i in range(53):
x.append([])
#import pdb; pdb.set_trace()
odee=ode(f).set_integrator('dopri5',method='bdf',nsteps=1e6)
odee.set_initial_value(y0,t0)
while odee.successful() and odee.t<t1:
odee.integrate(odee.t+dt)
for i in range(53):
x[i].append(odee.y[i])
t.append(odee.t)
#for i in range(53):
# x[i]=np.array(x[i])
print(x1.shape)
return x
def main():
fun=int_amar()
Z,f,g,m=[[],[],[],[],[]],[],[],[]
for i in range(5):
for j in range(5):
m.append(member(fun[0],b[i])*member(fun[1],b[j]))
for i in range(25):
Z[i].append(m[i]/sum(m))
zetta1,zetta2,zetta3,zetta4,zetta5,zetta6,zetta7,zetta8,zetta9,zetta10,zetta11,zetta12,zetta13,zetta14,zetta15,zetta16,zetta17,zetta18,zetta19,zetta20,zetta21,zetta22,zetta23,zetta24,zetta25=Z[0],Z[1],Z[2],Z[3],Z[4],Z[5],Z[6],Z[7],Z[8],Z[9],Z[10],Z[11],Z[12],Z[13],Z[14],Z[15],Z[16],Z[17],Z[18],Z[19],Z[20],Z[21],Z[22],Z[23],Z[24],Z[25]
e=fun[0]-sin(t)
s=fun[1]+c**2*fun[2]+2*c*e
for i in range(len(fun[2])):
f.append(np.dot(np.array([[fun[3][i],fun[4][i],fun[5][i],fun[6][i],fun[7][i],fun[8][i],fun[9][i],fun[10][i],fun[11][i],fun[12][i],fun[13][i],fun[14][i],fun[15][i],fun[16][i],fun[17][i],fun[18][i],fun[19][i],fun[20][i],fun[21][i],fun[22][i],fun[23][i],fun[24][i],fun[25][i],fun[26][i],fun[27][i]]]),np.array([[zetta1[i]],[zetta2[i]],[zetta3[i]],[zetta4[i]],[zetta5[i]],[zetta6[i]],[zetta7[i]],[zetta8[i]],[zetta9[i]],[zetta10[i]],[zetta11[i]],[zetta12[i]],[zetta13[i]],[zetta14[i]],[zetta15[i]],[zetta16[i]],[zetta17[i]],[zetta18[i]],[zetta19[i]],[zetta20[i]],[zetta21[i]],[zetta22[i]],[zetta23[i]],[zetta24[i]],[zetta25[i]]])))
g.append(np.dot(np.array([[fun[28][i],fun[29][i],fun[30][i],fun[31][i],fun[32][i],fun[33][i],fun[34][i],fun[35][i],fun[36][i],fun[37][i],fun[38][i],fun[39][i],fun[40][i],fun[41][i],fun[42][i],fun[43][i],fun[44][i],fun[45][i],fun[46][i],fun[47][i],fun[48][i],fun[49][i],fun[50][i],fun[51][i],fun[52][i]]]),np.array([[zetta1[i]],[zetta2[i]],[zetta3[i]],[zetta4[i]],[zetta5[i]],[zetta6[i]],[zetta7[i]],[zetta8[i]],[zetta9[i]],[zetta10[i]],[zetta11[i]],[zetta12[i]],[zetta13[i]],[zetta14[i]],[zetta15[i]],[zetta16[i]],[zetta17[i]],[zetta18[i]],[zetta19[i]],[zetta20[i]],[zetta21[i]],[zetta22[i]],[zetta23[i]],[zetta24[i]],[zetta25[i]]])))
f,g=np.array(f),np.array(g)
u=(1./g)*(-f-c*e-sin(t)-eta*tanh(s)-k*s)
print(u.shape)
for i in range(12):
plt.subplot(3,4,i+1)
plt.plot(t,fun[i])
#plt.figure(2)
#plt.plot(t,u)
plt.show()
if __name__=='__main__':
main()
将numpy导入为np
将matplotlib.pyplot作为plt导入
从scipy.integrate导入ode
从numpy导入exp、sin、cos、concatenate、transpose、pi、tanh
从mpl_toolkits.mplot3d导入Axes3D
c、 eta,k,gamma1,gamma2,gamma3,gamma4,gamma5,gamma21,gamma22,gamma23,gamma24,gamma25=2,1.5,2,2,3,4,5,6,7,8,9,10
x=np.linspace(0,10100)
xdot=np.linspace(0,10100)
def成员(x,z):
返回exp(((x+z)/(pi/24))**2)
m=[]
b=[pi/6,pi/12,0,-pi/12,-pi/6]
定义f(t,Y):
x1、x2、x3、eta1、eta2、eta3、eta4、eta5、eta6、eta7、eta8、eta9、eta10、eta11、eta12、eta13、eta14、eta15、eta16、eta17、eta18、eta19、eta2、eta2、eta2、eta2、geta3、geta4、geta4、geta5、geta5、geta5、geta9、geta10、geta10、geta11、geta12、geta13、geta14、geta15、geta16、geta17、geta16、geta17、geta16、geta17、geta17、geta16、geta17、geta17、geta18、geta17、geta17、geta16、geta17、geta17、geta17、geta18、get
eta=np.数组([eta1,eta2,eta3,eta4,eta5,eta6,eta7,eta8,eta9,eta10,eta11,eta12,eta13,eta14,eta15,eta16,eta17,eta18,eta19,eta20,eta21,eta22,eta23,eta24,eta25])
geta=np.数组([geta1,geta2,geta3,geta4,geta5,geta6,geta7,geta8,geta9,geta10,geta11,geta12,geta13,geta14,geta15,geta16,geta17,geta18,geta19,geta20,geta21,geta22,geta23,geta24,geta25])
m、 z=[],[]
对于范围(5)中的i:
对于范围(5)内的j:
m、 追加(成员(x1,b[i])*成员(x2,b[j]))
对于范围(25)内的i:
z、 追加(m[i]/总和(m))
z=np.数组(z)
e、 de=x1 sin(t),x2 cos(t)
s=de+c**2*x3+2*c*e
f=np.点(eta,z)
g=np.dot(geta,z)
u=(1./g)*(-f-c**2*e-2*c*de sin(t)-eta*tanh(s)-k*s)
x1点=x2
x2dot=f+g*u
x3dot=e
eta1dot、eta2dot、eta3dot、ETA4 dot、eta5dot、eta6dot、eta7dot、eta8dot、eta9dot、eta10dot、eta11dot、eta13dot、eta2dot、eta3dot、eta14dot、eta15dot、eta10dot、eta24dot、eta3dot、eta5dot、eta6dot、eta7dot、eta8dot、eta9dot、ETA110dot、eta11dot、eta14dot、,一个15点,一个15点,一个15点,一个16点,一个18点,一个18点,一个19点,一个21点,一个22点,一个22点,一个23点,一个12点,一个24点,一个24点,一个24点,一个14点,一个15点,一个15点,一个15点,一个12点,一个22点,一个22点,一个22点,一个22点,一个22点,一个12点,一个12点,一个12点,一个12点,一个12点,一个12点,一个12点,一个12点,一个12点,一点,一个12点,一个12点,一点,一个12点,一点,一个12点,一个12点,一个12点,一个12点,一个12点,一个12点,一个12点,一个12点,一个12点,一个z[13]、-2*s*z[14]、-2*s*z[15]、-2*s*z[16]、-2*s*z[17]、-2*s*z[18]、-2*s*z[19]、-2*s*z[20]、-2*s*z[21]、-2*s*z[22],-2*s*z[23],-2*s*s*z[24],-3*s*z[0],-3*s*z[1]*u,--3*s*s*s*z[22],[22],,-2*s*s*z[22],-3*s*s*z[22],-3*s*s*s*z[1[1]u、-3*s*s*s*s*z[1]z[1]*u、-3*s*s*s*s*z[1、-3*3*s*s*s*s*z[3*z[1、[3*z[2]u、、-3*s[3*s[3*s*s*s[3*s*s*s*s*z[2]z[2]z[3*s[3*s[3*s[2]z[2]z[2],,,,,[3*s*z[15]*u、-3*s*z[16]*u、-3*s*z[17]*u、-3*s*z[18]*u、-3*s*z[19]*u、-3*s*z[20]*u、-3*s*z[21]*u、-3*s*z[22]*u、-3*s*z[23]*u、-3*s*z[24]*u
返回[x1点,x2点,x3点,eta1dot,eta2dot,eta3dot,eta4dot,eta5dot,eta6dot,eta7dot,eta8dot,eta10dot,eta11dot,eta12dot,eta13dot,eta14dot,eta15dot,eta16dot,eta17dot,eta18dot,eta19dot,eta20dot,eta22dot,eta24dot,eta24dot,geta3dot,geta3dot,geta4dot,geta1dot,geta1dot13点、14点、15点、16点、17点、18点、19点、20点、21点、22点、23点、24点、25点]
Y、 t=[],[]
对于范围(50)内的i:
Y.append(0.1)
#Y=np.数组(Y)
y0=[0,0,0]
y0=y0+Y
t0=0
def int_amar():
t1,dt=10,.01
x=[]
对于范围(53)内的i:
x、 追加([])
#导入pdb;pdb.set_trace()
odee=ode(f)。设置积分器('dopri5',方法=bdf',nsteps=1e6)
设定初始值(y0,t0)
当odee.successful()和odee.t时,问题出现在第30行:
u=(1./g)*(-f-c**2*e-2*c*de sin(t)-eta*tanh(s)-k*s)
eta
是一个数组,但其余的术语是标量。这导致u
是一个数组,结果返回列表混合了标量和数组
您可能在这里输入了错误的公式。您应该查看和。如果np.array中有一个元组,那么您应该使用([括号]而不是([[此代码编写得非常糟糕。请将其重构为其他人可以阅读的形式。停止使用大量编号的变量和1040个字符行,调试代码就会容易得多。哦,天哪!该死的,你是对的!这是一个打字错误,老实说,tanh的eta应该只是一个标量=1.5,而在我的代码中,它接受了它我想是一个数组吧!