# 1. Objective

• To introduce the concept of numerical integration
• To have an understanding of the trade-off 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 $L\cdot W$ or $\pi\cdot r^2$.

# 3. Pre-questions

How would you determine the area of the shape?

# 4. Slides

## 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. Tile Size

• Would Kelper have come up with his rule if the tiles were huge in comparison to his drawing?
• Probabaly not

## 4.5. Basic Rules

• 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. Histogram

• You have already encountered this concept before
• How many people were surveyed?
• Bonus question: Something is fishy about these survey results.

• 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. Question

• Why not use tiles that are shaped like triangles?!

• Most of the time the shape of the curve is not so simple (show diagram).

## 4.9. Alternative to Tiles

• 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.10. 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 rectangles may have any height.

## 4.11. Rules cont.

The possibilities for rectangle heights.

• Left: Always make the rectangle the height of the curve at the left 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.12. 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.13. Rectangle Width

• The equivalent to choosing a tile size is choosing a rectangle width and an algorithm for picking the rectangle height.
• Smaller-width rectangles lead to more computation ...
• ... but more accuracy

## 4.14. Estimates using Matlab

Left

w = 1.0;
for i = [1:4]
H(i) = w*(i-1.0);
end
H
sum(H)


Middle

w = 1.0;
for i = [1:4]
H(i) = w*(i-0.5);
end
H
sum(H)


Right

w = 1.0;
for i = [1:4]
H(i) = w*i;
end
H
sum(H)


## 4.15. Questions

### 4.15.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?

### 4.15.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?

## 4.16. Numerical Integration in Matlab

### 4.16.1. Example I

How would you determine the area of the shape?

>> M = imread('http://cds130.org/wiki/images/thumb/complicatedshape.svg/180px-complicatedshape.svg.png');
>> [A,map] = rgb2ind(M,2)



### 4.16.2. Example II

A driver drives his car at an unsteady speed. The speed (x1) changes from time to time as shown in the following graph.

• Based on this figure, how far has he traveled after time t ?
• Can you make a plot showing the distance he travels as a function of time?

Let's suppose x1(t) = cos(2 * t) + 2

clear all;
y = 0; % cumulative sum of distance

for t=0:0.1:10     %discretization
y = y+ t*(cos(2*t)+2); % integration
end

%print out the result
fprintf('At time %f (s), the total distance traveled is %f (m). \n', t, y);


This is still not enough to make the plot. We can revise the code:

clear all;

y = 0; % cumulative sum of distance
i = 0;
for t=0:0.1:10     %discretization
y = y+ t*(cos(2*t)+2); % integration
i = i +1;
time(i) = t;
distance(i) = y;
end

plot(time, distance);
legend('distance');
xlabel('time');
ylabel('distance')



Now, let's start with the same vectors as before.

 t=0:0.1:10;
x1=cos(2*t)+2;
y=cumsum(x1.*diff(t));


This generates an error because x1 is incompatible with the vector returned by diff(), as you can tell by checking their sizes. So we can do the following:

y=cumsum(x1(1:(length(x1)-1)).*diff(t));
y(length(y)+1)=y(length(y));
plot(t,x1,t,y)
legend('cos(2*t)+2','integral')


Q: Explain why the variable y will be approximately the integral of x1. You might want to refer to the image above when answering.

Q: Is the function y what you expect? Why or why not?