This tutorial should give the reader a good, solid overview regarding the definition of custom functions in Scilab. Today is Friday, the 6th day of the week. Save the file as dayDateTime.sci and load it in the Scilab environment: ->exec('dayDateTime.sci')Ĭall the function and check the result: ->dayDateTime() ![]() Mprintf("\nToday is %s, the %dth day of the week.\n", dayString, dayNumber) The content of the function will be saved in a *.sci file: function dayDateTime() For example, we are going to define a function which displays in the Scilab console the current day, date and hour. ![]() We can also define custom Scilab function without any arguments or return variables. If we want to delete the function from the Scilab environment, we can use the clear() function: ->clear f Running the above script will return in the Scilab console the following result: -0.000 0.333 1.000 3.000 Nan 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 Let us create a script which evaluates the f function for x and y between -5 and 5. Once the f function is loaded in the Scilab environment, we can use it inside other Scilab scripts. 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. 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. Use the function in the same way as an embedded Scilab function: ->k = f(2,3) 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')Īfter the function is loaded, it can be called as any embedded Scilab function.Īs an example, let’s define the following mathematical function is Scilab: \[f(x,y)=\left\Īfter running the instruction, all the content of the function will be executed in the Scilab console. ![]() To do this we need to use the exec() function. The next step is to load the function in the Scilab environment. This extension informs Scilab that the script file contains a function definition. 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 įunction 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 Įndfunction – is a required keyword which must be added to the function definition it defines the end of the function definition Īfter the function is defined, the script file is saved with the extension *.sci. 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 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 ![]() Function – is a required keyword which must be added to the function definition it defines the start of the function definition
0 Comments
Leave a Reply. |