Session 1: Computers, Code, & Functions - Module D: Functions & Plotting

1. Discussion: What is a Function?

What is a function, anyway?
What are not functions?
Variables and function notation.

2. Evaluating Functions

We would often like to be able to find the value of f(x) for a particular value of x. This is called evaluating the function.

Let's use the function f(x) = 2x + 3 as an example. We'll evaluate the function when x is 3. In order to evaluate the function, 'plug in' the value 3 for x. Replace all x with 3 so that you have: f(3) = 2*3 + 3. Now simplify the right side: f(3) = 6 + 3 = 9. So f(3) = 9. We say that the value of the function f at 3 is 9.

For each of these functions, evaluate f(x) for x = 0, x = 1, and x = 2. Build a table of values for each function.

3. Plotting Functions by Hand

Let's plot the function f(x) = 2x + 3.

Start by choosing several 'good' values for x. A 'good' value of x is one for which it is easy to calculate f(x), and which is easy to plot. You need several x values that are a reasonable distance apart; if you use too few points, or points that are too close together, it will be hard to tell what the plot looks like. x = 0, 1, 4, and 5 are some decent choices for this function. Fill in a table of x values and corresponding f(x) values (the first has been filled in already):

xf(x)
03
1
4
5

Next, grab some graph paper and draw a pair of coordinate axes. Plot each of the (x, f(x)) points from your table by using x as the x-coordinate and f(x) as the y-coordinate.

Now finish the plot by connecting the points with a curve that seems to best fit. Since this is a linear function (can you see why?), the plot will be a straight line. Use a ruler to draw a line through the points.

Follow a similar process, choosing your own values for x, to plot each of the following functions. (Be careful with the last one - it's not a straight line!)

Have you noticed a shortcut for plotting linear functions? Since you know the plot will be a straight line, you don't need to find a bunch of points - in fact, just two will do!

4. Estimating Function Values from a Graph

You can use a plot to estimate values of f(x) without actually evaluating the function.

Use your first plot (for f(x) = 2x + 3) to estimate the value of f(3.5):

  1. Start on the x-axis, where 3.5 should be.
  2. Move straight up from the x-axis, until you hit the line.
  3. Move straight left until you hit the y-axis.
  4. Estimate the value you have reached on the y-axis. This is your estimate for f(3.5).

Now evaluate f(3.5). Was your estimate close?

Choose some of your plots and use them to estimate the following:

5. Plotting Functions with a Computer

Plotting functions by hand as we did earlier can be used to get a good idea of what a bunch of different functions look like. But it can get rather tedious. And what happens if you want to plot something like f(x) = 100x6 - 99x4 + x - 3? Even working out the points to plot would be challenging! A computer can do a lot of calculations very quickly; we'd love to have it plot functions for us.

Before we can begin, we need to create a new directory for the programs that you will make this week. Let's begin by opening a terminal (remember: top left corner of the screen) and typing the following commands:

mkdir programs
cd programs/

Now we need to download a Python library called macslib. A library contains resources that many other programs can use. This particular file contains tools that will help us create plots over the next few days. Save it in your new programs directory as macslib.py.

We're ready to see some graphs! Let's plot the function f(x) = 4x2. Follow along as I demonstrate.

  1. Start IPython:

    ipython

  2. We need to tell IPython where to find the plotting library. Type the command:

    from macslib import *

  3. The plotting library includes a tool called drawFunction() which will plot functions for us. Use it to plot f(x) by typing:

    drawFunction('4 * x^2')

Plot another function by calling drawFunction() again using a different value between the single quotes. Use the program to check your hand plots from the functions we plotted earlier:

How close do your hand plots look to the computer plots? Once you've finished plotting these functions, you can try making up your own functions to plot. Here are some you might try: x3, -x2, x2 + 3x + 1, sin(x), cos(x), tan(x), sqrt(x), log(x), abs(x).

Save your favorite plot and answer these questions on your website.