Creative Commons License Copyright © Michael Richmond. This work is licensed under a Creative Commons License.

Scilab: Functions and Plotting

Functions

Functions are the basic building blocks of any non-trivial program. Scilab has hundreds of built-in functions, but it also allows you to define your own. Here's how:

Input and output arguments

Functions can have any mix of input and output arguments. Input arguments should not be changed in the course of the function! Output arguments should not be referenced in the function until they have been set!

Many common, simple functions have a single input and a single output argument. For example, one could write Scilab definitions of some common mathematical functions like so:

      square root:         function y = square_root(x)
      cosine:              function y = cosine(x)

Some functions have no input arguments; for example, a random number generator which simply returns a random value between 0 and 1 might be defined like this:

      random number:       function y = rand
Scilab's built-in rand function works this way (actually, one can call it with arguments, but only for fairly arcane purposes).

Some functions have two or more input arguments. Suppose you want to write a function which performs the same job as a for loop: beginning at some starting value, count by some increment until reaching an ending value. You function definition might look something like this:

      count over a range:  function counter(startval, increment, endval)

It's also possible for a function to have more than one output argument. One very common mathematical task is to convert from rectangular [x,y] coordinates to polar [r, theta], and vice versa. Either conversion requires two input values, and two output values. For example, the definition of one such function might look like this:

    rectangular to polar:  function[r, theta] = rect_to_polar(x, y)

The output arguments appear immediately after the keyword function, enclosed in square brackets. Note that the input arguments are enclosed in parantheses.

You may download the 'rect_to_polar' function, examine it, and try it out. Note the checks to figure out in which quadrant the vector lies. We need to do this because the Scilab functions all deal with angles in the range (-pi to +pi), whereas we want to yield angles in the range (0 to +2pi).


Making simple plots

Scilab contains many powerful tools for creating complicated graphs and images. We will need only the simplest of these tools. You can find a more detailed discussion with pictures elsewhere.

To create a 2-D x/y plot,

For example, try these commands:

      x = 0 : 0.1 : 30;
      y = sin(x);
      plot(x, y);

If you call plot a second time, it will sit on top of the first plot:

      y = sqrt(x);
      plot(x, y);

If you want to erase the old plot and start from scratch, and add to it the new one on the same set of axes, use the clf command before making the second plot:

      clf;
      y = sin(x);
      plot(x, y);

There's another way to do it, too: you can supply more than a single pair of (x, y) values to the plot function:

      x = 1 : 0.1: 30;
      y1 = sin(x);
      y2 = cos(x);
      y3 = sqrt(x);
      plot(x, y1, x, y2, x, y3);

The Graphics chapter of the Scilab documentation contains many, many more examples of the software's graphing capabilities.


Creative Commons License Copyright © Michael Richmond. This work is licensed under a Creative Commons License.