290 likes | 630 Views
Numerical Methods. Lecture 3 - Numerical Integration Dr Andy Phillips School of Electrical and Electronic Engineering University of Nottingham. Today’s Topics. Numerical Integration Basic theory Left Hand rule Right Hand rule Midpoint rule Trapezoidal Rule Simpson’s rule. Motivation.
E N D
Numerical Methods Lecture 3 - Numerical Integration Dr Andy Phillips School of Electrical and Electronic Engineering University of Nottingham
Today’s Topics • Numerical Integration • Basic theory • Left Hand rule • Right Hand rule • Midpoint rule • Trapezoidal Rule • Simpson’s rule
Motivation • While mathematically integration is “finding the area under a curve” there are many, many applications of integration within engineering where we are actually finding a physical quantity that interests us • Integrate… • …current over time to get charge • …power over time to get energy • …velocity over time to get distance • etc.
Numerical Integration f(x) I x x1 x2 x2 For a definite integral, represented by area I: I = f(x).dx x1
Why numerical integration? • For DEFINITE integrals • Simple integrals we can solve by hand • For more complex ones (e.g. below) we can use computer systems like Maple or Mathcad to get an analytical solution
But… • Often integrals • Are too difficult to solve analytically (by hand) • May not even have a solution that can be expressed symbolically • As a result we resort to numerical methods
Methods Available • The are a number of methods available • Left Hand Rule • Right Hand Rule • Midpoint • Trapezoidal Rule • Simpson’s Rule • But fundamentally they are all the same • (especially the 1st four)
How they work • The idea is to • break the integral down into a number of individual areas • sum these areas to get the total integral • The accuracy of the final result depends upon • The ‘width’ of the blocks • The ‘fit’ of the block shape we use
Left hand/right hand/midpoint methods • We approximate the curve to a series of rectangles • The name of the method indicates what we consider the height of the block to be • The next slide show this graphically
Comparing the methods Left Right Middle The only difference is what we consider the height of the block to be
Proof (by inspection) y(x1) x1 y(xmp) A = y(x).dx y(x0) x0 A (A is the exact area of the strip) xmp=(x0+x1)/2 xo xmp x1 • But if (x1-x0) is small, A = width * height = (x1-x0)H (approx.) • where H = y(x0) (lh) or y(x1) (rh) or y(xmp) (mp) • Note: as width (x1-x0) tends to zero all 3 methods converge
The final result is the sum of the areas • We do this numerically by: • Asking the user the limits of the integral • And how many ‘blocks’ to use (hence the interval size) • We then use a loop to calculate the size of the individual areas • Which we sum for the final result
However • Whilst the methods are OK, the error is often large • E.g. Left Hand Rule: Error Rectangle
A better solution is a trapezoid • A better fit to a curve Error (smaller!) b a A =(a+b)h 2 Trapezoid (approx.) A h width = h (sometimes called x)
Hence the trapezoidal rule A1 A2 xo x2 x1 x1 x2 A = y(x).dx + y(x).dx = A1 + A2 (approx.) x0 x1 (A1 and A2 are trapezoid areas)
Continuing… x1 x2 A = y(x).dx + y(x).dx = A1 + A2 (approx.) x0 x1 =h[y(x1) + y(xo) ]/2 + h[y(x2) + y(x1)]/2 =h [y(x1) + y(xo) + y(x2) + y(x1)] 2 =h [y(x0) + 2y(x1) + y(x2) ] 2
Over the complete range n-1 A =h [y(x0) + y(xn) ] + h y(xi) 2 i=1 Note: this formula only works if ‘h’ is the same size over the whole range
To do this computationally • We could use the formula from previous slide: • (1) Define the function y(x) • (2) ask for limits & blocks -> ‘h’ • (3) Calculate first and last heights and halve them, storing result in ‘sum’ • (4) Loop i = 1 to (n-1), adding heights to ‘sum’ • (5) Multiply ‘sum’ by ‘h’ to get answer
Alternatively… • We could simply calculate the area of a strip and add it to a running total: • (1) Define the function y(x) • (2) ask for limits & blocks -> ‘h’ • (3) initialise ‘sum’ to be 0 • (4) Loop i = 1 to n calculating area of ith strip as (y(xi)-y(xi-1))h/2 and add to ‘sum’
Simpson’s rule • This is a more advanced method where we use two strips and approximate the curve using a quadratic Curve f(x) q(x) = ax2 + bx + c Note: i) we MUST have an even total number of strips ii) q(x) is different for each pair of strips
Lab work • On Wednesday… • You are required to numerically determine • LH, RH & Midpoint methods • The Trapezoidal rule • Simpson’s rule (if time permits) • The actual value is 0.4134109052 • from solving the integral by hand using: