Skip to content

线性化 #3

Open
Open
@fly-daiuser

Description

@fly-daiuser

你好,我在作者给的非线性动力学模型 线性化 案例中,按照给的代码,运行总是会出现类型不匹配的错误,运行了作者的代码也是同样的问题,请问是为什么呢
作者代码如下:
mc = 0.2 # mass of the cart (kg)
mp = 0.1 # mass of the pole (kg)
ℓ = 0.5 # distance to the center of mass (meters)
g = 9.81

def cartpole_dynamics(x, u):
r = x[0] # cart position
theta = x[1] # pole angle
rd = x[2] # change in cart position
theta_d = x[3] # change in pole angle
F = u[0] # force applied to cart

#问题出现处,显示np.sin(theta)中的theta不符合数据要求
theta_dd = (gnp.sin(theta) + np.cos(theta) * ((-F - mpl*(theta_d**2) *
np.sin(theta))/(mc + mp))) / (l*(4/3 - (mp*(np.cos(theta)2))/(mc + mp)))
rdd = (F + mpl((theta_d
2)np.sin(theta) - theta_ddnp.cos(theta))) / (mc + mp)

return np.array([rd, theta_d, rdd, theta_dd])

def cartpole_rk4(x, u, dt):
f1 = dtcartpole_dynamics(x, u)
f2 = dt
cartpole_dynamics(x + f1/2, u)
f3 = dtcartpole_dynamics(x + f2/2, u)
f4 = dt
cartpole_dynamics(x + f3, u)
return x + (1/6)(f1 + 2f2 + 2*f3 + f4)
import autograd as AG

xgoal = np.array([0.0, np.pi, 0.0, 0.0])
ugoal = np.array([0.0])

dt = 0.01

A = AG.jacobian(lambda x_: cartpole_rk4(x_, ugoal))(xgoal)
B = AG.jacobian(lambda u_: cartpole_rk4(xgoal, u_))(ugoal)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions