Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00 1
Lecture 25 � Attention: The last homework HW5 and the last project are due on Tuesday November 24!! � Last time: � Cubic splines � Today � Searching and sorting � Numerical integration (chapter 17) � Next Time � Numerical integration of functions (chapter 18). Lecture 25 2
Search algorithms � Find an element of a set based upon some search criteria. � Linear search: � Compare each element of the set with the “target” � Requires O(n) operations if the set of n elements is not sorted � Binary search: � Can be done only when the list is sorted. � Requires O(log(n)) comparisons. � Algorithm: � Check the middle element. � If the middle element is equal to the sought value, then the position has been found; � Otherwise, the upper half or lower half is chosen for search based on whether the element is greater than or less than the middle element. 3
Sorting algorithms � Algorithms that puts elements of a list in a certain order, e.g., numerical order and lexicographical order. � Input: a list of n unsorted elements. � Output: the list sorted in increasing order. � Bubble sort � complexity: average O(n 2 ); )); worst case O(n 2 ). � Compare each pair of elements; swap them if they are in the wrong order. � Go again through the list until no swaps are necessary. � Quick sort � complexity: average O(n log(n)); worst case O(n 2 ). � Pick an element, called a pivot , from the list. � Reorder the list so that � all elements which are less than the pivot come before the pivot and � all elements greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. � Recursively sort the sub-list of lesser elements and the sub-list of greater elements. 4
Sorting algorithms (cont’d) Merge sort – invented by John von Neumann: � Complexity: average O(n log(n)); worst case O(n log(n)); 1. If the list is of length 0 or 1, then it is already sorted. Otherwise: 2. Divide the unsorted list into two sublists of about half the size. 3. Sort each sublist recursively by re-applying merge sort. 4. Merge the two sublists back into one sorted list. 5. Tournament sort: � Complexity: average O(n log(n)); worst case O(n log(n)); � It imitates conducting a tournament in which two players play with each other. � Compare numbers in pairs, then form a temporary array with the winning � elements. � Repeat this process until you get the greatest or smallest element based on your choice. 5
Integration Integration: � ∫ b ( ) I = f x dx a is the total value, or summation, of f ( x ) dx over the range from a to b : 6
Newton-Cotes formulas Replace a function difficult or � impossible to integrate analytically or tabulated data with a polynomial easy to integrate: ∫ ∫ b b ( ) ( ) I = dx ≅ f x f n x dx a a f n ( x ) is an n th order interpolating polynomial. The integrating function can be � polynomials for any order - for example, (a) straight lines or (b) parabolas. The integral can be approximated in � one step or in a series of steps to improve accuracy . 7
Trapezoidal rule The trapezoidal rule is the first � of the Newton-Cotes closed integration formulas; it uses a straight-line approximation for the function: ∫ b ( ) I = f n x dx a ⎡ ( ) − f a ( ) ⎤ f ( a ) + f b ∫ b ( ) I = x − a dx ⎢ ⎥ b − a ⎣ ⎦ a ( ) + f b ( ) ) f a ( I = b − a 2 8
Error of the trapezoidal rule An estimate for the local truncation � error of a single application of the trapezoidal rule is: E t = − 1 ( ) b − a ( ) 3 ′ ′ f ξ 12 where ξ is somewhere between a and b . � This formula indicates that the error is dependent upon the curvature of the actual function as well as the distance between the points. � Error can thus be reduced by breaking the curve into parts. 9
Composite trapezoidal rule Assuming n +1 data points are evenly spaced, � there will be n intervals over which to integrate. The total integral can be calculated by � integrating each subinterval and then adding them together: ∫ ∫ ∫ ∫ x n x x 2 x n ( ) ( ) ( ) ( ) I = dx = dx + dx + L + 1 f n x f n x f n x f n x dx x 0 x 0 x 1 x n − 1 ( ) + f x ( ) ( ) + f x 2 ( ) ( ) + f x n ( ) ) f x 0 ) f x ) f x n − 1 ( ( ( I = x 1 − x 0 + x 2 − x + L + x n − x n − 1 1 1 1 2 2 2 ⎡ ⎤ n − 1 I = h ∑ ( ) + 2 ( ) ( ) + f x n 2 f x 0 f x i ⎢ ⎥ ⎣ ⎦ i = 1 10
11
Simpson’s rules � The error of the trapezoidal rule is related to the second derivative of the function. � To improve the accuracy use (a) 2nd and (b) 3rd order polynomials; the results are called Simpson’s rules . 12
Simpson’s 1/3 rule � Simpson’s 1/3 rule corresponds to using second-order polynomials. Using the Lagrange form for a quadratic fit of three points: ( ) ( ) ( ) ( ) ( ) ( ) x − x 1 x − x 2 ( ) + x − x 0 x − x 2 ( ) + x − x 0 x − x 1 ( ) = ( ) f n x f x 0 f x 1 f x 2 ( ) ( ) ( ) ( ) ( ) ( ) x 0 − x 1 x 0 − x 2 x 1 − x 0 x 1 − x 2 x 2 − x 0 x 2 − x 1 � Integration over the three points simplifies to: ∫ x 2 ( ) I = f n x dx x 0 I = h [ ] ( ) + 4 f x 1 ( ) + f x 2 ( ) 3 f x 0 13
Error of Simpson’s 1/3 rule An estimate for the local truncation error of a single application of Simpson’s � 1 ( ) ξ 1/3 rule is: ( ) b − a ( ) 5 4 E t = − 2880 f where again ξ is somewhere between a and b . This formula indicates that the error is dependent upon the fourth-derivative � of the actual function as well as the distance between the points. Note that the error is dependent on the fifth power of the step size (rather � than the third for the trapezoidal rule). � Error can thus be reduced by breaking the curve into parts. 14
Composite Simpson’s 1/3 rule Simpson’s 1/3 rule can be used on � a set of subintervals in much the same way the trapezoidal rule was, except there must be an odd number of points. Because of the heavy weighting of � the internal points, the formula is a little more complicated than for the trapezoidal rule: ∫ ∫ ∫ ∫ x n x 2 x 4 x n ( ) ( ) ( ) ( ) I = dx = dx + dx + L + f n x f n x f n x f n x dx x 0 x 0 x 2 x n − 2 I = h ] + h ] + L + h [ [ [ ] ( ) + 4 f x 1 ( ) + f x 2 ( ) ( ) + 4 f x 3 ( ) + f x 4 ( ) ( ) + 4 f x n − 1 ( ) + f x n ( ) 3 f x 0 3 f x 2 3 f x n − 2 ⎡ ⎤ n − 1 n − 2 ⎢ ⎥ I = h ∑ ∑ ( ) + 4 ( ) ( ) ( ) + 2 + f x n 3 f x 0 f x i f x i ⎢ ⎥ ⎢ ⎥ i = 1 j = 2 ⎣ ⎦ i , odd j , even 15
Simpson’s 3/8 rule Simpson’s 3/8 rule corresponds to using � third-order polynomials to fit four points. Integration over the four points simplifies to: ∫ x 3 ( ) I = f n x dx x 0 I = 3 h [ ] ( ) + 3 f x 1 ( ) + 3 f x 2 ( ) + f x 3 ( ) f x 0 8 � Simpson’s 3/8 rule is generally used in concert with Simpson’s 1/3 rule when the number of segments is odd. 16
Higher-order formulas � Higher-order Newton-Cotes formulas may also be used - in general, the higher the order of the polynomial used, the higher the derivative of the function in the error estimate and the higher the power of the step size. � As in Simpson’s 1/3 and 3/8 rule, the even-segment-odd-point formulas have truncation errors that are the same order as formulas adding one more point. For this reason, the even-segment-odd- point formulas are usually the methods of preference. 17
Integration with unequal segments � The trapezoidal rule with data containing unequal segments: ∫ ∫ ∫ ∫ x n x 1 x 2 x n ( ) ( ) ( ) ( ) I = dx = dx + dx + L + f n x f n x f n x f n x dx x 0 x 0 x 1 x n − 1 ( ) + f x 1 ( ) ( ) + f x 2 ( ) ( ) + f x n ( ) ) f x 0 ) f x 1 ) f x n − 1 ( ( ( I = x 1 − x 0 + x 2 − x 1 + L + x n − x n − 1 2 2 2 18
19 Integration with unequal segments
Built-in functions � MATLAB has built-in functions to evaluate integrals based on the trapezoidal rule z = trapz(y) z = trapz(x, y) produces the integral of y with respect to x. If x is omitted, the program assumes h =1. z = cumtrapz(y) z = cumtrapz(x, y) produces the cumulative integral of y with respect to x. If x is omitted, the program assumes h =1. 20
Multiple Integrals Multiple integrals can be determined � numerically by first integrating in one dimension, then a second, and so on for all dimensions of the problem. 21
Recommend
More recommend