UNIVERSITY OF SUSSEX - INFORMATICS School of Computer Science The University of Birmingham Examples of what can be done with the Pop-11 RC_GRAPHPLOT package (Part of RC_GRAPHIC and RCLIB).

Note: This is part of the Free Poplog web site.
Some of the additional teaching materials in Poplog are described here

```
RC_GRAPHPLOT_DEMO                                  Aaron Sloman Aug 2009

USING LIB RC_GRAPHPLOT AND LIB RC_GRAPHPLOT2

Below are some examples of what can be done by giving commands using the
RC_GRAPHPLOT and RC_GRAPHPLOT2 extensions to Pop-11's RC_GRAPHIC library.
(With thanks to David Young at Sussex University.)

Together these provide a fairly versatile package for interactively
drawing graphs of functions specified in a variety of ways, as explained
in the TEACH and HELP files.

TEACH RC_GRAPHPLOT
Introduction, with many examples (some included below with graphical
output).

HELP RC_GRAPHPLOT
Terse summary documentation on the package.

-- Getting started

These commands load the library facilities:
uses popxlib;
uses rc_graphplot
uses rc_graphplot2

Example 1: Plotting the built-in function 'log'

;;; Plot the function log(x) from x = 1 to x = 10 in steps of 1/10

;;; create an empty picture
rc_start();

;;; variable to hold the output of rc_graphplot.
vars region;
rc_graphplot( 1, 1/10, 10, 'X', log, 'log(X)') -> region;

This is what is displayed:

;;; The resulting list, region, shows the rounded bounds of the
;;; rectangle containing the resulting graph:
region =>
** [1 10 0 3]

;;; I.e., as x moved between 1 and 10, y remained between. 0 and 3

Example 2: Plotting the user-defined function 'myfunc'

define myfunc(x) -> y;
;;; A function to be plotted - actually a polynomial
lvars x y;
2 * x**4  +  x**3  -  16 * x**2  +  50 -> y
enddefine;

;;; Plot the function myfunc(x) from x = -3 to x = 3 in steps of 1/10

rc_graphplot( -3, 1/10, 3, 'X',      myfunc, 'Y') -> region;

region ==>
** [-3 3 0 110]
;;; Here the maximum value of y is 110

region ==>
** [-3 3 0 110]
;;; Here the maximum value of y is 110

Example 3: Plotting specific data-points

Suppose you have done an experiment and recorded some data you would
like to plot, and that you have stored them in a Pop-11 data structure
such as a list, a vector, an array, or a string. To simulate this, load
these two lines of code:

vars mydata;
[13 12 7 3 1 1 19 29 25 20 undef 15 10 3 -3 2 1] -> mydata;

The "undef" value means that something went wrong and there is no value
for this time. Let's say the data were collected at 5-second intervals,
starting at 20 seconds into the experiment. This would then be an
appropriate call to the routine:

rc_graphplot( 20, 5, 100, 'time (s)', mydata, 'Data') -> region;

Which produces:

region =>
** [20 100 -10 35]

Example 4: Plotting x and y together - separate data sets or functions

If you have two sets of data, and you want to plot one set against the
other, you simply pass these to graphplot with the x data first, each
set followed by its label. The data can be lists, vectors, arrays, or
any other subscriptable structure. In this example, the two vectors
might represent the x and y coordinates of moving object, for example.

vars xvals, yvals, region;
{ 2  3  2  3  5  7  8  7  8  6  4} -> xvals;
{20 24 28 30 31 28 33 26 19 23 24} -> yvals;

rc_graphplot(xvals, 'X', yvals, 'Y') -> region;

region =>
** [1 9 16 36]

This would do the same:

vars xyvals;
[{2 20} {3 24} {2 28} {3 30} {5 31} {7 28} {8 33} {7 26} {8 19}
{6 23} {4 24}] -> xyvals;

;;; Use RC_GRAPHPLOT2
uses rc_graphplot2

rc_graphplot2(xyvals, 'X', 'Y') -> region;

Example 5: Plotting x and y together - combined data set or function

Sometimes, it may be more convenient to store x-y data as a single data
structure containing pairs of values, or to plot the results of a single
function which returns x and y values together. To do this, you will
need to load a different version of graphplot - LIB * GRAPHPLOT2, thus:

;;; Define a function that produces two values each time it is
;;; called.

define xy_func(t) -> (x,y);
lvars t x y;
t * cos(t) -> x;
t * sin(t) -> y
enddefine;

;;; Plot this for values of t from 0, increasing by 2, to 3200:

rc_graphplot2(0, 2, 3200, xy_func, 'X', 'Y') -> region;

See the automatically computed bounds:

region =>
** [-4000 4000 -4000 4000]

Example 6 Plotting points instead of lines

You might prefer to plot points on the graph instead of drawing lines
between the data points or function values. This can be done by changing
the value of one of the many controlling global variables, like this:

;;; Use RC_GRAPHPLOT2
uses rc_graphplot2

;;; data to be plotted
vars xyvals;
[{2 20} {3 24} {2 28} {3 30} {5 31} {7 28} {8 33} {7 26} {8 19}
{6 23} {4 24}] -> xyvals;

;;; specify shape to be drawn at each point
"plus" -> rcg_pt_type;
;;; specify diameter of shape
20 -> rcg_pt_cs;
rc_graphplot2( xyvals, 'X', 'Y') -> region;

The region

region =>
** [1 9 16 36]

You will find that the individual points are plotted with "plus" signs.

Other words that you can assign to rcg_pt_type are "square", "cross",
"plus" and "circle". More might be added in due course. You can assign a
plotting procedure of your own to rcg_pt_type - it must take two
arguments, x and y.

Example 7: Repeat but drawing circles instead of crosses

"circle" -> rcg_pt_type;
30 -> rcg_pt_cs;
rc_graphplot2( xyvals, 'X', 'Y') -> region;

That produces

To get back to drawing lines, do this:

"line" -> rcg_pt_type;

Example 8: Drawing several graphs on the same axes

So far, the program has started setting the scales from scratch for each
graph, and has cleared the window between graphs. Often, however, you
will want to put several curves or sets of data on the same axes. Let's
plot some data, and a curve (the square root function) that happens to
fit them approximately, on the same axes. We start by setting up some
more synthetic data and plotting them, using crosses of diameter 20:

rc_start();
vars moredata;
[0.1 0.9 1.3 1.8 1.95 2.2 2.5 2.9 3.1 3.2 3.4 3.45 3.5] -> moredata;
"cross" -> rcg_pt_type;
20 -> rcg_pt_cs;
rc_graphplot(0, 1, 12, 'X',   moredata, 'Y') -> region;

We'll now change the plotting style so that the theoretical function
comes out as a line:

"line" -> rcg_pt_type;      ;;; make it look different

Now we change some more global variables and plot the curve for sqrt.

false -> rcg_newgraph;      ;;; don't clear the window
region -> rcg_usr_reg;      ;;; use same axes as before
undef -> rcg_win_reg;       ;;; use same bit of window as before
rc_graphplot(0, 1/10, 12, false,    sqrt, false) -> region;

More examples can be found in TEACH RC_GRAPHPLOT

```

Suggestions for improvement are welcome.

This file maintained by:
Aaron Sloman
http://www.cs.bham.ac.uk/~axs/
Last Updated: 24 Aug 2009
Installed: 24 Aug 2009