Scilab "Warning adding a matrix with the empty matrix will give an empty matrix result."

Scilab "Warning adding a matrix with the empty matrix will give an empty matrix result."


function  y = myTest(t) 

// First Part
y(find(t < 0)) = 0; 

// Middle Part
y(find(0 <= t & t <= 1)) = 1; 

// Middle Part
ti3 = find(1 < t & t <= 3);
y(ti3) = -1*t(ti3) + 2; 

// Middle Part
y(find(3 < t & t <= 4)) = -1; 

// Middle Part
ti5 = find(4 < t & t <= 6);
y(ti5) = +1*t(ti5) -5;

// Middle Part
y(find(6 < t & t <= 8)) = 1; 

// Last Part
y(find(8 < t)) = 0; 

endfunction

myTest(1)

t = 0:0.1:10;

plot(t',myTest(t))

(使用 t = 0:0.1:10 绘制 myTest(t)。)

(令人困惑的警告。)

您必须删除 find() 调用并使用逻辑索引(逻辑表达式产生与 t 大小相同的布尔向量,可用作索引):

function  y = myTest(t) 

// First Part
y(t < 0) = 0; 

// Middle Part
y(0 <= t & t <= 1) = 1; 

// Middle Part
ti3 = 1 < t & t <= 3;
y(ti3) = -1*t(ti3) + 2; 

// Middle Part
y(3 < t & t <= 4) = -1; 

// Middle Part
ti5 = 4 < t & t <= 6;
y(ti5) = +1*t(ti5) -5;

// Middle Part
y(6 < t & t <= 8) = 1; 

// Last Part
y(8 < t) = 0; 

endfunction

myTest(1)

t = 0:0.1:10;

plot(t',myTest(t))

但是,在 StackExchange 上阅读您的 post 之后,如果您的速度始终是分段仿射的,那么您可以使用 interpln,它可以进一步推导(参见 SO other post ) 或集成 ode:

function out=rhs(t,y)
  out = interpln(ty,t);
end

ty=[0 1  3 4 6 8
    1 1 -1 1 1 0]
t=linspace(0,8,1000);
h=sqrt(%eps);
d=(interpln(ty,t+h)-interpln(ty,t))/h;
p=ode(0,0,t,rhs)
plot(x,interpln(xy,x),x,d,x,p)

请参阅 odeinterpln scilab 帮助页面以了解其参数的含义(如果上面不够明确)。