scipy.integrate 的 Odeint 函数给出错误的结果

均值机

我使用 odeint 函数来求解耦合微分方程系统,并在系统求解后绘制其中一个变量 (theta_i)。我的变量 (theta_i) 来自等式:

theta_i = np.arctan2(g1,g2)

其中 g1 和 g2 是在同一函数中计算的变量。结果必须介于 -pi 和 pi 之间,并且它们应该如下所示(来自 matlab 模拟图):在此处输入图片说明

但是,当我在 odeint 完成后尝试绘制 theta_i 时,我得到了这个(从我的 python 代码中绘制):

在此处输入图片说明

which is really weird. When I print the values of theta_i right after its calcumation (still inside the function) they look correct (between -0.2 and 0.5), so it has to be something with the result's storing and my implementation of odeint. All the other variables that come from the odeint solution are correct. I searched similar posts but nobody had the same problem with me. What might be the problem here? I am new to python and I use python 2.7.12. Thank you in advance.

import numpy as np 
from scipy.integrate import odeint 
import matplotlib.pyplot as plt

added_mass_x = 0.03 # kg 
added_mass_y = 0.04 
mb = 0.3 # kg 
m1 = mb-added_mass_x 
m2 = mb-added_mass_y 
l1 = 0.07 # m 
l2 = 0.05 # m 
J = 0.00050797 # kgm^2 
Sa = 0.0110 # m^2 
Cd = 2.44 
Cl = 3.41 
Kd = 0.000655 # kgm^2 
r = 1000 # kg/m^3

c1 = 0.5*r*Sa*Cd 
c2 = 0.5*r*Sa*Cl 
c3 = 0.5*mb*(l1**2) 
c4 = Kd/J 
c5 = (1/(2*J))*(l1**2)*mb*l2 
c6 = (1/(3*J))*(l1**3)*mb

theta_0 = 10*(np.pi/180) # rad 
theta_A = 20*(np.pi/180) # rad 
f = 2 # Hz

t = np.linspace(0,100,8000) # s

def direct(u,t):
    vcx = u[0]
    vcy = u[1]
    wz = u[2]
    psi = u[3]
    x = u[4]
    y = u[5]
    vcx_i = u[6]
    vcy_i = u[7]
    psi_i = u[8]
    wz_i = u[9]
    theta_i = u[10]
    theta_deg_i = u[11]

    # Subsystem 1
    omega = 2*np.pi*f # rad/s
    theta = theta_0 + theta_A*np.sin(omega*t) # rad
    theta_deg = (theta*180)/np.pi # deg
    thetadotdot = -(omega**2)*theta_A*np.sin(omega*t) # rad/s^2

    # Subsystem 2
    vcxdot = (m2/m1)*vcy*wz-(c1/m1)*vcx*np.sqrt((vcx**2)+(vcy**2))+(c2/m1)*vcy*np.sqrt((vcx**2)+(vcy**2))*np.arctan2(vcy,vcx)-(c3/m1)*thetadotdot*np.sin(theta)
    vcydot = -(m1/m2)*vcx*wz-(c1/m2)*vcy*np.sqrt((vcx**2)+(vcy**2))-(c2/m2)*vcx*np.sqrt((vcx**2)+(vcy**2))*np.arctan2(vcy,vcx)+(c3/m2)*thetadotdot*np.cos(theta)
    wzdot = ((m1-m2)/J)*vcx*vcy-c4*wz*wz*np.sign(wz)-c5*thetadotdot*np.cos(theta)-c6*thetadotdot
    psidot = wz

    # Subsystem 3
    xdotdot = vcxdot*np.cos(psi)-vcx*np.sin(psi)*wz+vcydot*np.sin(psi)+vcy*np.cos(psi)*wz # m/s^2
    ydotdot = -vcxdot*np.sin(psi)-vcx*np.cos(psi)*wz+vcydot*np.cos(psi)-vcy*np.sin(psi)*wz # m/s^2
    xdot = vcx*np.cos(psi)+vcy*np.sin(psi) # m/s
    ydot = -vcx*np.sin(psi)+vcy*np.cos(psi) # m/s

    # Subsystem 4
    vcx_i = xdot*np.cos(psi_i)-ydot*np.sin(psi_i)
    vcy_i = ydot*np.cos(psi_i)+xdot*np.sin(psi_i)
    psidot_i = wz_i
    vcxdot_i = xdotdot*np.cos(psi_i)-xdot*np.sin(psi_i)*psidot_i-ydotdot*np.sin(psi_i)-ydot*np.cos(psi_i)*psidot_i
    vcydot_i = ydotdot*np.cos(psi_i)-ydot*np.sin(psi_i)*psidot_i+xdotdot*np.sin(psi_i)+xdot*np.cos(psi_i)*psidot_i

    g1 = -(m1/c3)*vcxdot_i+(m2/c3)*vcy_i*wz_i-(c1/c3)*vcx_i*np.sqrt((vcx_i**2)+(vcy_i**2))+(c2/c3)*vcy_i*np.sqrt((vcx_i**2)+(vcy_i**2))*np.arctan2(vcy_i,vcx_i)
    g2 = (m2/c3)*vcydot_i+(m1/c3)*vcx_i*wz_i+(c1/c3)*vcy_i*np.sqrt((vcx_i**2)+(vcy_i**2))+(c2/c3)*vcx_i*np.sqrt((vcx_i**2)+(vcy_i**2))*np.arctan2(vcy_i,vcx_i)

    A = 12*np.sin(2*np.pi*f*t+np.pi) # eksiswsi tail_frequency apo simulink
    if A>=0.1:
        wzdot_i = ((m1-m2)/J)*vcx_i*vcy_i-c4*wz_i**2*np.sign(wz_i)-c5*g2-c6*np.sqrt((g1**2)+(g2**2))
    elif A<-0.1:
        wzdot_i = ((m1-m2)/J)*vcx_i*vcy_i-c4*wz_i**2*np.sign(wz_i)-c5*g2+c6*np.sqrt((g1**2)+(g2**2))
    else:
        wzdot_i = ((m1-m2)/J)*vcx_i*vcy_i-c4*wz_i**2*np.sign(wz)-c5*g2


    if g2>0:
        theta_i = np.arctan2(g1,g2)
    elif g2<0 and g1>=0:
        theta_i = np.arctan2(g1,g2)-np.pi
    elif g2<0 and g1<0:
        theta_i = np.arctan2(g1,g2)+np.pi
    elif g2==0 and g1>0:
        theta_i = -np.pi/2
    elif g2==0 and g1<0:
        theta_i = np.pi/2
    elif g1==0 and g2==0:
        theta_i = 0
    theta_deg_i = (theta_i*180)/np.pi
    #print theta_deg_i


    return [vcxdot, vcydot, wzdot, psidot, xdot, ydot, vcxdot_i, vcydot_i, psidot_i, wzdot_i, theta_i, theta_deg_i]

# arxikes synthikes 
vcx_0 = 0.1257 
vcy_0 = 0 
wz_0 = 0 
psi_0 = 0 
x_0 = 0 
y_0 = 0 
vcx_i_0 = 0.1257 
vcy_i_0 = 0 
psi_i_0 = 0 
wz_i_0 = 0 
theta_i_0 = 0.1745 
theta_deg_i_0 = 9.866 
u0 = [vcx_0, vcy_0, wz_0, psi_0, x_0, y_0, vcx_i_0, vcy_i_0, psi_i_0, wz_i_0, theta_i_0, theta_deg_i_0]

u = odeint(direct, u0, t, tfirst=False)

vcx = u[:,0] 
vcy = u[:,1] 
wz = u[:,2] 
psi = u[:,3] 
x = u[:,4] 
y = u[:,5] 
vcx_i = u[:,6] 
vcy_i = u[:,7] 
psi_i = u[:,8] 
wz_i = u[:,9] 
theta_i = u[:,10] 
theta_deg_i = u[:,11] 
print theta_i

plt.figure(17) 
plt.plot(t,theta_i,'r-',linewidth=1,label='theta_i') 
plt.xlabel('t [s]') 
plt.title('theta_i [rad] (Main body CF)') 
plt.legend() 
plt.show()
Mstaino

The problem as you stated is that theta_i is not part of the gradient. When you formulate your direct, it should be of the form:

def direct(vector, t):
    return vector_dot

The quickest and dirtiest solution (without cleaning the code) is to use the function you already defined:

theta_i = [direct(u_i, t_i)[10] for t_i, u_i in zip(t, u)]

I used a a shorter interval: t = np.linspace(0,10,8000). It yielded this:

在此处输入图片说明

编辑:如何从积分器中删除您的 theta:

def direct(u, t):
    # your original function as it is

def direct2(u,t):
    return direct(u,t)[:9]

#now integrate the second function
u = odeint(direct2, u0, t)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

scipy.integrate.odeint返回错误的结果

来自分类Dev

来自scipy.in的odeint在Python中给出错误的结果?

来自分类Dev

使用scipy.integrate.odeint解决微分方程:“函数调用的结果不是正确的浮点数组。”

来自分类Dev

(scipy.integrate.odeint如何加速功能评估?

来自分类Dev

使用scipy.integrate.odeint覆盖ODE集成中的值

来自分类Dev

使用scipy.integrate.odeint时发生TypeError

来自分类Dev

scipy.integrate.odeint和scipy.integrate.ode有什么区别?

来自分类Dev

scipy.integrate.odeint和scipy.integrate.ode有什么区别?

来自分类Dev

用于编写与`scipy.integrate.odeint'交互的Python类的设计启发法?

来自分类Dev

需要更好地了解rtol,atol在scipy.integrate.odeint中的工作方式

来自分类Dev

Scipy odeint给lsoda警告

来自分类Dev

python中的函数“ integrate.quad”和R中的函数“ integral”,“ integrate”给出错误的结果

来自分类Dev

MATLAB和SciPy对'buttord'函数给出不同的结果

来自分类Dev

如何修复scipy的odeint函数中的np.float64不可调用错误?

来自分类Dev

函数给出错误的结果

来自分类Dev

pow() 函数给出错误的结果

来自分类Dev

匹配函数给出错误的结果

来自分类Dev

使用Monte Carlo与scipy.integrate.nquad的不同积分结果

来自分类Dev

使用scipy.integrate.quad时结果不连续

来自分类Dev

使用scipy.integrate.quad时结果不连续

来自分类Dev

scipy.integrate.trapz和不连续函数

来自分类Dev

使用scipy.integrate.quad集成伽玛函数

来自分类Dev

scipy.integrate伪-Voigt函数,积分变为0

来自分类Dev

使用scipy.integrate.quad集成伽玛函数

来自分类Dev

Scipy odeint不带参数或括号的方法

来自分类Dev

Scipy.integrate 给出奇怪的结果;有最佳实践吗?

来自分类Dev

scipy.optimize.curve_fit 定积分函数与 scipy.integrate.quad

来自分类Dev

与Scipy集成会给出错误的结果,下限为负

来自分类Dev

SciPy 拟合给出错误

Related 相关文章

  1. 1

    scipy.integrate.odeint返回错误的结果

  2. 2

    来自scipy.in的odeint在Python中给出错误的结果?

  3. 3

    使用scipy.integrate.odeint解决微分方程:“函数调用的结果不是正确的浮点数组。”

  4. 4

    (scipy.integrate.odeint如何加速功能评估?

  5. 5

    使用scipy.integrate.odeint覆盖ODE集成中的值

  6. 6

    使用scipy.integrate.odeint时发生TypeError

  7. 7

    scipy.integrate.odeint和scipy.integrate.ode有什么区别?

  8. 8

    scipy.integrate.odeint和scipy.integrate.ode有什么区别?

  9. 9

    用于编写与`scipy.integrate.odeint'交互的Python类的设计启发法?

  10. 10

    需要更好地了解rtol,atol在scipy.integrate.odeint中的工作方式

  11. 11

    Scipy odeint给lsoda警告

  12. 12

    python中的函数“ integrate.quad”和R中的函数“ integral”,“ integrate”给出错误的结果

  13. 13

    MATLAB和SciPy对'buttord'函数给出不同的结果

  14. 14

    如何修复scipy的odeint函数中的np.float64不可调用错误?

  15. 15

    函数给出错误的结果

  16. 16

    pow() 函数给出错误的结果

  17. 17

    匹配函数给出错误的结果

  18. 18

    使用Monte Carlo与scipy.integrate.nquad的不同积分结果

  19. 19

    使用scipy.integrate.quad时结果不连续

  20. 20

    使用scipy.integrate.quad时结果不连续

  21. 21

    scipy.integrate.trapz和不连续函数

  22. 22

    使用scipy.integrate.quad集成伽玛函数

  23. 23

    scipy.integrate伪-Voigt函数,积分变为0

  24. 24

    使用scipy.integrate.quad集成伽玛函数

  25. 25

    Scipy odeint不带参数或括号的方法

  26. 26

    Scipy.integrate 给出奇怪的结果;有最佳实践吗?

  27. 27

    scipy.optimize.curve_fit 定积分函数与 scipy.integrate.quad

  28. 28

    与Scipy集成会给出错误的结果,下限为负

  29. 29

    SciPy 拟合给出错误

热门标签

归档