Friday, April 1, 2011

Approximation with rectangles

Usually when you approximate a function you have to use an increasing number of rectangles to infinity to get arbitrarily closer to the area of f(x). But what I noticed was the pattern of how you get closer as you keep doubling the amount of rectangles.

So for the functions I've tried over the intervals I've tried, this formula in blue lets you approximate the integral over an interval using just 5 carefully chosen points and in just a few additions, subtractions and multiplications to get at what before took an infinite number of them. And it gives an exact answer whereas you could only keep getting closer before. 

Using this formula A(1) +(4/3)[A(2)-A(1)]
to evaluate integral(e^x, x=0..1) gives 1.717776531 instead of the true value of 1.718281828 so it's off a little for exponentials . Seems to work for polynomials though. 

It turns out that when it works you only need 3 points' y values to find the integral, over your interval [a,b], you need to know f((b-a)/2), and f of both halfway points between that and the endpoints. Let's say our interval was [2,7] and we measured those 3 points to be f(4.5)=-50.625, f(3.25)=-13.203125, 
f(5.75)=-123.984375 after applying the blue formula:
we get: -4475/12

I hid from the formula but I know that these 3 points correspond to -x^3+2*x^2 the integral over that interval of which is:

Using the formula for x^4 +x^2 was off by 7.46, I get 658.666 instead of 666.1333
Still pretty close but I guess it's only exact up to cubics.
*Note That gives me the idea that if you know n points of a given curve, you can find the nth degree polynomial that runs through those (there's a formula for that) then integrate that over your boundaries, this should still be much faster than using an infinite number of rectangles.  

No comments:

Post a Comment