如何解决我正在尝试使用射击方法找到一个周期轨道
def integrate_ode(ode,u0,T):
sol=solve_ivp(ode,T,u0)
return sol.y[:,-1]
def phase(ode,u0):
return ode(0,u0)[0] #dx/dt(0)
def shooting(ode,U):
print(len(U))
u0=U[0:-1]
#print(u0)
T=U[-1]
#print(T)
G=np.concatenate((integrate_ode(ode,T)-u0,phase(ode,u0)))
return G
要获得我正在使用的解决方案:
fsolve(lambda U:shooting(ode1,U),...(initial conditions))
我收到此错误:
IndexError: invalid index to scalar variable.
这是我的完整追溯:
Traceback (most recent call last):
"...Python\workweek16.py",line 62,in <module>
G=fsolve(lambda U:shooting(ode1,(0,0))
File "C:\Users\tmara\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\python39\site-packages\scipy\optimize\minpack.py",line 160,in fsolve
res = _root_hybr(func,x0,args,jac=fprime,**options)
File "C:\Users\tmara\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\python39\site-packages\scipy\optimize\minpack.py",line 226,in _root_hybr
shape,dtype = _check_func('fsolve','func',func,n,(n,))
File "C:\Users\tmara\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\python39\site-packages\scipy\optimize\minpack.py",line 24,in _check_func
res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
File "...Python\workweek16.py",in <lambda>
G=fsolve(lambda U:shooting(ode1,0))
File "...Python\workweek16.py",line 51,in shooting
G=np.concatenate((integrate_ode(ode,u0)))
File "...Python\workweek16.py",line 40,in integrate_ode
sol=solve_ivp(ode,u0)
File "C:\Users\tmara\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\python39\site-packages\scipy\integrate\_ivp\ivp.py",line 508,in solve_ivp
t0,tf = float(t_span[0]),float(t_span[1])
IndexError: invalid index to scalar variable.
有人知道该怎么做吗?
解决方法
t_span
的 solve_ivp
参数实际上需要是一个时间跨度。因此,将 T
替换为 [0,T]
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。