38. Appendix: How to add a chapter

Motivation

The purpose of this appendix is to show you how you might contribute a chapter to this book. You should pay attention to the structure of how the topic is presented: what sections do we have at the beginning and end of the chapter, how we introduce exercises, and then be able to start writing about your topic.

Prerequisites

  • An idea you want to write about.

  • Learning how to use the Sphinx documentation system to add to the book. This is described in Section 37.

Plan

In keeping with our hands-on approach I will demonstrate the idea with a sample chapter that is actually an interesting mini-course.

The topic I will use for this demonstration is the quadratic formula. This is an important topic: one of the few formulas that you learn in middle school and then might go on using for the rest of your life.

38.1. Anatomy of the chapter

  1. Motivation, prerequisites, plan

  2. Simplest example of a calculation.

  3. Simplest example of a plot.

  4. A short Python program which generates or analyzes data related to the topic.

  5. Applications to real-world situations, such as science or “life”.

  6. Exercises. Exercises can be inserted at any point in the text. The exercises can also lead in to more advanced work.

  7. Further study. References and ideas to learn more about the topic than can be done in the 1.5 hour mini-course.

I will now show what the sample chapter looks like, prefixing every section title with “The chapter:” and putting notes in square brackets explaining the “pedagogical” motivation for some of what I put into the chapter.

NOTE: from here on we show the mock chapter.

38.2. The chapter: Title

Title: The quadratic formula

38.3. The chapter: frontmatter

Motivation

Our goal here is to learn to find roots of second degree polynomials. What does this mean? If we have a second degree polynomial, which looks like \(ax^2 + b x + c\), we look for values of x that give the result \(ax^2 + bx + c = 0\).

We will work toward the quadratic formula. This, like the Pythagoras theorem, is one of the few formulas which many people continue to use in life after they leave school. This is because second degree polynomials come up in many areas of science, even areas so simple that we might deal with them in life.

Our examples will involve:

physics

figuring out how long it takes a ball that you drop to hit the ground

geometry

or what is the biggest area rectangle you can draw with a fixed perimeter.

Prerequisites

  • The 10-hour “serious programming” course.

  • The “Data files and first plots” mini-course in Section 2

Plan

We will start by introducing the equation to be solved, then we will show the solution, after which we will look at some simple examples and then some examples of scientific calculations we can make with the quadratic formula.

For some very simple polynomials we will see that we can guess the solution, but most of the time it will not be that easy, so we will show:

  • The quadratic formula, which finds the roots to second degree polynomials (Section 38.6).

  • A numerical approximation. We will write a computer program which scans through the real number line looking for zeros (Section 38.7). This technique also applies to more complicated functions than the quadratic equation.

38.4. The chapter: The problem

38.4.1. A basic equation

The problem is that, given numbers a, b and c, we want to find a value of x for which:

(38.4.1)\[a x^2 + b x + c = 0\]

For example, if \(a=1\) and \(b=2\) and \(c=0\) then we have a simple case of Equation (38.4.1):

(38.4.2)\[x^2 - 5x + 6 = 0\]

and a bit of guesswork tells us that both \(x=2\) and \(x=3\) will make Equation (38.4.2) out:

Plugging \(x=3\) into Equation (38.4.2) we get:

\[3^2 - 5 \times 3 + 6 = 9 - 15 + 6 = 4 - 4 = 0\]

And plugging \(x=0\) into Equation (38.4.2) we get:

\[2^2 - 5 \times 2 + 6 = 4 - 10 + 6 = 0\]

so Equation (38.4.2) is satisfied when you plug 2 into x, and also when you plug 3 into x.

For this polynomial we were able to find the roots by guesswork. As I mentioned, most of the time we will not be able to guess the solutions.

38.4.2. Some terminology

Polynomial

A function \(f(x)\) which is a sum of terms with x at various powers. For example \(f(x) = 7x^3 + 2x^2 - 4x + 2\)

Second degree polynomial

A polynomial where the highest power is \(x^2\).

Parabola

The shape of the plot of a second degree polynomial.

Roots of a polynomial

The values for x for which \(y = 0\). These are the solutions to the equations we have been looking at.

Numerical approximation

An approximate solution to a problem found by carrying out calculations that get you closer and closer to a solution.

Numerical analysis

The academic discipline which resolves around finding numerical approximations to the solutions of mathematical problems.

38.4.3. But wait! Two solutions??

[Pedagogical goal: I want to toss in a mnemonic which has always helped me in understanding solutions to equations. A first order equation has up to one solution; a second order equation has up to two solutions, and so forth.]

You should find it notable that there are two solutions to this equation. You might have previously studied equations like \(7 x + 4 = 0\) which have a single solution \(x = -4/7\). So why do we have two solutions for this one?

Here are at least three ways of looking at this:

38.4.3.1. Squaring numbers removes the minus sign

If you take a simple second degree equation like \(x^2 = 16\), you can easily see that \(x=4\) and \(x=-4\) will both give you 16 when you square x. So the existence of the term \(x^2\) means that you can have up to two different values of x that satisfy the same equation.

38.4.3.2. Multiplying two first degree polynomials

Returning to Equation (38.4.2), let’s take a look at the two solutions 2 and 3. Now form two first degree equations that are easily solved by 2 and 3: \(x-2\) and \(x-3\). If one of those expressions is always zero when x is a solution, then the product of them \((x-2) \times (x-3)\) will always be zero. So we can write:

\[(x-2) \times (x-3) = 0\]

But we can also work out that product of \((x-2)\times(x-3)\):

\[(x-2) \times (x-3) = x^2 - 2x - 3x + 2 \times 3 = x^2 - 5x + 6\]

which is our euqation again! This is another way to see clearly that both settings for x, 2 and 3, will make that equation be zero.

38.4.3.3. Plotting the polynomial and looking at zero crossings

If you plot the polynomial, as we will see below in Figure 38.5.1 and other figures in that section, you see that the shape of the plot is what we call a parabola, and that the values of x for which it crosses the x axis also depend on the values of a, b and c.

38.5. The chapter: Plots

A very energetic math professor in college used to always say “Let’s get a picture going!” when embarking on understanding something new.

So here are some plots to get a visual feel for what these solutions are. We start with the simplest polynomial \(y=x^2\) which has a single solution at \(x=0\).

Listing 38.5.1 Simplest second degree polynomial \(y = x^2\)
set grid
plot x**2
../_images/poly-x2.svg

Figure 38.5.1 Plot of the simplest second degree polynomial \(y = x^2\). Note the only root is at \(x=0\).

Then we look at a more complicated second degree polynomial. The shape is the mostly similar, but the position is different. You can see from Figure 38.5.2 that it cuts through the x axis in two points: \(x=2\) and \(x=3\).

Listing 38.5.2 Second degree polynomial \(y = x^2 - 5x + 6\)
set grid
plot [1:4] x**2 - 5*x + 6
../_images/poly-x2-2roots.svg

Figure 38.5.2 Plot of the second degree polynomial \(y = x^2 - 5x\). Note the roots at \(x=2\) and \(x=3\). Also note that we had to narrow the values of x to go between 1 and 4 (instead of the default -10 to 10 that gnuplot does). This is so that we can see more closely where \(y = 0\) occurs.

And now for an example where you have no solutions: y will never be zero.

Listing 38.5.3 Second degree polynomial \(y = x^2 - x + 24\)
set grid
plot x**2 - x + 24
../_images/poly-x2-no-roots.svg

Figure 38.5.3 Plot of the simplest second degree polynomial \(y = x^2 - x + 24\). Note that there are no roots: the plot is entirely above the x axis.

Our final plot will show the example of a plot which points downward. This happens when the \(x^2\) term has a minus sign on it.

Listing 38.5.4 Second degree polynomial \(y = -x^2 + x + 2\)
set grid
plot [-3:4] -x**2 + x + 2
../_images/poly-x2-upside-down.svg

Figure 38.5.4 Plot of the second degree polynomial \(y = -x^2 + x + 2\) which points the opposite way: the minus sign in \(-x^2\) makes the branches of the parabola point down instead of up. It has roots at \(x=1\) and \(x=-2\).

What do we get out of all these plots? We have explored the landscape of second degree polynomials and seen that changing the parameters a, b and c affects the direction and placement of the figure, including the places in which the plot crosses the x axis. These are called the roots of the polynomial.

We have also seen how the values of a, b and c determine if there will be 2 solutions (the two arms of the parabola intersect the x axis), or one solution (the bottom of the parabola grazes the x axis), or no solutions (the parabola is entirely above or entirely below the x axis).

38.6. The chapter: The quadratic formula

Here is our centerpiece: the quadratic formula. The two solutions to the equation

\[ax^2 + bx +c = 0\]

are:

\[x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}\]

As we mentioned above, there can be up to two solutions. Sometimes there will be one solution (when \(b^2-4ac = 0\)), and sometimes there will be no real solutions (when \(b^2-4ac < 0\), since as you know the square root of a negative number is not a real number).

So how do we apply this? Here are some examples from the plots we showed in Section 38.5.

In the equation \(x^2=0\) we have \(a=1, b=0, c=0\) so there is a single root:

\[x = \frac{-0 \pm \sqrt{0^2 - 4 \times 1 \times 0}}{2 \times 1} = 0\]

Looking at \(y = x^2 - 5x + 6\) we have \(a=1, b=-5, c=6\), so the two roots are:

\[x = \frac{-(-5) \pm \sqrt{(-5)^2 - 4 \times 1 \times 6}}{2 \times 1} = \frac{5 \pm \sqrt{25 - 24}}{2} = \frac{5 \pm 1}{2} = (3, 2)\]

Now for the polynomial \(y = x^2 - x + 24\). This has \(a=1, b=-1, c=24\) and the quadratic formula gives:

\[x = \frac{-(-1) \pm \sqrt{(-1)^2 - 4 \times 1 \times 24}}{2 \times 1} = \frac{1 \pm \sqrt{-95}}{2} = \frac{1 \pm \sqrt{-95}}{2}\]

which has no solutions because \(b^2-4ac = -95\) which is negative and thus has no real square roots.

Finally the upside-down parabola \(y = -x^2 + x + 2\) has \(a=-1, b=1, c=2\) and the solutions are:

\[x = \frac{-1 \pm \sqrt{1^2 - 4 \times (-1) \times 2}}{2 \times (-1)} = \frac{-1 \pm \sqrt{5}}{-2} = ((1-\sqrt{5})/2, (1+\sqrt{5})/2)\]

All of these solutions should match the places in the plots where the graph crosses the axes.

38.7. The chapter: Numerical approximation

[Pedagogical goal: it’s important to have a bit of programming for each math/science topic we demonstrate.]

We have two goals in this section: one is to demonstrate how you can write programs to give a numerical approximation to the correct answer.

The other goal is to show a method which will also help with more complicated root-finding problems. You see, quadratic equations are one of the simplest kinds of equations. Once you go to cubic (third degree) polynomials the formula is much more complicated, for quartic (fourth degree) polynomials it’s crazy, and for fifth degree and higher there are no formulae. [1]

“Root finding” is one of the most studied topics in numerical analysis and we will just scratch the surface here, but we will get some good results. Type in the program in Listing 38.7.1 and try running it.

Listing 38.7.1 numerical-zeros.py Look at where a second degree polynomial crosses zero.
#! /usr/bin/env python

"""This program shows an example of looking for roots of a polynomial.
We start with a guess that the roots will be between x = -10 and x= 10
and sample points until we find that y has changed sign.
"""

import math

## FIXME: must write it; look for zero crossings by looking for a
## change in sign

lowest = -10
highest = 10
step = 0.01

def main():
    x = lowest
    y = poly(x)
    previous_sign = -1 if y < 0 else 0
    while x <= highest:
        y = poly(x)
        sign = -1 if y < 0 else 1
        x += step               # move along the x axis
        if previous_sign != sign:
            print('we found a root at approximately x = %g' % x)
        previous_sign = sign

def poly(x):
    y = -x**2 + x + 2
    # y = x**2 - 5*x + 6
    # y = 7*x**2 + 2*x - 40
    ## now try a higher order polynomial
    # y = 2*x**5 - 3*x**4 - x**3 + 2*x**2 + x - 1
    return y

main()

It will tell you an approximate

38.8. The chapter: Applications

38.8.1. Physics: falling bodies

One of the earliest successes of physics, hundreds of years ago, was being able to calculate what happens with falling bodies, both those that fall straight to the ground and those that are constrained (like a pendulum).

The force pulling a body to the ground on the surface of the earth is given by:

\[F = -mg\]

where m is the object’s mass (typically measured in kilograms, kg), and \(g\) is the acceleration of gravity on the earth’s surface (typically measured in meters per seconds squared, \(m/s^2\)).

You can then couple this with Newton’s second law for how any body feels a force:

\[F = m a\]

where a is the acceleration produced by that force. With some elementary calculus you can find what the height of the body is at any moment in time \(h(t)\). If you have not yet studied calculus then bear with me and you will soon see the result which you can just trust.

The acceleration is the second derivative of the position: \(a = \frac{d^2h(t)}{dt^2}\). The solution to the physics equation is:

\[h(t) = h_0 + v_0 t - \frac{1}{2} g t^2\]

where:

  • \(h_0\) is the height from which we drop it. Let’s say we hold it up to 2 meters.

  • \(v_0\) is the initial velocity you give it. In our case \(v_0 = 0\) because we are just letting it drop.

So now our physics question is: after how much time does the body hit the ground?

To answer this we note that we hit the ground when the height is 0: \(h(t) = 0\). If we substitute the physics equation for \(h(t)\) we get:

\[0 = h_0 + v_0 t - \frac{1}{2} g t^2\]

Since \(v_0\) is 0, we have:

(38.8.1)\[h_0 - \frac{1}{2} g t^2 = 0\]

If we put in the values we discussed for initial height and acceleration of gravity (\(h_0 = 2\) and \(g = 9.81\)) we get:

\[-4.905 \times t^2 + 2 = 0\]

Now we can use our quadratic formula with \(a=-4.905, b=0, c=2\) and we get:

(38.8.2)\[t = \frac{-0 \pm \sqrt{0^2 - 4 \times (-4.905) \times 2}}{2 \times -4.905} = \frac{\pm \sqrt{39.24}}{-9.81} = \pm 0.64\]

The two solutions are then \(\pm 0.64\) which is measured in seconds.

In a physics problem, when you have two possible solutions due to a “plus or minus” in the square root you look at which of those times makes sense physically. Clearly the negative solution (it hits the ground in the past) does not make sense, so we conclude that the time we hit the ground is:

\[t = 0.64 \; {\rm seconds}\]

This value of t solves the equation for when the object hits the ground.

38.8.1.1. Exercises for this section

  1. Try dropping a somewhat dense object from a height of 2 meters and see if you can get a good measurement of how long it takes to hit the ground.

  2. Solve the same equation for t using a height of 3 meters and 4 meters, and see if you can safely time how long it takes a body to fall from those heights.

  3. Write out the general solution to Equation (38.8.1). You can use the quadratic formula with \(a=-g, b=v_0, c=h_0\).

  4. Discuss how to pose a more interesting problem in which you give the ball a slight upwards toss. This basically boils down to using a small initial velocity \(v_0\). Then take the more general form of the equation that you just wrote out in the previous exercise and plug in your value for \(v_0\) as well as those for \(h_0, g\), and calculate the time at which the object will hit the ground.

  5. I mentioned that the solution for t has two values, due to the \(\pm\) that comes from solving a second degree polynomial. I told you to ignore the negative time solution, but try discussing with your classmates what the meaning of that negative time solution might be if you started tracking the trajectory of your body before the moment at which you drop it.

38.8.2. Geometry: areas and the Dido problem

[Pedagogical note: I like to introduce associated historical facts to make the subject more fun, to connect it to other subjects, and to remind students that all areas of knowledge shine light on each other.]

Now we solve a geometrical problem, inspired by the legend of Dido, queen of Carthage.

The backstory is that some 8 or more centuries BCE, Dido led a party of Phoenicians to settle an area in what is today Tunisia. She negotiated permission with a local Berber king to found her colony in an area big enough to be encircled by an ox hide.

She cut the hide into very narrow strips, and thus had a fixed length of hide and needed to draw the shape with the biggest area that could be surrounded by that hide.

So “Dido’s problem” is: what shape gives you the most area with a fixed perimeter?

The general mathematical solution of this problem is hard to prove, but we will compare two candidate shapes with a fixed perimeter: a square and a circle. We will then calculate the areas of each and see which would be a better choice.

If we call the perimeter p, then we have:

\[\begin{split}A_c = \pi r^2 \\ p = 2 \pi r\end{split}\]

which gives

\[A_c = \frac{\pi p^2} {(2\pi)^2} = \frac{p^2} {4\pi}\]

For the square:

\[\begin{split}A_{sq} = r^2 \\ p = 2 \pi r\end{split}\]

which gives:

\[A_{sq} = \frac{p^2} {(2\pi)^2} = \frac{p^2} {4 \pi^2}\]

FIXME: must still find the killer app for a quadratic formula on this.

38.9. Exercises for the chapter

[Pedagogical goal: apart from the usefulness of doing exercises, these are formulated to draw students beyond the material they should be prepared for – sometimes even beyond what is reached in high school, like 3rd degree polynomials.]

Third degree polynomial equations like \(a x^3 + b x^2 + c x + d = 0\) have messy-looking solutions (and higher degree polynomals even more so), so we will not look in to them here, but some of these exercises will show some of the cleaner cases that can come up.

Exercise 38.1

On paper expand the expression \((x-3) (x-2) (x-1) (2x-1) (x+1)\) into a polynomial. It should be a fifth degree polynomial. With your classmates find a way to plot it so that you see the ranges well (hint: I came up with setting an x range of [-2:4] and a y range of [-6:14]). Can you guess what the roots are? Does the plot sem to confirm your guess? Discuss with your classmates why the plot diverges so much at the left and right ends.

Exercise 38.2

Repeat the steps in the previous exercise, but this time craft your own sixth degree polynomial. Discuss how the plot is different from the previous one.

Exercise 38.3

Make up a seventh degree polynomial, plot it, then put that polynomial in the program in Listing 38.7.1 to find its roots. Do the roots found in your program seem to match what you see in your plot?

Exercise 38.4

Look for the roots of non-polynomial equations. For example: the \(\sin(x)\) function has roots at \((0, \pi, 2\pi, 3\pi, ...)\) and in fact at any integer multiple of \(\pi\). Can you find approximations to that?

Exercise 38.5

The program in Listing 38.7.1 has a rather coarse step size of 0.01. Discuss how this limits the accuracy of your solution (i.e. by how much have your solutions been “off” of the correct solution?), and try making it smaller to see if the accuracy gets better. Try making it really small (how about 1.0e-6, which is scientific notation for one millionth) and see if the program starts taking too long to execute.

38.10. Further study

https://en.wikipedia.org/wiki/Quadratic_formula

Footnotes