4
  • I am a new user of Scilab (see also here).
  • I define a simple piece-wise function and get a confusing warning when I use the function ("Warning adding a matrix with the empty matrix will give an empty matrix result.").
  • Do you see where my mistake is?

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))

enter image description here

(Plot for myTest(t) using t = 0:0.1:10.)

enter image description here

(Confusing warning.)

Dr. Manuel Kuehner
  • 389
  • 1
  • 6
  • 16

1 Answers1

2

You have to remove the find() call and use logical indexing (the logical expressions yield boolean vectors with the same size as t and can be used as indexes) :

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))

However, after reading your post on StackExchange, if your velocity is always piecewise affine, then you could use interpln, which can be further derivated (see SO other post) or integrated with 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)

See ode and interpln scilab help pages for the meaning of their arguments (if not explicit enough above). enter image description here

Stéphane Mottelet
  • 2,853
  • 1
  • 9
  • 26
  • Thanks! Regarding the `find` function, I was following a tutorial: https://www.matrixlab-examples.com/scilab-piecewise-function.html. Is there any benefit in using the `find` function? And I still don't understand why using the `find` functions leads to the warning message. – Dr. Manuel Kuehner Sep 06 '21 at 21:30
  • 1
    Using `find` or logical indexing is equivalent, athough the latter is more elegant. The warning message occurs with both approaches and is due to the call `myTest(1)` which yields empty matrices in `t(i3)` and `t(i5)` (and addition/substraction of empty matrices with any matrices yields an empty matrix) – Stéphane Mottelet Sep 07 '21 at 09:51
  • Thanks for your reply. So the first part of your answer is not about the warning message, it's about a cleaner code, right? But the second part of your answer resolves the warning message I assume (haven't tested it yet). – Dr. Manuel Kuehner Sep 08 '21 at 15:10
  • 1
    Yes, exactly. The second part of the answer gives the simplest way (in Scilab) to handle a piecewise affine function, and won't give the same empty matrix warning when using a scalar argument. – Stéphane Mottelet Sep 08 '21 at 16:05