* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download X - EGR 115!
Survey
Document related concepts
Plateau principle wikipedia , lookup
Mathematical optimization wikipedia , lookup
Time value of money wikipedia , lookup
Computational chemistry wikipedia , lookup
Strähle construction wikipedia , lookup
Numerical continuation wikipedia , lookup
Receiver operating characteristic wikipedia , lookup
Determination of the day of the week wikipedia , lookup
Horner's method wikipedia , lookup
Computational fluid dynamics wikipedia , lookup
Computational electromagnetics wikipedia , lookup
Root-finding algorithm wikipedia , lookup
Transcript
Numerical Methods Applications of Loops: The power of MATLAB Mathematics + Coding 1 Numerical Methods Numerical methods are techniques used to find quantitative solutions to mathematical problems. Many numerical methods are iterative solutions – this means that the technique is applied over and over, gradually getting closer (i.e. converging) on the final answer. Iterative solutions are complete when the answer from the current iteration is not significantly different from the answer of the previous iteration. “Significantly different” is determined by the program or user – for example, it might mean that there is less than 1% change or it might mean less than 0.00001% change. 2 Ex 1. Newton’s Method Newton’s Method is used to find the roots of an equation. “When the curve crosses the x-axis.” “When f(x)=0.” Prepare a MATLAB program that uses Newton’s Method to find the roots of the equation: y(x) = 3 x + 2 2x – 300/x 3 Newton’s Method f(x) = x3 + 2x2 – 300/x What does it seem the answer is? 4 Newton’s Method Closer look… Actual Root! 5 Newton’s Method X is the root we’re trying to find – the value where the function becomes 0 Xn is the initial guess Xn+1 is the next approximation using the tangent line Credit to: http://en.wikipedia.org/wiki/Newton’s_method 6 The Big Picture (1/4) 1st: guess! xn 7 The Big Picture (1/4) 2nd: Go hit the actual curve At this (xn , yn), calculate the slope of the curve using the derivatives: m = f’(xn) 8 The Big Picture (1/4) 3rd: Using the slope m, the coordinates (Xn , Yn), calculate Xn+1 : 9 The Big Picture (2/4) TOO FAR Guess again! 10 The Big Picture (2/4) Iterate! 11 The Big Picture (2/4) TOO FAR Guess again! 12 The Big Picture (3/4) – X interecept Eventually… CLOSE ENOUGH! guess Stop the loop, when there is not much difference between the guess and the new value! 13 Ex 1: algorithm, skeleton % Provide an initial guess at the answer % Find the slope of the tangent line at the point (requires derivative) % Using the line’s equation, find the x intercept % Repeat above until close enough, using the x intercept as the new “guess” 14 Newton’s Method % Define initial difference as very large (force loop to start) % Repeat as long as change in x is large enough % Make the “new x” act as the current x % Find slope of tangent line using f’(x) % Compute y-value % Use tangent slope to find the next value: % two points on line are the current point (x, y) % and the x-intercept (x_new, 0) 15 % Define initial difference as very large % (force loop to start) Xn+1 TOO FAR x_n = 1000; x_np1 = 5; % initial guess Xn % Repeat as long as change in x is large enough while (abs(x_np1 – x_n) > 0.0000001) % x_np1 now becomes the new guess x_n = x_np1; % Find slope of tangent line f'(x) at x_n m = 3*x_n^2 + 4*x_n + 300/(x_n^2); Xn+1 ? Xn % Compute y-value y = x_n^3 + 2*x_n^2 - 300/x_n; % Use tangent slope to find the next value: % two points on line: the current point (x_n, y_n) % and the x-intercept (x_np1, 0) x_np1 = ((0 - y) / m) + x_n; end 16 Newton’s Method Add fprintf’s and see the results: x ------------------5.000000000000000 3.925233644859813 3.742613907949879 3.739045154311932 3.739043935883257 y ------------------115.000000000000000 14.864224007996924 0.279824373263295 0.000095471294827 0.000000000011084 Root of f(x) = x3 + 2x2 – 300/x is approximately 3.7390439 17 Real-World Numerical Methods are used in CFD, where convergence can take weeks/months to complete (get “close enough”)... while loops are generally used, since the number of iteration depends on how-close the result should be, i.e. the precision. Practice! Code this, and use a scientific calculator to see how close the results are. 18 Numerical Methods Secant Method: Find the Derivative 19 Derivatives, intro. Another numerical method involves finding the derivative at a point on a curve. Basically, this method uses the secant line as an approximation – bringing it closer and closer to the tangent line This method is used when the actual symbolic derivative is hard to find! f’(x)= 20 Derivatives, big picture What is the slope at this point? 21 Derivatives (1/5) Choose a and b to be equidistant from that point, calculate slope 1 Slope 1 X 22 Derivatives (1/5) Reduce the range for the second iteration. Slope 1 range X range/2 range/2 23 Derivatives (2/5) Slope 1 loX x hiX Zoom in on x 24 Derivatives (3/5) Slope 1 loX x hiX Find new secant line. 25 Derivatives (3/5) Slope 1 Slope 2 hiY loY loX x hiX Calculate new slope 26 Derivatives (3/5) Slope 1 Slope 2 loX x hiX Is slope 2 “significantly different” from slope 1? Assume they are different… let’s iterate!... 27 Derivatives (4/5) Slope 1 Slope 2 Slope3 loX x hiX Zoom in again.. 28 Derivatives (4/5) Slope 1 Slope 2 Slope3 loX x hiX Zoom in again.. Is Slope2 very different from Slope3? Iterate while yes! 29 (5/5) Eventually… Stop the loop when the slopes are no longer “significantly different” Remember: “significantly different” means maybe a 1% difference? maybe a 0.1% difference? maybe a 0.000001% difference? 30 Derivatives - Algorithm % Define initial difference as very large % (force loop to start) % Specify starting range % Repeat as long as slope has changed “a lot” % Cut range in half % Compute the new low x value % Compute the new high x value % Compute the new low y value % Compute the new high y value % Compute the slope of the secant line % end of loop Let’s do this for f(x) = sin(x) + 3/sqrt(x) 31 % Define initial difference as very large % (force loop to start) m = 1; oldm = 2; Code % Specify starting range and desired point range = 10; x=5; ctr=0; % Repeat as long as slope has changed a lot (or have just started) while (ctr<3 || (abs((m-oldm)/oldm) > 0.00000001)) % Cut range in half range = range / 2; % Compute the new low x value lox = x – range ; % Compute the new high x value hix = x + range ; % Compute the new low y value loy = sin(lox) + 3/sqrt(lox); % Compute the new high y value hiy = sin(hix) + 3/sqrt(hix); % Save the old slope oldm = m; % Compute the slope of the secant line m = (hiy – loy) / (hix – lox); % increment counter ctr = ctr + 1; end % end of loop 32 Derivatives - the end Add some fprintf’s and… 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: LOW x 0.000000000000000, 2.500000000000000, 3.750000000000000, 4.375000000000000, 4.687500000000000, 4.843750000000000, 4.921875000000000, 4.960937500000000, 4.980468750000000, 4.990234375000000, 4.995117187500000, 4.997558593750000, 4.998779296875000, 4.999389648437500, 4.999694824218750, 4.999847412109375, 4.999923706054688, HIGH x 10.000000000000000 7.500000000000000 6.250000000000000 5.625000000000000 5.312500000000000 5.156250000000000 5.078125000000000 5.039062500000000 5.019531250000000 5.009765625000000 5.004882812500000 5.002441406250000 5.001220703125000 5.000610351562500 5.000305175781250 5.000152587890625 5.000076293945313 Slope -Inf -0.092478729683983 0.075675505484728 0.130061340134248 0.144575140383541 0.148263340290110 0.149189163013407 0.149420855093148 0.149478792897432 0.149493278272689 0.149496899674250 0.149497805028204 0.149498031366966 0.149498087951815 0.149498102098005 0.149498105634484 0.149498106518877 Difference in slope -Inf Inf 0.168154235168711 0.054385834649520 0.014513800249293 0.003688199906569 0.000925822723297 0.000231692079740 0.000057937804284 0.000014485375257 0.000003621401561 0.000000905353954 0.000000226338761 0.000000056584850 0.000000014146190 0.000000003536479 0.000000000884393 Slope of f(x) = sin(x) + 3/sqrt(x) at x=5 is approximately 0.1494981 33 Numerical Methods Bisection Method A general purpose mathematical solution finder 34 More Examples ES206 Fluid Dynamics Solve for f e 1 2.51 D 2.0 log 3.7 R N f f The Colebrook Equation 35 ES206 – Fluid Dynamics e 2.51 D 2.0 log 3.7 R N f f 1 “WOW – that’s hard to solve!” How about this instead: “By hand, find the square root of 127.3” Both problems use the same technique to solve: the Bisection Method. 36 Bisection Method The Bisection Method is a very common technique for solving mathematical problems numerically. Most people have done this even if they didn’t know the name. The technique involves taking a guess for the desired variable, then applying that value to see how close the result is to a known value. Based upon the result of the previous step, a better “guess” is made and the test repeated. 37 Bisection Method For example: Find the square root of 127.3 First guess: 11 (since we know that 112 is 121) 11*11 = 121 too low (looking for 127.3) Adjust: 12 is the new guess 12*12 = 144 too high Adjust: 11.5 (“bisect” the difference) 11.5*11.5 = 132.25 too high Adjust: 11.25 11.25 * 11.25 = 126.5625 too low etc. 38 Bisection Method For the Colebrook Equation, we know that this equation must hold: e 1 2 . 51 2.0 log D 3.7 R N f f Guess for f and compare the left side value to the right side value. If they’re not the same, adjust f and try again. This is the same as solving for 127.3 = X2 : we guessed at X and then compared the two sides. 39 Wrapping Up Very small amount of code to solve a mathematical problem MATLAB can iterate 5 times, or 1,000 times, or a million! The more precision needed, the more iterations, the more time! Try to code it. See it you can find the slope of any equation. 40 Wrapping Up We learned about three numerical methods 1. Newton’s Method for finding the roots of an equation 2. Secant line approximation of a tangent point Requires the symbolic derivative Finds the numerical value of the derivative at a point 3. Bisection Method Finds numerical solutions to complex mathematical problems Very general – essentially “Guess, Test, Adjust” 41