How to define a custom function in Scilab

Scilab is very flexible and versatile in terms of custom functions definitions. If the user is not satisfied with the already existing embedded Scilab function, he/she has the option of defining custom Scilab functions. In order to make the function reusable, the best option is to define it as a script in the SciNotes editor.

In order to create a custom function in Scilab, we need to define it following a certain structure:

Scilab custom function structure

Image: Scilab custom function structure

where:

function – is a required keyword which must be added to the function definition; it defines the start of the function definition;

y1, y2 – return values of the function; they have to be separated by a comma (,) and enclosed in right brackets [ ]; the function can have none, one or multiple return values;

myFunction – the name of the custom function; it needs to be different than the existing embedded Scilab functions and it should not have the same name as a Scilab script file (*.sce) in the same working directory;

x1, x2 – function arguments, input variables; they need to be separated by a comma (,) and enclosed in round brackets ( ); the function can have none, one or multiple arguments;

function content – is the sum of all the Scilab instructions that represents the definition of the function; it is also named the body of the function;

endfunction – is a required keyword which must be added to the function definition; it defines the end of the function definition;

After the function is defined, the script file is saved with the extension *.sci. This extension informs Scilab that the script file contains a function definition.

The next step is to load the function in the Scilab environment. To do this we need to use the exec() function. If the function is saved in the working directory, the argument of the exec() function is the name of the custom function file as a string:

exec('myFunction.sci')

After the function is loaded, it can be called as any embedded Scilab function.

As an example, let’s define the following mathematical function is Scilab:

\[f(x,y)=\left\{\begin{matrix}
\frac{x^2-y^2}{x+y}, x+y>0, y>0\\
\frac{x^2+y^2}{x+y}, x+y>0, y \le 0\\
\frac{x-y}{x+y}, x+y<0, y \in \mathbb{R}
\end{matrix}\right.\]

Step 1. Open a new file in SciNotes and define the function.

We can observe that the function has only one return variable and two arguments. Also, according to the above definition, the function f is not defined for x = y = 0. One way of implementing it is by using if-elseif conditional statements:

function [z] = f(x,y)
   if ((x+y>0) & (y>0)) then
      z = (x^2-y^2)/(x+y);
   elseif ((x+y>0) & (y<=0)) then
      z = (x^2+y^2)/(x+y);
   elseif (x+y<0) then
      z = (x-y)/(x+y);
   else
      z = %nan;
   end
endfunction

Step 2. Save the file as f.sci in the working Scilab directory.

Step 3. Load the function in the Scilab environment:

-->exec('f.sci')

After running the instruction, all the content of the function will be executed in the Scilab console.

Step 4. Use the function in the same way as an embedded Scilab function:

-->k = f(2,3)
 k =
 
 - 1. 
 
-->

Notice that the return of the function is associated with the variable k,which has nothing to do with the z, x and y variables from the function definition. Bear in mind that the function arguments (x, y) and the return variable (z) are only visible locally in the function, they can not be accessed from outside the function.

Once the f function is loaded in the Scilab environment, we can use it inside other Scilab scripts. Let us create a script which evaluates the f function for x and y between -5 and 5. To make the exercise more interesting, we are going to use the mprintf() function to display the result in a matrix form:

for i=-2:2
   for j=-2:2
      mprintf("%1.3f\t",f(i,j));
   end
   mprintf("\n");
end

Running the above script will return in the Scilab console the following result:

-0.000 0.333  1.000 3.000  Nan 
-0.333 -0.000 1.000 Nan    -3.000 
-1.000 -1.000 Nan   -1.000 -2.000 
-3.000 Nan    1.000 0.000  -1.000 
Nan    5.000  2.000 1.000  0.000

If we want to delete the function from the Scilab environment, we can use the clear() function:

-->clear f

We can also define custom Scilab function without any arguments or return variables. For example, we are going to define a function which displays in the Scilab console the current day, date and hour. The content of the function will be saved in a *.sci file:

function dayDateTime()
   today = datenum();
   [dayNumber,dayString] = weekday(today,'long');
   mprintf("\nToday is %s, the %dth day of the week.\n", dayString, dayNumber);
   mprintf("The date is: %s\n", date());
   t = clock();
   mprintf("The time is: %d:%d:%d\n",t(4), t(5), t(6));
endfunction

Save the file as dayDateTime.sci and load it in the Scilab environment:

-->exec('dayDateTime.sci')

Call the function and check the result:

-->dayDateTime()

Today is Friday, the 6th day of the week.
The date is: 13-Jan-2017
The time is: 0:57:29
 
-->

This tutorial should give the reader a good, solid overview regarding the definition of custom functions in Scilab. Try the above examples and ask for more details, if needed, using the comment form below.

Don’t forget to Like, Share and Subscribe!

Leave a Reply

Ad Blocker Detected

Dear user, Our website provides free and high quality content by displaying ads to our visitors. Please support us by disabling your Ad blocker for our site. Thank you!

Refresh