This presentation illustrates how to work with piecewise functions in Scilab and/or Scicoslab.
It teaches two ways to do it: using iterations and using vectors. In the first way, you work with every element of the input; in the second way you work with intervals and this way is much faster and efficient.
Smart indices and built-in function find are explained.
For more information, see:
http://matrixlab-examples.com/scilab-piecewise-function.html
2. Piecewise Functions
• A piecewise function is a function which is
defined by multiple sub functions, each
sub function applying to a certain interval
of the main function's domain.
3. Piecewise Functions
• I’ll show you two ways to define and plot
them in Scilab:
1. With iterations, one element at a time.
2. Without iterations, the vectorized way.
(I’m using Scilab ver 5.4.0 for Win 7 - 64 bits)
5. 1.- Using Iterations
• First, you are going to define your piecewise
function, where you’ll consider a scalar number
as input. You’ll verify each interval and assign the
appropriate value.
• Second, you’ll call that function for all the
necessary elements.
• At the end, you’ll have two vectors, x and y, so
that you can plot your initial function.
6. 1.- Using Iterations – Define PW
// Define your function assuming that you'll
// get a scalar as input
function y = pw1(x)
// Filter and evaluate your first interval
if x <= 1 then y = -x/3 + 4/3;
// Filter and evaluate your second interval
elseif (1 < x) & (x <= 3) then y = x^2/6 + x/3 + .5;
// Filter and define your remaining intervals
else y = .5*x + 1.5;
end
endfunction
7. 1.- Using Iterations – Call Function
// Clear your command window and clear memory
clc, clear
// Make sure that Scilab can see your function.
// Load it into memory and add the full path if necessary
exec('C:UsersUsuarioDocumentsScilab_docspw1.sci');
// This is your range of interest
x = -2 : .2 : 5;
// Call the function and evaluate element-by-element
for ix = 1 : length(x)
y(ix) = pw1(x(ix));
end
// Now you have your vectors ready to plot
8. 1.- Using Iterations – Call Function
// Plot and add labels if needed
plot(x, y, 'ro-')
title('Piecewise in Scilab - Example 1');
xlabel('x');
ylabel('y');
9. 2.- Using Vectorization
• First, you are going to define your piecewise
function, where you’ll consider a vector as input.
You’ll find values for each interval and assign the
appropriate values.
• Second, you’ll call that function as you would for
any other function that takes vectors.
• At the end, you’ll have two arrays, x and y, so that
you can plot the function under study.
10. 2.- Using Vectorization
x = -2 : .2 : 5;
y = pw2(x);
plot(x, y)
Ideally, you should type something like this
and get the same plot shown above
interval of interest
function to be defined
call the plot, just as it’s done
with any other function
11. 2.- Using Vectorization
The interesting part is how to define the
piecewise function without going element-by-
element in the domain, but going instead
interval-by-interval.
To accomplish this, we’ll use two ideas:
• Specially selected indices.
• Function find.
12. 2.- Using Vectorization
In Scilab, if we have vector x = [-2 -1 0 1 2 3 4 5 6 7 8 9], and do this:
i2 = x(0 < x & x <= 3)
we’ll take all the values in vector x that meet the condition 0 < x ≤ 3, that is,
i2 = [1 2 3]
If we do:
i3 = x(3 < x & x <= 8)
we’ll take all the values in vector x that meet the condition 3 < x ≤ 8, thus
i3 = [4 5 6 7 8]
First important concept: special indices
13. 2.- Using Vectorization
In Scilab, if we have vector x = [-2 -1 0 1 2 3 4 5 6 7 8 9], and do this:
find(0 < x & x <= 3)
we’ll find the indices (not values) in vector x that meet 0 < x ≤ 3, so we’ll get
the vector [4 5 6].
If we do:
find(3 < x & x <= 8)
we’ll get the vector [7 8 9 10 11]
Second important concept: function find
14. 2.- Using Vectorization – Define PW
Putting all together, defining the function under study:
// Define your function considering a vector as an input
function y = pw2(x)
// Find the indices for the first interval
ix1 = find(x <= 1);
// Assign the appropriate values to the correct values
y(ix1) = -x(ix1)/3 + 4/3;
// Now the second interval, repeat the concept
ix2 = find(1 < x & x <= 3);
y(ix2) = x(ix2).^2/6 + x(ix2)/3 + .5;
15. 2.- Using Vectorization – Define PW
// Now, the last interval for this function
ix3 = find(x > 3);
y(ix3) = .5*x(ix3) + 1.5;
endfunction
16. 2.- Using Vectorization– Call Function
// Clear your command window and clear memory
clc, clear
// Make sure that Scilab can see your function.
// Load it into memory and add the full path if necessary
exec('C:UsersUsuarioDocumentsScilab_docspw2.sci');
// This is your range of interest
x = -2 : .2 : 5;
// Call the function - don’t need iterations
y = pw2(x);
// Now you have your vectors ready to plot
17. 2.- Using Vectorization– Call Function
// Plot and add labels if needed
plot(x, y, 'bo-')
title('Piecewise in Scilab - Example 2');
xlabel('x');
ylabel('f(x)');
18. For more examples and details, visit:
matrixlab-examples.com/scilab-piecewise-function.html