Bottom     Previous     Contents

11 Displaying shapes of functions

11.0 Introduction

11.1 Selecting a function for display

11.2 Using the function-drawing program

11.3 The operation of the program

11.4 Activities

11.5 Displaying two functions together

11.6 Activities

11.7 Discussion of activities

11.0 Introduction

In this chapter we provide and explain programs by which you can display the shapes of functions. There are many situations where it is useful to able to do this, perhaps to get some idea whether a function doubles back on itself, or whether it reaches a maximum or a minimum - and if so, where. In fact, a standard method for solving simultaneous equations relies on the technique of displaying the shapes of the functions concerned and identifying where they coincide.

Screen Display 11.1

Even if you do not want to use our programs for any specific purpose, you will find it fun just to experiment and see the shapes that various functions produce. In doing so, you will also be gaining some worthwhile mathematical insights.

11.1 Selecting a function for display

Our programs are suitable for displaying functions with two variables. We arbitrarily call these X and Y, although they could of course be called anything else. Such functions produce a display in two dimensions. Our programs are not suitable for displaying functions with more than two variables, because these would need at least three dimensions.
In order to use our programs to display functions, you have to feed in the function in the following form, where the dots represent an expression which the computer can evaluate:

Y = ....

The Y must be alone on the left-hand side. This means that expressions such as the following need some prior manipulation before they are suitable:

2Y + sin X = 3
Y sin X = 5
6 cos X + Y sin X + tan X = 0

The expression for X can be as complex as you like, provided it is written in terms that the computer can recognise and use, The following are suitable examples:

Y =SIN(X/9)/SQR(X)
Y =SQR(X^2+45*X)
Y=EXP(-X/98.2)*SIN(X/41)

11.2 Using the function-drawing program

Before we discuss the program which draws the functions, you may like to see what it can do. We shall take the following function as an example, which - incidentally - is for damped oscillatory motion:

Y =EXP(-X/98.2)* SIN(X/41)

Screen Display 11.1 shows the requests that the program makes to the user: about the function required; about the smallest value a f X and the largest value of X; and about the number of steps. (Our example data is underlined to distinguish it from the part of the dialogue which is due to the computer rather than to the user.) Screen Display 11.1 also shows the resulting display for the data which we provide. You could of course input other data and get a very different display.

11.3 The operation of the program

Listing 11.1 gives the program responsible for Screen Display 11.1. As you are probably coming to expect, it relies on PROCscale, PROCgraduate and PROCnumber. We do not use PROCgraph on this occasion because PROCgraph plots each point as a small + whereas we want a continuous curve. We use the DRAW statement of BBC BASIC. However, by calling upon PROCscale, we still take advantage of the automatic scaling which so simplifies the programming. To convert from the X and Y co-ordinates supplied by the program to the numbers required when addressing the screen, we have included two functions within PROCscale: FN£CVX and FN£CVY. At any place where your program wishes to write to the screen, the scaling is done automatically if, in place of X and Y, you use FN£CVX(X) and FN£CVY(Y). Therefore line 390 draws the continuous curve by repeated references to:

DRAW FN£CVX(X),FN£CVY(EVAL(A$))

The program starts with some PRINT statements about what it can do, Once the function is entered, it is evaluated in response to the statement EV AL in line 210, 230, 370 and 390. In case this statement is unfamiliar to you, we now discuss it briefly. Essentially the program requests the user to type in a valid BASIC expression in a line such as:

110 INPUT"Y="A$

Suppose the user types in, say, 2*X+5 as response. The INPUT statement then gives A$ this value. Suppose also that the next line of the program uses A$ in an expression involving EVAL in the following form:

Screen Display 11.2a (first part)

120 Y=EVAL(A$)

The result is identical to what would have happened if fines 110 and 120' had been replaced by:

110 Y=2*X+5

The use of EVAL, however, allows a different expression to be entered every time the program is run. If you enter a function that is impossible to evaluate, for example, one involving the square root of a negative number, you will get an error message.

Screen Display 11.2a (second part)

The program next asks for the range of values of X for which to display the equation. It then evaluates the smallest and largest values of X and Y in the display, by working through all the possible points that it will later plot. An error message can be produced at this stage. It might be because the equation produces too large a value for display or because of a division by zero. It is impossible to check for these in advance.
The program may take some time to run, as it works out the co-ordinates for each point twice: once to find the smallest and largest and secondly to plot the points with an appropriate scaling. Consequently the program may pause during execution. The delay depends on the complexity of the calculation and the number of points required. This number can be as small as 20 for a near straight line relationship, but should be more like 100 for a more complex curve.

11.4 Activities


i. Run the program of Listing 11.1 with various functions of your own choosing.

ii. Experiment to see the effect of varying the smallest and largest values of X and the number of steps.

iii. Purposely choose an expression which will result in a division by zero to see the error message produced.


11.5 Displaying two functions together

It is often interesting to display two functions together. For example, you can see where they coincide and hence solve them as simultaneous equations. For viewing two functions on the screen simultaneously, the automatic scaling has to take into account the smallest and largest values of both functions over the range of plotting. This means that you may have to put in some thought when entering the functions. Otherwise the automatic scaling may make one negligibly small compared with the other, as it has to ensure that the composite fills the screen.

Screen Display 11.2 (first part)

In order to display two functions, the program of Listing 11.1 is extended and is given as Listing 11.2. Screen Display ll.2a shows the result for plotting the following two functions: and

Y=EXP(X/40)*COS(X/2)

and

Y=2+C0S(X/2)

You can more precisely identify the point where the two graphs intersect by reducing the range of value is of X for the screen display. We illustrate with Screen Display ll.2b. The functions are the same, but, as you will see from the dialogue between the program and the person running it, the plot is over a smaller range of values for X.

11.6 Activities


i. Use the method of plotting two functions together to solve the following simultaneous equations.

Y = SIN(X)
Y = TAN(X)

Do you get an error message? If so, why might this be? (See Section 1.7.)

ii. Experiment with the range of X values and see how precisely you can get the solution.

iii. Choose some more pairs of functions and solve them as simultaneous equations.


11.7 Discussion of activities

Activity 11.6 i: You might get an error message because TAN(X) goes to infinity if the range of values is not wisely chosen.


Next     Top