Being able to define a custom function in Scilab comes in handy especially when we need to reuse a piece of algorithm or an actual mathematical function.
As we’ve seen in the article How to define a custom function in Scilab, using a script, saved as *.sci
file, we can define any kind of functions we need for our Scilab projects.
A faster way is to use the embedded Scilab function deff()
.
deff()
it is an embedded Scilab function for defining custom functions. It seems a bit confusing but this is what it actually does.
The deff()
function receives two arguments, both strings. Each string contains parts of the function definition which are going to be evaluate by Scilab and turn into instructions.
First string:
'[y1,y2,...]=myFunction(x1,x2,...)'
contains the definition of:
y1, y2, ...
– function return variable; we can have none, one, two or more return variables
myFunction
– the name of the function we want to define
x1, x2, ...
– function arguments; we can have none, one, two or more function arguments
Second string:
'y1=..., y2=..., ...'
contains the definition of the function body.
The advantage of the deff()
function is that we can also use it directly in the Scilab console as well as in a normal script files (*.sce
).
Example 1 (one argument, one return variable): Define the function f(x) using deff()
:
Scilab console instructions:
-->deff('y=f(x)','y=sqrt(x.^2-5*x+6)') -->f(1) ans = 1.41 -->
Example 2 (two arguments, one return variable): Define a function which calculates the kinetic energy of a body, given the mass m [kg] and the speed v [m/s], using deff()
:
The formula of the kinetic energy Ek [J] of a body is:
\[E_k = \frac{1}{2} m v^2\]Scilab console instructions:
-->deff('Ek=kineticEnergy(m,v)','Ek=(1/2)*m*v^2') -->kineticEnergy(2,10) ans = 100. -->
Example 3 (one vector argument, multiple return variables): For a given vector of values, define a function which calculates the arithmetic mean and geometric mean, using deff()
:
The formulas for arithmetic mean and geometric mean are:
\[x_{AM} = \frac{\sum_{i=1}^{N}x_i}{N}\] \[x_{GM} = \left(\prod _{i=1}^{N}x_{i}\right)^{\frac {1}{N}}\]where:
xi – is the vector of values
N – the total number of values
Scilab console instructions:
-->deff('[AM,GM]=myMeans(x)','N=length(x),AM=sum(x)/N,GM=prod(x)^(1/N)') -->[xAM,xGM]=myMeans([1 2 3 4 5]) xGM = 2.61 xAM = 3. -->
Example 4 (using conditional statements inside the function body): Define the signum (sign) function using deff()
:
The mathematical definition of the signum function is the following:
\[\text{sgn}(x)=\left\{\begin{matrix}-1, &\text{ if }x<0\\
0, &\text{ if }x=0\\
1, &\text{ if }x>0
\end{matrix}\right.\]
Scilab console instructions:
-->deff('y=mySgn(x)','if x<0 then, y=-1;elseif x>0 then, y=1;else, y=0; end') -->mySgn(0) ans = 0. -->mySgn(-3) ans = - 1. -->mySgn(3) ans = 1. -->
As you can see the Scilab deff()
function is pretty flexible and easy to use. Try your examples and let us know in the comment section below if you have any questions or feedback.
Don’t forget to Like, Share and Subscribe!