我正在尝试使用ode45确定差动驱动机器人的姿态(x,y,theta)。我下面的代码可以解决x位置,但是初始条件有问题。我将其设置为0,因为在时间0假定机器人位于原点,但出现错误。如何设置ode45的初始条件,以便获得预期的输出?
我试图通过将初始条件设置为41x1零矩阵来制作与dxdt长度相同的初始条件矢量,但是我不理解输出,并且我不相信自己做得正确。
% Given conditions for a differential drive robot:
B = 20; % (cm) distance along axle between centers of two wheels
r = 10; % (cm) diameter of both wheels
w_l = 5*sin(3*t); % (rad/s) angular rate of left wheel
w_r = 5*sin(3*t); % (rad/s) angular rate of right wheel
v_l = r*w_l; % (cm/s) velocity of left wheel
v_r = r*w_r; % (cm/s) velocity of right wheel
v = (v_r+v_l)/B; % (cm/s) velocity of robot
theta = 90; % constant orientation of robot since trajectory is straight
% Solve differential equation for x:
dxdt = v*cos(theta); % diff equaition for x
tspan = [0 20]; % time period to integrate over
x0 = 0; % initial condition since robot begins at origin
[t,x] = ode45(@(t,y) dxdt, tspan, x0);
我想用初始条件为dxdt
的{{1}}至0
秒求解微分方程20
。我希望输出给我一个从0
到0
的时间向量,以及一个20
的数组。我认为问题在于初始条件。 MATLAB在实时编辑器中给我一个错误,告诉我“ x
”
答案 0 :(得分:1)
问题不是最初的状况。您需要将dxdt
定义为一个函数,即
% Define differential equation
function derivative = dxdt(t,y)
% Given conditions for a differential drive robot:
B = 20; % (cm) distance along axle between centers of two wheels
r = 10; % (cm) diameter of both wheels
w_l = 5*sin(3*t); % (rad/s) angular rate of left wheel
w_r = 5*sin(3*t); % (rad/s) angular rate of right wheel
v_l = r*w_l; % (cm/s) velocity of left wheel
v_r = r*w_r; % (cm/s) velocity of right wheel
v = (v_r+v_l)/B; % (cm/s) velocity of robot
theta = 90; % constant orientation of robot since trajectory is straight
derivative = v*cos(theta); % diff equation for x
end
然后,当您使用ode45
时,应该告诉它将t
和y
变量作为参数传递给dxdt
,例如
[t,x] = ode45(@(t,y) dxdt(t,y), tspan, x0);
这应该可以工作。在这种情况下,由于dxdt
仅采用默认参数,因此您也可以编写
[t,x] = ode45(@dxdt, tspan, x0);
您得到的错误表明您有时将dxdt
设为长度为69的向量,而MATLAB期望在传递dxdt
时返回t
的1个值。和y
“功能”中的一个dxdt
。每当您遇到此类错误时,建议您
clear all
`clearvars` % better than clear all - see am304's comment below
位于脚本顶部,以避免使用先前定义的变量污染您的工作空间。