FUNC: A Program for Generating Data for Graphing a Function

Andrew D. Lewis

18 March 1998

1 Introduction

I often have need to make the graph of a given function. To do so, and to allow some flexibility in what the plot looks like (i.e., not using Mathematica or some such thing), I have a C program I call FUNC. The documentation was written mainly so I would remember how to use the program after long periods of enforced absence.

The program is set up to allow easy parameter changes. The program also produces a log file which should allow the user to reproduce any data at a later date.

2 Program Compilation^{1}

To get a version of FUNC up and running, simply edit the variables BINDIR and MANDIR in Makefile to reflect your system settings. Also, DOCDIR and DEMODIR are used to build a correct man page. Then type make to build the binary, make install to put it in the specified location. make man will build and install the man page. make clean will get rid of object files, etc.

3 Basic Usage

To use FUNC, simply type

func filename[.dat]

If the argument is left off, FUNC will read from standard input and write to standard output. If the argument is present, FUNC will check for the existence of a file filename.finp from which to read the input data. If this file is not present, FUNC will next check for a file finp.finp. If this file is also not present, you will have to enter input data by hand. The input data consists of the following things:

1This set up procedure has been performed with SunOS 5.4 and is not guaranteed to work on any other operating system.

1. The parameters for the problem. In Section 5 we will tell you how to set up FUNC to give you the parameters you need.

2. Next you enter the initial value of the independent variable.

3. Next enter the final value of the independent variable.

4. Next enter the increment for the independent variable.

Now FUNC will take this data and compute the value of the function at the specified points. The output data will be place in the file filename.dat. A log file, filename.flog will be created which contains the time of file creation, as well as all the input data. If the output is written to standard output, no log file is created.

4 Specifying Which Function Will be Plotted

Now you know how to make FUNC generate the data for the graph of a function. But which function will it graph? Well, the one you define with the function function. In the src directory you will see a file function.c which contains the definition of the function you will graph. Every time you change this file, you must recompile FUNC to reflect these changes.

The format of function.c is fairly simple. The number x is the independent variable. function simply returns the value of the function at the current value of x. There is also a vector parms which contains any parameters for the function.

5 Specifying the Parameters in the Function

You will need to specify the values in the parms vector in function.c. This is done in the subroutine parmenter which is in the file parmenter.c. This subroutine should be set up so that it echoes to the screen which parameter is to be read, and then allows the data to be read from either the screen or from the input data file. If you wish, you may want to echo the value of the parameter to the log file.

The following segment of code illustrates how one may read in parms[0].

    printf("\n\tEnter the frequency in rad/sec.\n");
    fscanf(input_fp,"%lf",&parms[0]);
    if (!UsingStdIn) {
fprintf(log_fp,"\nThe frequency is %lf rad/sec.\n",parms[0]);
    }

I hope this is self-explanatory. I should also mention that you may wish to use parmenter.c to do some computations involving parameters that you would otherwise have to do by hand. This might allow you to, for example, enter a rotational speed in rpm, but convert it to rad/sec for use in the program. I have found this very helpful.

6 Demo

There is a demo set up in the demo directory. It will plot a sine wave of a specified frequency, and should give you some idea of how to use the subroutines

function and parmenter.

Please send any comments and errors to: andrew@mast.queensu.ca.