Matlab:ode15i求解器引发有关尺寸的错误,我看不到哪里

时间:2019-03-15 21:08:59

标签: matlab error-handling ode

代码

% Why?

tstart = 0;
tfinal = 1;
tspan = [tstart tfinal];
options = odeset('Events',@myEventsFcn);

% Initial conditions
y0 = [0,-0.6,0,0,0,0];
yp0 = [0,0,0,0,0,0];
% decic funtion calculates consistent ICs
[y0,yp0] = decic(@StateI,t0,y0,[0 1 0 0 0 0],yp0,[0 0 0 0 0 0]);

% Arrays for plots
tout = tstart;
sout = y0(1:1);
bout = y0(2:2);
zout = y0(3:3);
svout = y0(4:4);
bvout = y0(5:5);
zvout = y0(6:6);

% ode15i solves system of implicit ODEs
[t,y,te,ye,ie] = ode15i(@StateI,tspan,y0,yp0,options);

% system of implicit ODEs defined as StateI function
function res = StateI(t,y,yp)
% Constants
mS = 3*10^(-4); % [kg]
JS = 5*10^(-9); % [kgm]
mB = 4.5*10^(-3); % [kg]
g = 9.81; % [m/s^2]
JB = 7*10^(-7); % [kgm]
lS = 10^(-2); % [m]
lG = 1.5*10^(-2); % [m]
cp = 5.6*10^(-3); % [N/rad]

res = [(mS+mB)*yp(6) + mB*lS*yp(4) + mB*lG*yp(5) + (mS+mB)*g;
    mB*lS*yp(6) + (JS+mB*lS^2)*yp(4) + mB*lS*lG*yp(5) - cp*(y(2)-y(1)) +     mB*lS*g;
    mB*lG*yp(6) + mB*lS*lG*yp(4) + (JB+mB*lG^2)*yp(5) - cp*(y(1)-y(2)) +     mB*lG*g;
    y(4)-yp(1);
    y(5)-yp(2);
    y(6)-yp(3)];
end

% my events defined in myEventsFcn
function [value,isterminal,direction] = myEventsFcn(t,y,yp)
% Constants
mS = 3*10^(-4); % [kg]
mB = 4.5*10^(-3); % [kg]
g = 9.81; % [m/s^2]
rS = 3.1*10^(-3); % [m]
lS = 10^(-2); % [m]
r0 = 2.5*10^(-3); % [m]
hS = 2*10^(-2); % [m]
lG = 1.5*10^(-2); % [m]
lB = 2.01*10^(-2); % [m]
hB = 2*10^(-2); % [m]
cp = 5.6*10^(-3); % [N/rad]


Z2 = -(mS+mB)*g-mB*lG*yp(5);
Z1II = (cp*(y(2)+(rS-r0)/hS)-rS*Z2-mB*lS*lG*yp(5)-mB*lS*g)/hS;

value = [y(1)+(rS-r0)/hS, y(1)-(rS-r0)/hS, Z1II, y(2)-(lS+lG-lB-    r0)/hB];
isterminal = [1, 1, 1, 1];
direction = [-1, 1, -1, 1];
end

错误消息

Matrix dimensions must agree.

Error in odezero (line 46)
    indzc = find((sign(vL) ~= sign(vR)) & (direction .* (vR - vL) >= 0));

Error in ode15i (line 506)
    [te,ye,ie,valt,stop] = odezero(@ntrp15i,@events_aux,events_args,valt,...

Error in silly (line 24)
[t,y,te,ye,ie] = ode15i(@StateI,tspan,y0,yp0,options);

所以我之前运行过此代码,没问题,然后此错误开始弹出。我看不到该错误可能发生的位置。我尝试对向量y0和yp0进行转置,虽然不成功,但确实给出了另一个错误消息,这似乎很奇怪,因为我认为Matlab的ode求解器可以处理转置的初始条件。

最诚挚的问候

1 个答案:

答案 0 :(得分:2)

您的代码在我的MATLAB(2018b)版本上运行得很好。检查您的版本。