Using Maple for Numeric IntegrationBackground for numeric integrationWhen our book first defined the definite integral, it defined it as signed area under a curve. This was computed as the limit of an approximating sum (a Riemann sum), usually the sum of the areas of a collection of rectangles. The book then showed that the definite integral was the limit of any Riemann sum as the number of subintervals went goes to infinity and the size of the largest subinterval goes to zero. When we use the sums to find an approximation of a definite integral we say that we are doing numeric integration. Numeric integration is the main subject of chapter 7. Maple has a number of functions available to look at such sums and do numeric integration.Before we start doing any numeric integration, first need to read in student, a package of procedures designed to be useful for students taking calculus.Place your cursor in the red input section below and hit enter to load the packages.with(Student[Calculus1]);(Maple returns a list of functions that are now defined with the packages.)An example using the right hand rule:Now we are ready to explore some of the procedures given. The example we want to consider is NiMtJSRpbnRHNiQtJSRzaW5HNiMqJiUieEciIiMqJiIiJSIiIiUjUGlHRi4hIiIvRio7IiIhKiZGK0YuRi9GLg==. This is not an integral that we can do with the antiderivatives and the fundamental theorem of calculus.In section 7.1 of our book (page 88), the authors give some commands for calculating approximating sums in one version of Maple. Unfortunately, the commands don't quite work in the version of Maple that we have here.With Maple 9.5, the procedure we want to look at is ApproximateInt.In its simplest form, ApproximateInt uses the midpoint rule with 10 subintervals, simplifying the answer if possible.(Put your cursor in each red input section in turn and hit enter.)Answer1 := ApproximateInt(f(x), x=0..5);
Answer2 := ApproximateInt(x, x=0..5);We would like to use some of the options. Maple lets you specify the number of partitions and the type of Riemann sum used. To illustrate our work we want to use the right hand rule, with the interval broken into 7 subintervals.The nicest answer is when we plot the output. we would also like to see the formal sum and get a numeric value. First the plot.ApproximateInt(sin(x^2/(4*Pi)), x=0..2*Pi,method=right, partition=7,output=plot);Next we look at the sum.ApproxSum := ApproximateInt(sin(x^2/(4*Pi)), x=0..2*Pi,method=right, partition=7,output=sum);Then we get rid of the sigma notation:ApproxValue := ApproximateInt(sin(x^2/(4*Pi)), x=0..2*Pi,method=right, partition=7,output=value);Finally we evaluate the sum numerically:evalf(ApproxValue);For comparison, we take the numeric integral with Maple and check the error:evalf(int(sin(x^2/(4*Pi)), x=0..2*Pi));
IntError := evalf(int(sin(x^2/(4*Pi)), x=0..2*Pi) -
ApproxValue);For comparison, the next block of commands does these things with one section. It is designed to easily change the function, or boundary points, (One of the reason for putting it in a single section is to make it easy for you to cut and paste the section into the exercises. You can then modify the code appropriately to do other problems.)func := sin(x^2/(4*Pi));
lowx := 0: hix := 2*Pi: numbox := 7:
ApproximateInt(sin(x^2/(4*Pi)), x=0..2*Pi,method=right, partition=7,output=plot);
ApproxSum := ApproximateInt(sin(x^2/(4*Pi)), x=0..2*Pi,method=right, partition=7,output=sum);
ApproxValue := ApproximateInt(sin(x^2/(4*Pi)), x=0..2*Pi,method=right, partition=7,output=value);
rightapprox:= evalf(ApproxSum);
numint:= evalf(int(func, x=lowx..hix));
IntError:= numint - rightapprox;Exercises:Time to try some problems on your own.1) Redo the example above with 7 boxes replaced by 10 and 20 boxes. 2) Repeat the example above using the left hand rule or the midpoint rule. (The method becomes left and midpoint respectively.)3) The classic problem for numeric integration is area under the normal curve. SAT verbal and mathematics scores are supposed to fall in a normal distribution with a mean of 500 and a standard deviation of 100. Thus a score of n corresponds to being NiMvSSJzRzYiKiYsJkkibkdGJSIiIiIkKyYhIiJGKSIkKyJGKw== standard deviations above the mean. In terms of percentile, a score s standard deviations above the mean is at the (NiMsJiIjXSIiIiomIiQrIkYlLUkkaW50RzYiNiQqJi1JJGV4cEdGKjYjLCQqJkkieEdGKiIiI0YzISIiRjRGJS1JJXNxcnRHRio2IyomRjNGJUkjUGlHSSpwcm90ZWN0ZWRHRjpGJUY0L0YyOyIiIUkic0dGKkYlRiU=)th percentile. Use 10 boxes to approximate the percentile ranking of someone who scored a 650 on the mathematics test.Warnings:(a) The 50 should be added in after the integrating.(b) All of the commands for numeric approximation will fail to work unless the "with(Student[Calculus1]);" command has been previously executed in the same session.4) Estimate the percentile ranking of a score of 790. Use theorem 1 to predict how many boxes are needed to be accurate to.01.The number of boxes needed is ...Checking convergence on numerical integration methodsOne of the nice things about Maple is that it can do a lot of computations quickly. This lets us look at how quickly a method of numerical integration converge.Let us return to our example, NiMtJSRpbnRHNiQtJSRzaW5HNiMqJiUieEciIiMqJiIiJSIiIiUjUGlHRi4hIiIvRio7IiIhKiZGK0YuRi9GLg== , and see what happens if we increase the number of boxes for the right hand rule. The loop below starts with 10 boxes and doubles it 7 times to about 1000 boxesfunc := sin(x^2/(4*Pi));
lowx := 0: highx :=2*Pi:
for i from 0 to 7 do
print([10*2^i, evalf(ApproximateInt(func,
x=lowx..highx, method=right, partition=10*2^i,
output=sum))]);
end do;For comparison, we look at the results of Maple's numeric integration.evalf(int(func, x=lowx..highx));Thus we see that 1000 boxes is accurate to 5 decimal places in this example.It is also instructive to see how the various sum rules compare for accuracy. The following command set looks at how 5 sum rules compare in our example.func := sin(x^2/(4*Pi));
lowx := 0: highx :=2*Pi:
print([`numeric integration yields: `, evalf(int(func, x=lowx..highx))]);
print([`n`, `lsum`, `midpoint`, `trapezoid`, `rsum`, `simpson`]);
for i from 0 to 5 do
print([10*2^i,
evalf(ApproximateInt(func, x=lowx..highx,
method=left, partition=10*2^i, output=sum)),
evalf(ApproximateInt(func, x=lowx..highx,
method=midpoint, partition=10*2^i, output=sum)),
evalf(ApproximateInt(func, x=lowx..highx,
method=trapezoid, partition=10*2^i, output=sum)),
evalf(ApproximateInt(func, x=lowx..highx,
method=right, partition=10*2^i, output=sum)),
evalf(ApproximateInt(func, x=lowx..highx,
method=simpson, partition=10*2^i, output=sum))
]);
od;There are several thigs you should notice in the example:1) Simpson's rule converges much faster than any of the other methods. At the same time, it is worth noting that all the methods have actual error that is smaller than the theoretical error.2) In this example, both the right hand rule and the left hand rule underestimate. (Since f(a) = f(b), the left hand rule and the right hand rules will always agree with each other and with the trapezoid rule.)3) The midpoint rule is somewhat better than the right hand rule or the left hand rule. Exercises:Time to try some problems on your own.5) Redo the example above, replacing the integral given with the integral to find the percentile of a score of 700 on the SAT verbal test. (That integral was discussed in part a of this worksheet.)6) Redo the example above, replacing the integral given with the integral NiMtSSRpbnRHNiI2JC1JJGV4cEdGJTYjLUkkc2luR0YlNiNJInhHRiUvRi07LCQqJiIjXSIiIkkjUGlHSSpwcm90ZWN0ZWRHRjVGMyEiIiomIiRdIkYzRjRGMw== from exercise 14 on page 122. [A warning on this exercise. The Maple command for numeric integration cannot handle this integral. It runs into a singularity. Thus you are forced to either rely on approximating sums, or find a clever way to do the integral over a smaller region.]Use the alternate method that the book suggests.7) Experiment and have fun.