Midpoint, Trapezoid, and Simpson's Rule

Start   we finished pretty much with techniques of integration. uhh as you noticed no clickers today because ive been having a lot of problems so umm midterm is 2 weeks from tomorrow night.
it will cover stuff that we havent done yet itll cover through section 6.3 in which we will definitely get through before the midterm.
0:30is this on?
I just dont sound very loud can you hear me okay?
umm what else?
if youre having...ive been having receiving increasingly increasing emails about this is hard I cant do it.
if you cant do it this is why we have office hours
1:00but theres a lot unavailable.
so the math learning center is open quite a number of hours it tends to be open something like 10 to 8 monday through thursday and also fridays until about 3 the drawback of the math learning center is it drop in and so you get what you get and sometimes you have to fight for help.
there are 8 of us I think associated with this course
1:32each of us have 3 office hours so that makes 24 office hours uhh a number of them are in the math learning center but theres still quite a few you can use and you can also make it a point to go outside office hours.
ive seen exactly 2 people so far in this course for office hours.
thats pretty bad out of 340.
theres not 340 in this room but still.
2:03you know I dont mind if you dont come ive got stuff to do but im there in case you have questions.
anything else?
I dont know.
any questions or issues people are having?
no did I go along with academics? what does that mean?
like youre having problems with other stuff?
2:32youre having problems with the material? sure.
well then you should ask.
okay great yeah! yay!
sorry go ahead.
so the partial fractions and..
3:02I dont I mean I dont want to say your questions arent..i dont care but I mean I can go over 1 more partial fractions ive sort of gone over that so I would recommend that what you do if youre having problems with partial fractions and integration by parts just come see me after.
and we'll go over it together.
you know I kind of said all I can if you have specific questions about partial fractions or integration by parts
3:32just the whole idea. okay so then definitely thats an office hour kind of thing.
other questions or issues people are having?
I guess not cause he just asked and I shot him down.
I suck.
okay so lets move on.
so I want to point out a few things. weve covered all the basic techniques in fact less than basic.
all of the techniques of integration.
4:00you know theres some special tricks that you can use hyperbolic signs things like that but for the most part, thats it.
you should be able to do pretty much probably more integrals than I can do cause im actually not very good at doing integrals.
there are a there are a lot of and I was going to show it but I dont have the laptop there are computer programs that can integrate better than anybody.
So theres a program called Maple
4:31which you can download from softweb you have a license for it by virtue of being a student here.
There's a program called mathematica, same thing theres a free program called octave there are a number of programs where you just type in the integral and you and then you know in syntax like int x^3 x and it says the answer is 1/3 no it doesnt
5:01it says the answer is 1/4x^4 so it just does the integral for you.
so there are programs that will do this stuff for you probably better than either you or I could do they do a whole lot more than just that.
because integral is just a very mechanical process theres a bunch of techniques, you mess around with it, out comes the answer.
same thing with derivatives and so on.
but theres a lot of things
5:33so how would you do this integral?
any clue?
try parts I would try parts too.
it wont work.
then what?
whatever you try, it wont work.
so this integral or
6:04this integral or... let me get another one that doesnt have an e in it well this one sort of has an e in it this integral or get one with a sin this integral
6:30or this integral none of these integrals. and hundreds of others, thousands, infinitely many others none of these integral have a nice formula to express what they are.
but they are well defined functions.
they make sense. they are real things they represent actual quantities.
um so theres an area underneath the curves involved
7:02in all of these things.
so theres an integral here.
but we just dont have the symbols to represent it.
now sometimes if you need to use this people will make up formulas I think this one has a special name ..SI this one I know for sure is something
7:37something like that I always forget exactly how it goes theres a thing called the error function this guy forget about this thing this integral..lets look at that integral.
so if I graph
8:00this..its a familiar thing.
does anyone know what the graph of this is? theres a negative here.
anyone know what the graph of this looks like?
yes.
so this is a bell curve.
it looks like that.
and it comes up all the time.
and so this integral uhh integral of e^-x^2 dx maybe with a 2pi and maybe with a 1/2 in there and stuff like that
8:31shows up all the time, especially in statistics.
We want to know how much area there is between here and here -- this tells us something about the probably of a normally distributed event occuring.
tells us the expected height or the expected grade on a big exam or something like that.
all of these things so this is something important that we need to be able to do but we have no formula for it
9:01except calling it oof or something like that.
calculate the dots.
so how does that work?
the way that works is remember that the integral of a function is in fact the limit as the number of things goes to infinity uhh adding up
9:35let me just call it delta x so this stuff that we did at the start of the class but this is add add up lots of skinny rectangles.
and so you would write in the computer program and estimate this pretty easily.
10:01you can calculate this with whatever precision you want.
if I want to know what is the integral from 0 to 1/2 of e^-x^2 dx this is a number. I dont know what it is at the top of my head but I can calculate it to any precision I want.
or I can calculate this to 1,000 digits if you want.
and so what I want to talk about mostly today
10:32its not a hard topic but its something we need to cover is how to do this because this comes up a lot of times in applications and some words get thrown around that maybe youve heard before like simpsons rule. how many of you have heard of sentence rule?
a couple.
how many of you have not heard of simpsons rule?
most, okay.
so like simpsons rule or trapezoid method or midpoint method or things like that these are numerical methods for approximating integrals
11:02that enable you to do this.
and you should have some understanding of what they are even though you probably will never write a computer program yourself youll just ask your computer to do it and it will leave you with some numerical method if you have an idea of whats going on behind the scenes you know what works and what doesnt work and why it doesnt work.
so the general idea here is we have our function
11:31let me draw an unusual unknown function.
and I want to calculate this area so what we, what we did in the beginning is we just take a bunch of rectangles and here im doing them on the left side so I could just take a bunch of rectangles I have too many here already and in this case I chose the rectangles
12:02so that they all sit on what I just called the left side I guess it is the left side.
so if we just add up the area of these rectangles this gives us the left approximation
12:31and this is with 1 2 3 4 5 6 rectangles and thats..you can figure out I think pretty easily what that formula is.
another thing I could do is I could make the right approximation draw the same function. which would be slightly different.
13:15so heres the right approximation and again its pretty straightforward to figure out the formula here there are 6 6 guys and im going
13:31from lets say 0 to 3 then the first one since im going from 0 to 3 then if I have 6 each one is going to be 1/2y so this would be 1/2(1) 1 and 1/2..2 2 and 1/2..3 and so on. ill do an explicit example in a second.
so those are 2 pretty obvious things that you can do much harder is trying the lower approximation
14:01so we wont do this but we should know it exists.
the lower approximation is going to fit my rectangles underneath so sometimes ill use the right sometimes ill use the left sometimes ill use the middle and then theres an upper approximation where I fit my rectangles over.
im not going to talk about these
14:31these are hard because you have to do hard work to find the points.
and usually you dont know the function well enough to find the points anyway so when would the lower approximation be the same as lets say the left approximate?
when the slopes always positive
15:05okay so if the function is increasing then the lower will be the same as the left and if the function is decreasing the lower will be the same as the right.
and if the function has a max or a min then the lower will be mixed.
and the same about the other one, I mean reverse it okay so thats pretty straightforward.
and rather than writing down the formula for this
15:31cause we already did these left approximations and right approximations in the first week of classes.
lets talk about some slightly better ones. so so the problem with using this method is I might have to use a lot of points.
alright if im using end points by 1 I want to get good accuracy
16:01I might have to use a whole lot of points.
so there are some ways we can improve the accuracy umm.
and so what is 1 can anyone sugget what might be a better way to get the better typically better accuracy without using more points?
okay I dont know whether youre just like..tired
16:34hes the only guy whose awake. yeah?
so what does that mean?
1 suggestion is trapezoid which means if I have this function and I have my 6 rectangles
17:02then instead of using rectangles let the slopes on the tops just connect the dots.
so here I just connect the dots between 2 points.
now how can I write a formula..how can i..so what is the
17:33what is the area of a trapezoid?
yeah?
well I guess if youre laying on the side, okay.
the way I drew it here, the base is the same so its really the average of the 2 heights times the base.
so the area
18:02of something that looks like this if this is my..well lets just call this delta x for my base actually lets call this x1 and x2 these 2 points.
so in this case to get the area of this thing then the base is just x2-x1
18:30and the height..this height is f(x1) and this height is f(x2) and 1 way you can remember this is its the same as the area of the rectangle if I cut this part off and trump it over there.
then I get a rectangle. cut the top off and fold it over.
then I get a rectangle of exactly the average height.
19:01if its not in the middle necessarily this is very steep so..so the area in this case is going to be the average of the heights times the base.
19:33now suppose ive already calculated the left and the right approximations what does that tell me of the trapezoid approximations?
its the average of the two.
right? because the left guys are all of the the heights on this side.
and the right guys are all the heights on that side.
and I want to average the heights.
20:03so these 2 things...this guy is just the average of those 2 things.
but lets write a formula for the trapezoid.
so suppose im going to use n rectangles. so I want to integrate from a to b
20:30f(x) dx.
now I dont recommend that you memorize these formulas because youll forget them if you memorize them.
just draw a picture and look at the picture and figure it out it only takes a minute if you know whats going on.
so what we have to do here well the base is in everything right? so were just going to multiply by the base so at every stage im going to multiply by the width of my rectangle.
21:08thats the width of each one of those rectangles.
and now I just have to worry about the heights.
so lets worry about the heights.
so lets look at the whole picture rather than just 1 picture how many right sides am I going to-or left sides am I going to get?
21:30well I need 1 for this rectangle 1 for this rectangle 1 for this 1, 1 for this 1, 1 for this 1 until you get to the last one.
so im going to get n yeah im going to get n left sides and n right sides so im going to get f(x0), this is the left side
22:01of number 1.
and then im going to get f(x1) this is the left of number 2.
and then im going to get f(x2), f(x3) and then im going to stop at 1 before the last.
this is the left side of number n.
the last rectangle.
this is a big parentheses
22:32and now I need to add in the right sides.
well the first one here I dont use this but once I only call this the right/left side once.
the second one, then I get the right side here and a right side here and a right side here and a right side here and I right side here and a right side there.
so I get n right sides but I dont get this one but one time
23:01so this is a right side and then f(x2), f(x3) and this left side of the last guy which is also the right side of the next to last guy but then I get 1 more.
and I have to average
23:31you want me to write that a little cleaner notice that here I get 2, here I get 2, everybody I get 2 except the first and the last.
so with n rectangles this is going to be 1/2 (b-a)/n and im going to use well, let me not use that so then the first guy plus twice the second guy
24:02plus twice the 3rd guy plus twice the next guy blah blah blah blah plus twice the ultimate guy plus the last guy.
right? so thats a pretty easy formula.
its just so if im using n rectangles
24:30that means im going to have n+1 points so if im using 5 rectangles ill havce 6 points cause I have edges.
if you chop something in half you actually have 3 ends.
left side right side and I count everything double except for the 2 n's.
and then I average that so I need 1/2(b-a/n) so this is a pretty straightforward formula its pretty easy to do.
should I do an example with an actual function?
25:07here?
f(xn).
its just the function f of the very last one.
so let me do just to make it easy ill do it with a very simple function lets just do x^2 because then I can do the numbers in my head.
25:30so lets.. I mean this is a stupid example because we can figure out this exactly so why do it approximately?
the reason were doing it approximately is because everything is easy.
we would never do this for real.
but lets do it for real anyway.
so lets integrate from..lets start at 1 rather than 0 uhh let me use 4 points
26:00I need to have 4 intervals so lets integrate from 1 to 2 x^2 dx with 4 rectangles.
k so this is a really stupid example that means im going to have 5 points I need to check at.
maybe I should go from 1 to 3. lets go from 1 to 3.
but I only have half.
okay and im going to use the trapezoid method
26:30so this would be t4 is the usual notation and this is going to be 1/2 the width of the whole interval 3-1 is 2.
but im doing 4 rectangles so its another half.
3-1/4..thats half.
and then the points that im doing it on uhh let me draw the picture I guess
27:05im starting here at 1 and im doing 4 rectangles..im going out to 3.
so 2 is in the middle and 1 and 1/2 and 2 and 1/2 so those are my trapezoids that im doing.
and so I just compute
27:30f(1) so thats 1^2 + twice f(2) thats (3/2)^2 + twice f(2) is 2^2 + twice (5/2)^2 + now the last guy only gets it once and thats 3^2.
so this is
28:001/2 times 1/2 1+ uhh... thats 9/4 times 2 9/2 and this is 4 times 2 is 8.
and this is 25/4 times 2 so thats 25/2.
and thats 9.
which is some number.
28:31I dont know..17... 17, 18 plus 18+17 is 35 so this is 35/4.
I get 35/4 for this.
ya?
29:03I have 1 2 3 4 rectangles I started at 1.
so I didnt use the stuff before 1.
so I started at 1 well, I started at 1.
its the integral from 1 to 3.
wouldnt it be over 2? is that what somebody said?
wait somebody made a comment about over 2.
okay so you changed your mind now I dont have to find you.
29:30okay so this is pretty easy just a matter of thinking about what youre doing and writing down what youre doing.
the correct answer of course is uhhh x^3/3 evaluated from 1 to 3 is 27/3- 1/3 is 26/3 which is some number.
30:01I dont even know what that number is so dont worry about it.
its like 8 and 2/3 and thats like 8 and 3/4 so this is actually not so bad right?
the exact answer is 8 and 2/3.
so with 4 rectangles I can sort of get ballpark.
with 400 rectangles I could get really close.
uhh let me mention
30:31let me not do the example another thing we could do here instead of doing trapezoids another thing we could do here instead of doing trapezoids and we can get kind of the same amount of work take the midpoints.
31:07so again think of the same picture so actually let me just do it.
so im doing the same integral.
here for midpoints
31:30so im going to think about the picture im going from here to here from 1 to 3 and then chop it into 4 bits I have 4 midpoints the numbers are a little loopier because I have these same intervals 1 1 and 1/2 2 2 and 1/2
32:01and the points that I want are the middle of those points.
so I can also..im not gonna do it all out so again it will be 2/4 thats the width of the rectangle cause each one was 1/2.
goes from 1 to 3..i have 4 of them.
times...and now I want to do the function evaluated here at the first middle
32:31so the first middle is halfway between 1 and 3/2 which is 1.25 or 1 and 1/4 and then the next one will be at halfway between 1 and 1/4 inbetween 1 and 1/2 and 2 which is 1 and 3/4.
and then the next one will be halfway between 2 and 2 and 1/2 thatll be a 2.25 and then the last one will be
33:03between 2 and 1/2 and 3 so thats 2.75.
so I square up all of those numbers then I divide by a half and that gives me my midpoint approximation.
also pretty easy.
now with a lot of work that we will not do in this class you can actually get a bound on how far off these are
33:31even if you dont know the right answer.
if you have no way of knowing how good your estimate is then this is kind of useless.
But if I could tell you so if i, if you ask me how much money do you have?
100 million dollars!
thats my estimate. well I dont know, maybe im only off by $999,999,000 that tells you like nothing.
34:02maybe I only have a dollar.
thats an estimate but its a useless one because the error is so big.
if we have no way of knowing what the error is then the estimate is useless.
so we can not in this class but in a numerical analysis class or if you really want to work hard you can figure out what the error is.
34:42so whats this going to depend on?
anybody have an idea of what what this is gonna I mean what will contribute
35:02to the error?
how many rectangles, thats certainly a critical factor.
and then another factor is sort of how wiggly your function is.
the..the derivative.
so so here
35:33so if the second derivative is less than an absolute value some other thing between the start and the end so if you know the second derivative, the biggest the second derivative is
36:02the second derivative tells us how much this function bends.
the second derivative is just some number k so the second derivative is no bigger than that absolute value.
then the trapezoid approximation minus the actual value is going to be less than
36:58so the error depends on the size that you chop it up into
37:03so it depends on the interval.
and it goes like the square of the number of of intervals I take.
that means that if in this case so here I was off by about 1/4 or less than 1/4.
if I went with with uh
37:30if I doubed the number of rectangles then I should increase my answer, my answer should get better by a factor of 4.
cause im going from 4^2 to 8^2.
so if I increase my thing by if I double it the number of rectangles, my answer gets better by a factor of 3.
the midpoint is kind of the same.
38:08if the second derivative is bound by k on the interval then the midpoint approximation is less than or equal to the same constant (b-a)^3
38:30over 24n^2 so the midpoint is a little better but it doesnt doesnt really buy you much.
error in the midpoint is about half of what the error in the trapezoid rule is but increasing n is way better.
yes?
k is the same thing spinoff of the second derivative second derivative is less than k
39:00on the whole interval.
k depends on the function so in this case second derivative..well x is..derivative of x is 2x the derivative of 2x is 2.
and in this case k is 2.
if im doing a sin then the derivative is second derivative of cosine which is again sin.
its never bigger than 1.
and after youre done.
39:31but if im doing a very bendy function if im doing e^x the second derivative of this is...e^x.
so depending on how bendy the function is it controls how good these approximations are.
second derivative..so so if I have a function
40:00and that makes some amount of sense because if my function goes like this on top of my rectangles the second derivative is very big on top of my rectangles so my rectangles are going to be a terrible approximation.
okay so for the remainder I want to talk about something thats even better and I want to do an example with deciding how many intervals you need.
so simpsons rule
40:31umm which im not going to have time to derive simpsons rule is way better than both of these.
because what it does is heres my function and heres my rectangle which im drawing very big.
41:04instead of just looking at the middle or at the 2 ends it uses all 3 of those and it puts a parabola here.
41:35so what simpsons rule does is instead of using the straight line like a trapezoid or sort of an average rectangle like the midpoint rule is it puts a parabola into the 3 points that go through the 2 ends and the middle.
so that means that I mean you almost cant even see the difference in most caes
42:00its very hard for me to draw a function unless its a poor wiggly thing that doesnt look a lot like a parabola on the small piece.
right it isnt a parabola the vertex doesnt have to be in the middle. the vertex in this case is over here.
its whatever parabola goes through those 3 points so just like you know that 2 points determine a line 3 points determine a parabola.
and I dont have time to go through all of the algebra and youll all fall asleep anyway.
42:30it is in the book if you want to read it.
but it turns out that this is just a kind of a special average between the trapezoid and the midpoint.
but the pattern is 1, 4, 2, 4, 2, 4, 2,...,4, 1 so what does that mean? so if we look at this then now we think about I'm going to put some more rectangles here with parabolas on them.
43:06so every time..notice that edges..well this one gets counted once cause he counts only for the beginning rectangle and this guy gets counted twice cause he counts for this side and for this side.
and this guy gets counted twice and so on and the last guy only gets counted once
43:35and then it turns out by doing the calculation that the middles of these get counted 4 times.
so so the middles get counted 4 times and then we have to average. I think we divide by 3.
I forget whether its 3 or 6.
44:04soo if this width is my base I divide by 6 so let me write out the formula.
44:31so that means its just about its just about so here im going to use so okay theres a little issue of terminology in this picture should I say that n is 1, 2, 3, 4, 5, 6...6?
or should I say that n is 3?
all depends on what picture youre thinking of.
45:02here im thinking of 3 rectangles.
3 rectangles I have 7 point I have to write.
the book calls this 6.
so, whatever.
so so lets call this n lets call it 2n lets call it n
45:33lets just call it n we'll call it n which means that im using half n intervals.
so theres a little issue of what you want to call n how many parabolas youre fitting or how many points youre using.
the way the book uses it is they call it n is the number of points and I tend to just call it the number of intervals.
46:03so lets, lets do the n from the example in the book.
theres a homework problem that you dont have yet.
okay so if n if n+1 points so that means that this little width here is (b-a)/n.
46:34thats the half width.
and instead of doing an average with a half kind of because I have 3 points I do an average with 1/3.
but then the averaging I do is kind of funny.
take the first point once.
then I take 4 times the second point.
47:06then I take twice of the third of the third point.
and then I take 4 times of the next point and then I take twice the next point and so on and I do this until im almost done.
47:36and then in the end I take 4 of the last midpoint and just 1 very last point.
48:02so this is kind of a funny average its a weighted sum.
the middle counts more than the ends.
the middle counts double the edges so the pattern is 1 4, 2 4, 2 4, 2 4, 2 4, 1 and if you do the whole calculation which im not going to do you can see that this actually finds you the very best parabola.
48:33the 2 comes because im counting this point twice.
thats 3 points that im averaging so instead of being a half its a third but its a funny average because im weighting it.
I better have gotten it right. yes.
49:01so let me do x^2 again I dont know so n has to be even ill do n=4.
well n=4 isnt enough to do the pattern.
well maybe it is.
so let me do x^2 with n=4 it doesnt matter.
49:32so im going to do this again the same 1 to 3 you get of n=4 by simpson's rule so this would be 1/3 times the width (b-a)/2 times and then here I want to do 1^2 + 4 times
50:074 times (3/2)^2 + 2(2^2) +4 times (5/2)^2 + 3^2.
50:30so thats what simpsons-what I really want is simpsons rule. again notice the pattern 1 4, 2 4, 1.
now the error here, which is the whole use of simpsons method I mean simpsons rule the error here is instead of saying lets let m so I need another number. so I need and im pretty sure its the fourth derivative
51:01yeah the 4th derivative.
so theres some number the the 4th derivative is less than m then the error of simpsons method
51:32is going to be less than is going to be less than m/n
52:09the important thing about simpsons rule is that the error goes down like n^4.
so doing a little more work gives you a huge increase.
dont memorize this formula you can memorize the patterns
52:31I will not ask you to memorize the error formula on the exam.
may ask you to use them, wont ask you to memorize them.
so I will do one more example of this at the start of next class.