我试图确定使用ODE45的差分驱动机器人的姿态(X,Y,θ-)。我有下面的代码解决了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
为0
与20
的初始条件0
秒。我希望输出给我从0
时间矢量20
以及为x
数组。我认为,问题出在初始状态。 MATLAB让我在现场编辑的错误告诉我,“@(t,y)dxdt returns a vector of length 69, but the length of initial conditions vector is 1. The vector returned by @(t,y)dxdt and the initial conditions vector must have the same number of elements.
”
问题不在于初始条件。您需要定义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
回1值当它通过一个t
和一个y
您dxdt
“功能”。当你得到这样的错误,我建议把
clear all
`clearvars` % better than clear all - see am304's comment below
在脚本的顶部,以避免与先前定义的变量污染您的工作空间。