1 / 24

Integration Methods in Computers Engineering Lecture

Learn about integration methods such as the rectangular midpoint rule, trapezoidal rule, Simpson's rule, and Monte Carlo in this lecture on Computers in Engineering.

carlz
Download Presentation

Integration Methods in Computers Engineering Lecture

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 21 Integration Comp 208 Computers in Engineering Yi Lin Winter, 2007 Comp208 Computers in Engineering

  2. Lecture 22 Learning goals • Integration • Rectangular midpoint rule • Trapezoidal rule • Simpson’s rule • Monte Carlo Comp208 Computers in Engineering

  3. Integration • Recall the definition of the integral • As with differentiation, let’s experiment with small but finite deltas Comp208 Computers in Engineering

  4. Meet our function for today Comp208 Computers in Engineering

  5. ‘Dumb’ integrals, Left-point rule • Again, applying the definition directly • Obtain the left-point rectangular rule Comp208 Computers in Engineering

  6. Left-point rectangular rule • Note how this scheme will always underestimate an increasing function and overestimate a decreasing function • This maps well to the definition but we can very easily make it much better so we won’t even bother to write code for this. Comp208 Computers in Engineering

  7. Right-point rule • Using the right point as height, it will overestimate the ascending functions but underestimate the descending functions Comp208 Computers in Engineering

  8. Midpoint rule • Each rectangular band which makes up the integral is evaluated at the midpoint • Hopefully the overestimation and underestimation effects cancel out on each panel Comp208 Computers in Engineering

  9. Midpoint algorithm double midpoint_int( DfD foo , double a , double b , int panels ) { int i; double sum = 0; double dx = ( b - a ) / panels; double x = a + dx / 2.0; for( i = 0; i < panels; ++i ) { sum += foo( x ); x += dx; } return sum * dx; } Comp208 Computers in Engineering

  10. How good is the midpoint rule? • Absolutely accurate for constant and linear functions • How accurate for parabolas and other non-linear functions? • This is a pretty naïve way of doing things. Comp208 Computers in Engineering

  11. Trying other geometries • The rectangle isn’t the only quadrilateral that we can use to evaluate an integral • What about a trapeze? • We could try even more sophisticated shapes • Fit a parabola through 3 points and integrate that • How can we make integration rules that guarantee higher accuracy? Comp208 Computers in Engineering

  12. Trapezoidal rule Approximate curves as sequences of straight lines instead of sequences of constants Comp208 Computers in Engineering

  13. Trapezoidal integral • The integral formula comes from the Taylor series • The error term has a known order of magnitude which comes from the terms we truncate from the series to make the integration rule Comp208 Computers in Engineering

  14. Summing trapezes • Area of a trapeze • Remember that all the trapezes share an edge Comp208 Computers in Engineering

  15. Optimizing the sum • Because we are adding up all the inner edges twice but dividing all of our sums by two we can reduce the effort • Start sum with the outer edges divided by two • Then add all the inner edges undivided Comp208 Computers in Engineering

  16. Trapezoidal integration double trapezoidal_int( DfD foo , double a , double b , int panels ) { int i; double sum = ( foo( a ) + foo( b ) ) / 2.0; double dx = ( b - a ) / panels; double x = a + dx; for( i = 0; i < ( panels - 1); ++i ) { sum += foo( x ); x += dx; } return sum * dx; } Comp208 Computers in Engineering

  17. How good is this? • If deltas are small enough then the straight line approximation can be good • How does it deal with errors? Is that a good way? • How does this method’s accuracy relate to how many points it uses (relate polynomials) Comp208 Computers in Engineering

  18. Guarantees? • Midpoint and trapezoidal rules, amazingly enough, offer the same guarantees • 100% accurate on constants and linear functions • BUT, trapezoidal deals with errors better, on average. Comp208 Computers in Engineering

  19. Using a parabola • Simpson’s rule fits a parabola through 3 points and integrates it • Definition of what a panel is, is now somewhat up in the air… our old panels had 2 points but now we need three • We can subdivide our current panels… • …or we can use neighbour panels together. Comp208 Computers in Engineering

  20. Using a parabola • As the trapezoidal rule for integration finds the area under the line connecting the endpoints of a panel, Simpson's rule finds the area under the parabola which passes through 3 points (the endpoints and the midpoint) on a curve. • Area under parabola: Δx Δx Comp208 Computers in Engineering

  21. Simpson’s formula • Again, the formula comes from the taylor series: Comp208 Computers in Engineering

  22. Simpson’s algorithm double simpsons_int( DfD foo , double a , double b , int panels ) { int i; double sum = 0; double dx = ( b - a ) / panels; double x = a; for( i = 0; i < panels; ++i ) { sum += foo( x ) + 4*foo( x + dx/2.0 ) + foo( x + dx ); x += dx; } return sum * dx / 6.0; } Comp208 Computers in Engineering

  23. How good is this? • Look at the error term… ∆x5!! • If ∆x is 0.01, then the precision is already beyond most engineering requirements… • Even more methods exist however… • Even methods to make methods of arbitrary precision Comp208 Computers in Engineering

  24. Monte Carlo Integration double monte_carlo_int(DfD f, double x0, double x1, int n) { double sum = 0; int i; srand(time(NULL)); // Sum n random values of f(x) with x in [x0, x1]. for(i = 0; i < n; ++i) sum += f( (rand() / RAND_MAX) * (x1 - x0) + x0 ); // Divide the sum by n to get the average value of f(x) with x in [x0, x1]. // Multiply by (x1 - x0) to get the area. return (sum / n) * (x1 - x0); } Comp208 Computers in Engineering

More Related