Download presentation
Presentation is loading. Please wait.
Published byJoella Johnson Modified over 9 years ago
1
Numerical Methods Applications of Loops: The power of MATLAB Mathematics + Coding 1
2
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
3
Root Finding If we can find where f(x) = 0, then we can find where f(x) equals any value. To find where f(x) = -2, find the roots of f(x)+2 Essentially, we can shift any function up or down and solve for the roots, so all value finding problems can essentially be adjusted to be root finding problems. We can turn any equation into a root finding problem. 3
4
Bracketing Methods There are 2 basic “bracketing methods” (only 1 of which we’ll discuss). They both rely on the same basic concept If you know that f(x 1 ) > 0 and f(x 2 ) < 0, then a root must occur between x 1 and x 2. 4
5
More Examples ES206 Fluid Dynamics 5 Solve for f The Colebrook Equation
6
ES206 – Fluid Dynamics 6 “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.
7
Bisection Method For the Colebrook Equation, we know that this equation must hold: Re-arrange and you have a roots problem. Guess for f and if the equation is not satisfied, adjust f and try again. This is the same as solving for 127.3 = X 2. We made it X 2 -127.3 = 0 and found the roots. 7
8
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. 8
9
Bisection Method Typical human method: Find the square root of 127.3 First guess: 11 (since we know that 11 2 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. 9
10
Bisection Method Let’s use the programmer’s method – an algorithm: Define x 1 so that f(x 1 ) is greater than 0. Define x 2 so that f(x 2 ) is less than 0. Evaluate f((x 1 +x 2 )/2) If it is positive, set x 1 to (x 1 +x 2 )/2 If is it negative, set x 2 to (x 1 +x 2 )/2 Repeat until you’re happy with x 1 -x 2 10
11
Pros/Cons with Bisection Cons Requires f(x1) 0. CAN be hard to find Can require A LOT of iterations. Pros GUARANTEED to work 11
12
Newton’s Method X is the root we’re trying to find – the value where the function becomes 0 X n is the initial guess (hopefully, a reasonably-close guess!) X n+1 is the next approximation using the tangent line Credit to: http://en.wikipedia.org/wiki/Newton’s_method 12
13
The Big Picture (1/4) 1 st : guess! x n 13
14
The Big Picture (1/4) 2 nd : Go hit the actual curve At this (x n, y n ), calculate the slope of the curve using the derivatives: m = f’(x n ) 14
15
The Big Picture (1/4) 3 rd : Using the slope m, the coordinates (X n, Y n ), calculate X n+1 : 15 Remember: m was obtained from the derivative – only X n+1 is unknown. What is Y n+1 ?
16
The Big Picture (2/4) Guess again! TOO FAR 16
17
The Big Picture (2/4) Iterate! 17
18
The Big Picture (2/4) Guess again! TOO FAR 18
19
The Big Picture (3/4) – Eventually… Stop the loop, when there is not much difference between the guess and the new value! CLOSE ENOUGH! Old estimate New estimate 19
20
Newton’s Method Prepare a MATLAB program that uses Newton’s Method to find the roots of the equation: y(x) = x 3 + 2x 2 – 300/x 20
21
Newton’s Method f(x) = x 3 + 2x 2 – 300/x What does it seem the answer is? 21
22
Newton’s Method Closer look… Actual Root! 22
23
Ex 1: algorithm, skeleton % Provide an initial guess for the x-intercept % Repeat until close enough: compare most recent x-intercept to previous x-intercept % Find the slope of the tangent line at the point (requires derivative) % Using the slope formula, find the x intercept of the tangent line 23
24
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) 24
25
% Define initial difference as very large % (force loop to start) x_n = 1000; x_np1 = 5; % initial guess % 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); % 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 TOO FAR XnXn X n+1 ? XnXn 25
26
Newton’s Method Add fprintf ’s and see the results: x y------------------- 5.000000000000000115.000000000000000 3.925233644859813 14.864224007996924 3.742613907949879 0.279824373263295 3.739045154311932 0.000095471294827 3.739043935883257 0.000000000011084 Root of f(x) = x 3 + 2x 2 – 300/x is approximately 3.7390439 26
27
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. 27
28
Cons Divide by 0 Error What happens if x is a maximum or minimum – the slope of the tangent line is 0. Divergence What if our tangent line leads us away from our root. Cycling f(x)=x 3 -x-3 f’(x)=3x 2 -1 x0=0 28 xixi x i+1 0.0000-3.0000 -1.9615 -1.1472 -0.0066 -3.0004 -1.9618 -1.1474 -0.0073 -3.0005 -1.9619
29
Secant Method: Find the Derivative Numerical Methods 29
30
Derivatives, intro. Another numerical method involves finding the value of the derivative at a point on a curve. Basically, this method uses secant lines as approximations to the tangent line. This method is used when the actual symbolic derivative is hard to find, the formula is simply not needed, or the function is prone to change as the main program does its job. 30
31
Derivatives, big picture What is the slope at this point? 31
32
Derivatives (1/5) Choose a and b to be equidistant from that point, calculate slope 1 Slope 1 X 32 range range/2
33
Derivatives (2/5) Zoom in on x Slope 1 x loXhiX 33 Reduce the range for the second iteration.
34
Derivatives (3/5) Find new secant line. Slope 1 x loXhiX 34
35
Derivatives (3/5) Calculate new slope Slope 1 x loXhiX loY hiY Slope 2 35
36
Derivatives (3/5) Slope 1 x loXhiX Slope 2 Is slope 2 “significantly different” from slope 1? Assume they are different… let’s iterate!... 36
37
Derivatives (4/5) Zoom in again.. x loXhiX Slope3 Slope 2 Slope 1 37
38
Derivatives (4/5) Zoom in again.. x loXhiX Slope3 Is Slope2 very different from Slope3? Iterate while yes! Slope 2 Slope 1 38
39
(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? 39
40
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) 40
41
Code % Define initial difference as very large % (force loop to start) m = 1; oldm = 2; % Specify starting range and desired point range = 10; x=5; ctr=0; % Repeat as long as slope has changed a lot while (ctr 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 (or have just started) 41
42
Derivatives - the end Add some fprintf’s and… LOW x HIGH x Slope Difference in slope 1: 0.000000000000000, 10.000000000000000 -Inf -Inf 2: 2.500000000000000, 7.500000000000000 -0.092478729683983 Inf 3: 3.750000000000000, 6.250000000000000 0.075675505484728 0.168154235168711 4: 4.375000000000000, 5.625000000000000 0.130061340134248 0.054385834649520 5: 4.687500000000000, 5.312500000000000 0.144575140383541 0.014513800249293 6: 4.843750000000000, 5.156250000000000 0.148263340290110 0.003688199906569 7: 4.921875000000000, 5.078125000000000 0.149189163013407 0.000925822723297 8: 4.960937500000000, 5.039062500000000 0.149420855093148 0.000231692079740 9: 4.980468750000000, 5.019531250000000 0.149478792897432 0.000057937804284 10: 4.990234375000000, 5.009765625000000 0.149493278272689 0.000014485375257 11: 4.995117187500000, 5.004882812500000 0.149496899674250 0.000003621401561 12: 4.997558593750000, 5.002441406250000 0.149497805028204 0.000000905353954 13: 4.998779296875000, 5.001220703125000 0.149498031366966 0.000000226338761 14: 4.999389648437500, 5.000610351562500 0.149498087951815 0.000000056584850 15: 4.999694824218750, 5.000305175781250 0.149498102098005 0.000000014146190 16: 4.999847412109375, 5.000152587890625 0.149498105634484 0.000000003536479 17: 4.999923706054688, 5.000076293945313 0.149498106518877 0.000000000884393 Slope of f(x) = sin(x) + 3/sqrt(x) at x=5 is approximately 0.1494981 42
43
Integration 43
44
Integration Some functions can’t be integrated symbolically Other functions are just REALLY ugly to do it
45
Trapezoidal Rule Approximate the curve with an easy to integrate first order polynomial - This ends up being a trapezoid.
47
Trapezoidal Rule Trapezoidal Rule works out as:
48
Composite Trapezoidal Can we just combine multiple trapezoids? Yes - Instead of 1 large trapezoid, we’ll use multiple smaller trapezoids and add up their areas (and their errors)
51
xyArea 00 0.2910 10.5820 0.9171 21.2521 1.3334 31.4147 1.3044 41.1941 1.0210 50.8480 Sum:4.8669
52
Can We Go Smaller? You can decrease the size of your trapezoids, but they are still always going to be approximating a curved line using a linear formula.
53
With a width of 1, the value is 4.8669. Error = 0.0302 With a width of 0.5, the value is 4.8924 Error = 0.0074 0.00.0000 0.0482 0.50.1927 0.1937 1.00.5820 0.3878 1.50.9694 0.5554 2.01.2521 0.6624 2.51.3973 0.7030 3.01.4147 0.6874 3.51.3350 0.6323 4.01.1941 0.5544 4.51.0237 0.4679 5.00.8480 4.8924
54
Simpson’s 1/3 Rule At some point, we can’t cut our width anymore. We need to actually approximate the line better. Trapezoidal Method approximated each pair of consecutive points with a linear line (1st order polynomial). What if we approximate every three points with a 2nd order polynomial?
55
Error = 0.0011 0.00.0000 0.2255 0.50.1927 1.00.5820 0.9519 1.50.9694 2.01.2521 1.3760 2.51.3973 3.01.4147 1.3248 3.51.3350 4.01.1941 1.0228 4.51.0237 5.00.8480 4.9010
56
What about 3rd & 4th Order? Simpson’s 3/8th Rule - 3rd Order Requires an EVEN number of points Boole’s Rule - 4th Order
57
MethodhEstimate Absolute Error Evaluation s of f(x) Math Operations Trapezoida l 14.86690.0302610+4 Trapezoida l 0.54.89240.00741120+9 Simpson’s 1/3 14.9010.00111130+4 Simpson’s 1/3 0.54.90.00012160+9 Simpson’s 3/8 14.90040.00051690+4 Simpson’s 3/8 0.54.8999031180+9 Boole’s14.8999021130+4 Math Operations = Number of +,-,*,/ to calculate segment + Number of Additions to add up all segments
58
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. 58
59
Wrapping Up We learned about four numerical methods 1. Bisection Method Finds numerical solutions to complex mathematical problems Very general – essentially “Guess, Test, Adjust” 2. Newton’s Method for finding the roots of an equation Requires the symbolic derivative 3. Secant line approximation of a tangent point Finds the numerical value of the derivative at a point 4. Integration using Trapezoidal and Simpson’s rules 59
60
More… Numerical Methods is often a separate class at some universities. Other Common Topics are: Solving large systems of equations Polynomial Approximation Curve Fitting Optimization ODE/PDE Chapra and Canale’s “Numerical Methods for Engineers” is considered “the resource” Also: http://nm.mathforcollege.com/ 60
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.