This is an attempt to illustrate numerical integration by animating an HTML5’s canvas.
We’re trying to find the area between x = 1 and x = 5, beneath the parabola:
By integrating, the area under the curve can be calculated as being 17 ⅔ (see below for the analytical solution). For numerical integration, however, the area under the curve is filled with trapezoids and the total area is calculated from the sum of all the areas. As you increase the number of trapezoids, the approximation becomes more accurate. The reduction in the error can be seen on the graph: with 1 trapezoid there is a large gap between the shaded area and the curve; more trapezoids fill in the gap better and better.
The table below show how the error (defined as the difference between the calculation using trapezoids and the analytic solution) gets smaller with increasing numbers of trapezoids (n).
Number of trapezoids
Area (units2)
Error (difference from 17.66)
1
15.00
2.66
2
17.00
0.66
3
17.37
0.29
4
17.50
0.16
5
17.56
0.10
6
17.59
0.07
7
17.61
0.05
8
17.63
0.03
9
17.63
0.03
10
17.64
0.02
Analytic solution
The area under the curve, between x = 1 and x = 5 can be figured out analytically by integrating between these limits.
The effects of changing the constants in the equation of a line (y=mx+b). Image by Tess R.
My high school pre-Calculus class is studying the subject using a graphical approach. Since we’re half-way through the year I thought it would be useful to introduce some programming by building their own graphical calculators using Vpython.
Now, they all have graphical calculators, and Vpython does have its own graphing capabilities, but they’re fairly simple, only 2-dimensional, and way too automatic, so I prefer to have students program the calculators in full 3d space.
My approach to graphing is fairly simple too, but its nice because it introduces:
Co-ordinates: Primarily in 2d (co-ordinate plane), but 3d is easy;
Lists: in this case its a list of coordinates on a line;
Loops: (specifically for loops) to repeat actions and produce a sequence of numbers (with range); and
A sideways glance at matrix-like operations with arrays: A list of numbers can be treated like a matrix in some relatively simple circumstances. However, it’s not real matrix operations: multiplying a scalar by a list works like real matrix multiplication, but multiplying two lists multiplies the corresponding elements in the list.
A Simple Graphing Program
Start the program with the standard vpython header:
from visual import *
x and y axes: curves and lists
Next create the x and y axes. This introduces the curve object and lists, because Vpython draws its curve from a series of points held in a list.
To keep things simple, we’re letting the graph go from -10 to positive 10 along both axes, which makes the x-axis a line segment with only two points:
line_segment = [(-10,0), (10,0)]
The square brackets say that what’s inside as a list. In this case it’s a list of two coordinate pairs, (-10,0) and (10,0).
Now we create a line using Vpython’s curve and tell it that the positions of the points on the curve are the ones we just defined:
xaxis = curve(pos=line_segment)
To create the y-axis, we do the same thing but change the coordinate pair to (0,-10) and (0,10).
In order to be better able to keep track of things, we’ll need some tic-marks on the axes. Ideally we’d like to label them too, but I think it works well enough to save that for later.
I start by having students create the first few tic-marks and then look for the emerging pattern. Their first attempts usually look something like this:
However, instead of tediously writing out these lines we can automate it by noticing that the only things that change are the x-coordinate of the coordinate pairs: they go from -10, to -9, to -8 etc.
So we want to produce a set of numbers that go from -10 to 10, in increments of 1, and use those number to make the tic-marks. The range function will do just that: specifically, range(-10,10,1). Actually, this list only goes up to 9, but that’s okay for now.
We tell the program to go through each item in the list and give its value to the variable i using a for loop:
for i in range(-10,10,1):
mark = curve(pos=[(i,0.3),(i,-0.3)])
In python, everything indented after the for statement is within the loop.
Tic marks on the x-axis.
The y-axis’ tic-marks are similar, and its a nice little challenge for students to figure them out. They usually come up with a separate loop, eventually, that looks something like:
for i in range(-10,10,1):
mark = curve(pos=[(0.3, i),(-0.3,i)])
Our axes.
The Curve
Now to create a line we really only need two points. However, so that we can make other types of curves later on we’ll create a line with a series of points. We’ll create the x and y values separately:
First we set up the set of x values:
line = curve(x=arange(-10,10,0.1))
Note that I use the arange function which is just like the range function but gives you decimal values (so you can do fractions) instead of just integers.
Next we set the y values that go with the x values for the equation (in this example):
line.y = 0.5 * line.x + 2
Finally, to make it look better, we change the color of the line to yellow:
line.color = color.yellow
In Summary
The final code looks like:
from visual import *
line_segment = [(-10,0),(10,0)]
xaxis = curve(pos=line_segment)
line_segment = [(0,-10),(0,10)]
yaxis = curve(pos=line_segment)
mark1 = curve(pos=[(-10,0.3),(-10,-0.3)])
mark2 = curve(pos=[(-9,0.3),(-9,-0.3)])
mark3 = curve(pos=[(-8,0.3),(-8,-0.3)])
mark4 = curve(pos=[(-7,0.3),(-7,-0.3)])
for i in range(-10,10,1):
mark = curve(pos=[(i,0.3),(i,-0.3)])
for i in range(-10,10,1):
mark = curve(pos=[(0.3, i),(-0.3,i)])
line = curve(x=arange(-10,10,0.1))
line.y = 0.5 * line.x + 2
line.color = color.yellow
which produces: A first line: y=0.5x+2
Note on lists, arrays and matrices: You’ll notice that we create the curve, give it a list of x values (using arange), and then calculate the corresponding y values using matrix multiplication: 0.5 * line.x. This works because line.x actually stores the values as an an array, not as a list. The key difference between lists and arrays, as far as we’re concerned, is that we can get away with this type of multiplication with an array and not a list. However, an array is not a matrix, as is clearly demonstrated by the second part of the command where 2 is added to the result of the multiplication. In this case, 2 is added to each value in the array; if it were an actual matrix you need to add another matrix of the same shape that’s filled with 2’s. Right now, this is invisible to the students. The line of code makes sense. The concern is that when they do start working with matrices there might be some confusion. So watch out.
And to make any other function you just need to adjust the final line. So a parabola:
would be:
line.y = line.x**2
(The two stars “**” indicates an exponent).
An Assignment
So, to assess learning, and to review the different functions we’ve learned, I asked students to produce “studies” of the different curves by demonstrating what happens when you change the different constants and coefficients in the equation.
For a straight line the general equation is:
you what happens when:
m > 1;
0 < m < 1;
m < 0
and:
b > 1;
0 < b < 1;
b < 0
The result is, after you add some labels, looks something like the image at the very top of this post.
This type of exercise can be done for polynomials, exponential, trigonometric, and almost any other type of functions.
A wonderful set of physical demonstrations of the different perspectives that come from different frames of reference. Excellent for physics, and maybe math too, because it does point out coordinate systems.
[- Leacock (1960): Frames of Reference, Presented by Ivey and Hume, via the Internet Archive.]
From the Coriolis Interactive Model.
The discussion of non-inertial (accelerating) frames of reference is particularly good, and would tie in well with the coriolis model demonstration.
Scale of the Universe 2, by Cary and Michael Huang. WARNING: look out for ads on the site.
In a modern variant on the Powers of Ten video, Cary and Michael Huang have created an excellent flash game that spans the scale of the universe, from the smallest, only hypothesized particles, through atomic, human, planetary, and galactic scales (to name a but a few), to the size of the universe itself. It goes further than the Cell Size and Scale flash app.
The link is here, but look out for an advertisement that takes up the game window, which will eventually let you through (or you can click the “Skip Ad” link on the bottom right of the ad).
After working with the hundred-squares, ten-bars, and thousand-cubes to figure out how to add polynomials, we borrowed the binomial and trinomial cubes to practice multiplying out factors. It’s a physical way of showing factor multiplication.
Binomial Square
You can first look at the binomial cube in two-dimensions as a binomial square by just finding the area of the top layer of four blocks.
If you label the length of the side of the red block, a, and the length of the blue block, b, you can calculate the areas of the individual pieces simply by multiplying their lengths times their widths.
Looking from the top down, the top layer of the binomial cube is a binomial square.
Adding up the individual areas you get the area of the entire square:
However, there is another way.
If you recognize that the length of each side of the entire square is equal to (a+b).
The length of each side of the cube is the sum of the lengths of the two squares.
Then the total area is going to be total length (a+b) times the total width (a+b):
which simplifies to give the same result as adding up the individual areas:
The Binomial Cube
We can do the same thing using the entire cube by recognizing that the volume of the cube is the length times width times the depth, and all of these dimensions are the same: (a+b).
Using the full binomial cube.
Now the students can go through the same process of multiplying out the factors, and can check their work be seeing if they get the same number of pieces (and dimensions) as the physical cube.
Working with the thousand cube, hundred square, ten bar and unit cells in algebra.
I sent a couple of my algebra students down to the pre-Kindergarten classroom to burrow one of their Montessori works. They were having a little trouble adding polynomials, and the use of manipulatives really helped.
The basic idea is that when you add something like:
you can’t add a n3 term to a n2 or a n term. You only combine the terms with the same degree (and same variables). So the equation above becomes:
which simplifies to:
The kids actually enjoyed the chance to run downstairs to burrow the materials from their old pre-K teacher (and weren’t they quite good about returning the materials when they were done with them).
And it clarifies a lot of misconceptions when you can clearly see that that you just can’t add a thousand cube to a ten bar — it just doesn’t work.