Numerical Integration
From CDS 130
1. Objective
 To introduce the concept of numerical integration
 To have an understanding of the tradeoff between number of calculations and accuracy of an estimate of area.
2. Motivation
 In scientific computing, we often want to figure out the area enclosed by a set of lines.
 Most often, those set of lines do not correspond to a shape for which we have an analytic formula, such as or .
3. Priming questions
4. Notes
4.1. Introduction
 Kepler was an astronomer born in 1571 who plotted data of the location of planets with respect to the sun.
 Like all scientists, he wanted a simple rule to explain lots of numbers. In his case his numbers were values of the positions of the planets throughout the year.
4.2. Introduction cont.
 One day his child was playing with little square lego blocks.
 He placed these blocks on his sheet of paper and found that 131 fit into the area "swept" out by the planet from point A to B (which took about 3 months).
 He then placed these blocks in the area "swept" out by the planet from C to D (which took 3 months).
 He noticed that he needed 131 legos for this area too.
4.3. Introduction cont.
 He then placed these blocks in the area "swept" out by the planet from C to H (which took about 3 months).
 He noticed that he needed 131 legos for this area too.
4.4. Introduction cont.
 Would Kelper have come up with his rule if the tiles were huge in comparison to his drawing?
 Probably not
4.5. Introduction cont.
 You can estimate areas by placing tiles on the area and counting the number of tiles required to cover the area.
 Smaller tiles give a better estimate of the area.
4.6. Introduction cont.
 You have already encountered the concept of estimating areas using squares before
 (a rectangle can be thought of as a bunch of stacked squares)
 How many people were surveyed?
 Bonus question: Something is fishy about these survey results.
Spreadsheet for this histogram
 Bonus answer: It is quite unlikely that the numbers in each bin would be a multiple of ten. It is also suspect that nobody chose 8 or 9.
4.7. Basic concept
Takeaway message from introduction:
 Estimate shapes with squares (or rectangles = stacks of squares)
 Smaller squares give better estimate
 More squares require more computation (counting)
4.8. Using a computer
 In this class, we will consider only "simple" shapes.
 always draw shapes on an xy scale
 the x and y axes will be the lower and upper boundaries, respectively
4.9. Approach A
Approach A for laying down tiles. Stack tiles until the righttop corner of one of the tiles is at or above the upper line.
4.10. Approach B
Approach B for laying down tiles. Stack tiles until the lefttop corner of one of the tiles is at or above the upper line.
4.11. Question
 Why not use tiles that are shaped like triangles?!
4.12. Answer
 Most of the time the shape of the curve is not so simple.
4.13. Alternative to Square Tiles
 This is the approach that we will use.
 Instead of counting tiles (squares), you add up the area of rectangles.
 What is the advantage of this?
 This will be the method used when we do the calculation with a computer.
4.14. Rules
 Use rectangles to compute the area of the triangle.
 Rule: you may use only four rectangles
 Rule: your rectangles do not need to completely cover (obscure) the triangle
 Rule: rectangles must have a width of 1 block
 Rule: The height of the rectangle for a given problem is always chosen based on one of three rules.
4.15. Rules cont.
The possibilities for rectangle heights.
 Left: Always make the rectangle the height of the curve at the left side of the rectangle. In the leftmost image, the first rectangle has a height of zero.
 Middle: Always make the rectangle the height of the line at the middle of the rectangle
 Right: Always make the rectangle the height of the line at the right side of the rectangle
4.16. Rules Summary
 Rectangles can be used to estimate area
 There are choices for how to choose the height of the rectangles
 Now repeat the rectangle exercise using rectangles of width 0.5.
4.17. Rectangle Width
 The equivalent to choosing a tile size is choosing a rectangle width and an algorithm for picking the rectangle height.
 Smallerwidth rectangles lead to more computation ...
 ... but more accuracy
4.18. Estimates using a spreadsheet
 This spreadsheet shows the area of all rectangles for the left, middle, and right choices of rectangle heights [2]
The possible rules to use for determining rectangle heights:
 Left: Always make the rectangle the height of the curve at the right side of the rectangle
 Middle: Always make the rectangle the height of the line at the middle of the rectangle
 Right: Always make the rectangle the height of the line at the right side of the rectangle
4.19. Estimates using program
In some cases, where we have a simple equation for the height of the actual curve (e.g., y=.33x^2), we can use iteration to compute the sum. To determine the required for loop, write out the calculations that must be performed without iteration first. Then write a for
that does the same calculation.
4.19.1. Using "Left" rule
H(1) = 1*0; H(2) = 1*1; H(3) = 1*2; H(4) = 1*3; a = H(1) + H(2) + H(3) + H(4); 
w = 1.0; for i = [1:4] % 4 rectangles, so need 4 iterations. H(i) = w*(i1.0); % or 1.0*(i1.0) end a = H(1) + H(2) + H(3) + H(4); % or a = sum(H) 
4.19.2. Using "Middle" rule
Write out the longhand version of the program to the right. 
w = 1.0; for i = [1:4] H(i) = w*(i0.5); end H sum(H) 
4.19.3. Using "Right" rule
H(1) = 1.0*1; H(2) = 1.0*2; H(3) = 1.0*3; H(4) = 1.0*4; 
Write out the forloop version of the set of commands to the left. 
4.20. Estimates using program II
Repeat the above three cases except use rectangle with onehalf the width.
5. Questions
5.1. Tile Size
Suppose that you want to estimate the area of room by laying down tiles.
 What is an advantage of using small tiles to estimate the area?
 What is an advantage of using big tiles to estimate the area?
Answer 


5.2. Rectangle motivation
When we are estimating area using a computer, we add up the area of rectangles instead of the area of a bunch of tiles (squares) or triangles. Why?
Answer 


5.3. Algorithm for splitting rectangles
The image below shows how a student doubled the number of rectangles used to estimate the area in the blue box. They just drew a line through the middle of each rectangle.
 Why is this approach not useful?
 Suggest a better algorithm for doubling the number of rectangles.
5.4. Numerical Integration

5.5. Numerical Integration

5.6. Numerical Integration
In this problem, we are given an equation that represents the height so we can compute the area by hand calculations and by iteration.
Compute the area between the lines y=0, x=3, and y=x^{2} using rectangles of width 1. Use this graph paper and let 1 unit be the width of a square.
Answer 

In the image below, three dots were drawn at xvalues of 1, 2, and 3. The yamplitude was computed using the formula y=x^{2} to give yvalues for the dots of 1^{2}=1, 2^{2}=4, and 3^{2}=9. The width of each rectangle is 1, so the total area is the sum of the areas of the three rectangles, or 1*1 + 1*4 + 1*9 = 14. 
Repeat the above using rectangles of width 1/2 of a square.
Answer 

In the image below, three dots were drawn at xvalues of 0.5, 1, 1.5, 2, 2.5, and 3. The yamplitude was computed using the formula y=x^{2} to give yvalues for the dots of 0.5^{2}=0.25, 1^{2}=1, ..., 2.5^{2}=6.25, and 3^{2}=9. The width of each rectangle is 0.5, so the total area is the sum of the areas of the six rectangles, or 0.5*0.25 + 0.5*1 + 0.5*2.25 + 0.5*4+ 0.5*6.25 + 0.5*9 = 11.375. 
Write down your rule for selecting the heights of the rectangles in 1. and 2. What would an advantage be of having a simple rule or a rule that is the same for all rectangles as opposed to a rule that depends on x?
Answer 

For the first problem, the rule was to draw a horizontal line from x=0 to x=1 on the xaxis. Then draw a vertical line up to x=1^{2} and draw a dot. The two lines represent two sides of the first rectangle. Next draw a line from x=1 to x=2 on the xaxis. Then draw a vertical line up to x=2^{2} and draw a dot. The two lines represent two sides of the second rectangle. Etc. For second problem, the rule was to draw a horizontal line from x=0 to x=0.5 on the xaxis. Then draw a vertical line up to x=0.5^{2} and draw a dot. The two lines represent two sides of the first rectangle. Next draw a line from x=0.5 to x=1.0 on the xaxis. Then draw a vertical line up to x=1^{2} and draw a dot. The two lines represent two sides of the second rectangle. Etc. 
Write a program without a for
to compute the area using w=1.
Answer 

To figure out how to write a program to do the computation with a for loop, write out the steps in longhand form. clear; w = 1; A(1) = w*1*1; A(2) = w*2*2; A(3) = w*3*3; Atotal = A(1)+A(2)+A(3) now, rewrite the above using an iteration variable. clear; w = 1 i = 1; A(i) = w*i*i; i = 2; A(i) = w*i*i; i = 3; A(i) = w*i*i; Atotal = A(1)+A(2)+A(3) 
Rewrite the program with a for
loop.
Answer 

Both programs above correctly compute the area. The last program can be rewritten as clear; w = 1; for i = [1:3] A(i) = w*i*i; end Atotal = sum(A) Note that the last line was rewritten using the z = sum([1,2,4]) will assign the value 
Answer the last two questions using w = 0.5.
Answer 

Here is a program that almost does what we want. The problem is that the array clear; w = 0.5 i = 0.5; A(i) = w*i*i; i = 1.0; A(i) = w*i*i; i = 1.5; A(i) = w*i*i; i = 2.0; A(i) = w*i*i; i = 2.5; A(i) = w*i*i; i = 3.0; A(i) = w*i*i; Atotal = sum(A) the above (nonvalid) program can be rewritten as clear; w = 0.5 for i = [0.5:0.5:3.0] A(i) = w*i*i end Atotal = sum(A) To make this program work, we need the array clear; w = 0.5 k = 1; for i = [0.5:0.5:3.0] A(k) = w*i*i; k = k+1; end Atotal = sum(A) If you rewrite the above in longhand notation, you will see that it does the correct calculation. 
5.7. Numerical Integration
The following program is used to estimate the area under the curve y = x^{2}/4 between x = 0 and x = 5:
A = 0; for i = [1:5] A = A+ (i)^2/4; end
Sketch the curve and the rectangles that correspond to the program above. Label the height of each rectangle.
6. Activity
6.1. Numerical Integration
Use vertical rectangles to estimate the areas of the objects shown on the following handouts.
 Handout #1: pdf version  svg version
 Handout #2: pdf version  svg version
6.2. Numerical Integration
In this problem, we are given an equation that represents the height so we can compute the area by hand calculations and by iteration.
 Compute the area between the lines y=0, x=3, and the curve y=0.3333·x^{3} using rectangles of width 1. Use this graph paper and let 1 unit be the width of a square. Turn in your diagram at the start of class. Include your calculations on your paper. You do not need to put your answers to this question on your wiki page.
 Write a program without a
for
to compute the area using w=1.
Answer clear; w = 1; for i = 1:3 A(i) = w*0.3333*i*i*i; end Atotal = sum(A) % Gives Atotal = 11.998799999999999
 Write a program with a
for
to compute the area using w=1.
Answer clear; w = 1; A(1) = w*0.3333*1*1*1; A(2) = w*0.3333*2*2*2; A(3) = w*0.3333*3*3*3; Atotal = A(1) + A(2) + A(3) % Gives Atotal = 11.998799999999999
or
clear; w = 1; i = 1; A(i) = w*0.3333*i*i*i; i = 2; A(i) = w*0.3333*i*i*i; i = 3; A(i) = w*0.3333*i*i*i; Atotal = sum(A) % Gives Atotal = 11.998799999999999
 Repeat all of the above using using w = 0.5.
Answer clear; w = 0.5; A(1) = w*0.3333*0.5*0.5*0.5; A(2) = w*0.3333*1*1*1; A(3) = w*0.3333*1.5*1.5*1.5; A(4) = w*0.3333*2*2*2; A(5) = w*0.3333*2.5*2.5*2.5; A(6) = w*0.3333*3*3*3; Atotal = A(1) + A(2) + A(3) + A(4) + A(5) + A(6) % Gives Atotal = 9.186581250000000
or
clear; w = 0.5; i = 1; A(i) = w*0.3333*(i/2)*(i/2)*(i/2); i = 2; A(i) = w*0.3333*(i/2)*(i/2)*(i/2); i = 3; A(i) = w*0.3333*(i/2)*(i/2)*(i/2); i = 4; A(i) = w*0.3333*(i/2)*(i/2)*(i/2); i = 5; A(i) = w*0.3333*(i/2)*(i/2)*(i/2); i = 6; A(i) = w*0.3333*(i/2)*(i/2)*(i/2); Atotal = sum(A) % Gives Atotal = 9.186581250000000
Answer clear w = 0.5; k = 1; for i = [0.5:0.5:3] A(k) = w*0.3333*i*i*i; k = k+1; end Atotal = sum(A) % Gives Atotal = 9.186581250000000
or
clear; w = 0.5; for i = 1:6 A(i) = w*0.3333*(i/2)*(i/2)*(i/2); end Atotal = sum(A) % Gives Atotal = 9.186581250000000
Note Note that the exact answer is
The following program computes the area using 100 rectangles with widths of 1, 1/2, 1/3, ..., 1/100; the first element in the array
Atotal
is for width=1 and the last is for width = 100.clear; for j = 1:100 w(j) = 1/j; k = 1; for i = [w(j):w(j):3.0] A(k) = w(j)*0.3333*i*i*i; k = k+1; end Atotal(j) = sum(A); end Atotal(end) % Atotal(end) is 6.794395492500002 (to be compared with the % exact value of 6.749325000000000)