Presentation is loading. Please wait.

Presentation is loading. Please wait.

Comp 208 Computers in Engineering Yi Lin Winter, 2007

Similar presentations


Presentation on theme: "Comp 208 Computers in Engineering Yi Lin Winter, 2007"— Presentation transcript:

1 Comp 208 Computers in Engineering Yi Lin Winter, 2007
01/06/2019 Lecture 20 Root finding Comp 208 Computers in Engineering Yi Lin Winter, 2007 6/1/201911/15/05 Comp208 Computers in Engineering

2 Comp208 Computers in Engineering
01/06/2019 01/06/2019 Lecture 19 Root finding Objectives Learn about 4 numerical methods Bisection Secant False position Newton Raphson Learn new features of C Pointer to function, pass as an argument to a function typedef Learn enough in-depth C to implement these 6/1/201911/15/05 Comp208 Computers in Engineering 2

3 Comp208 Computers in Engineering
01/06/2019 01/06/2019 Intro to Root Finding A way to solve equations in one unknown that cannot be solved symbolically. For example, suppose that we would like to solve the simple equation: x2 = 5 To solve this equation using the bisection method, we first manipulate it algebraically so that one side is zero. x2 - 5 = 0 Finding a solution to this equation is then equivalent to finding a root of the function f(x) = x2 - 5 6/1/201911/15/05 Comp208 Computers in Engineering 3

4 Comp208 Computers in Engineering
01/06/2019 Bisection (con’t) We next find two numbers, a positive guess and a negative guess, so that f(positive guess) > 0 and f(negative guess) <0. So long as the function whose root we are finding is continuous, there must be at least one root between the positive and negative guesses. The bisection method locates such a root by repeatedly narrowing the distance between the two guesses. middle = (positive guess + negative guess)/2 6/1/201911/15/05 Comp208 Computers in Engineering

5 Comp208 Computers in Engineering
01/06/2019 Bisection example X2 f(x2) X3=(x1+x2)/2 f(x3) X4=(x3+x1)/2 f(x4) X5=(x4+x1)/2 f(x5)≈0 X1 f(x1) 6/1/201911/15/05 Comp208 Computers in Engineering

6 Non-recursive implementation
double bisection_rf(DfD f, double x0, double x1, double tol){ double middle = (x0 + x1) / 2.0; while(fabs(f(middle)) > tol) { if(f(middle) * f(x0) < 0) x1 = middle; else if ( f(middle) * f(x1) < 0 ) x0 = middle; else printf("Should not be here! f(x0) and f(x1) are the same sign!\n"); middle = (x0 + x1) / 2.0; } return middle; 6/1/201911/15/05 Comp208 Computers in Engineering

7 Bisection-recursive implementation
01/06/2019 Bisection-recursive implementation double bisection_rf(DfD f, double x0, double x1, double tol) { double middle = (x0 + x1) / 2.0; if((middle - x0) < tol) return middle; else if(f(middle) * f(x0) < 0.0) // From the Intermediate Value Theorem, if there is a sign change then there is a root. return bisection_rf(f, x0, middle, tol); else return bisection_rf(f, middle, x1, tol); } 6/1/201911/15/05 Comp208 Computers in Engineering

8 Bisection complete program
01/06/2019 Bisection complete program #include <stdio.h> #include <math.h> typedef double (*DfD) (double); double bisection_rf(DfD f, double x0, double x1, double tol) { double middle = (x0 + x1) / 2.0; if(fabs(f(middle)) < tol) return middle; else if(f(middle) * f(x0) < 0.0) // From the Intermediate Value Theorem, if there is a sign change then there is a root. return bisection_rf(f, x0, middle, tol); else return bisection_rf(f, middle, x1, tol); } double func(double x){ return x*x - 5; } int main(){ double root, tolerance; tolerance = ; root = bisection_rf(func, -4, 1, tolerance); printf("root=%lf\n", root); 6/1/201911/15/05 Comp208 Computers in Engineering

9 Bisection counter-example
01/06/2019 Bisection counter-example If f(x1) and f(x2) are both positive or both negative, unable to find the root! X1 f(x1) X3=(x1+x2)/2 f(x3) x2 f(x2) 6/1/201911/15/05 Comp208 Computers in Engineering

10 Comp208 Computers in Engineering
01/06/2019 Secant method X1 f(x1) x2 f(x2) f(x4) f(x5)≈0 f(x3) x3 6/1/201911/15/05 Comp208 Computers in Engineering

11 Comp208 Computers in Engineering
01/06/2019 Secant method Assume a function to be approximately linear in the region of interest. Each improvement is taken as the point where the approximating line crosses the axis. 6/1/201911/15/05 Comp208 Computers in Engineering

12 Comp208 Computers in Engineering
Secant Non-recursive double secant(DfD f, double x1, double x2, double tol, double count){ double slope = (f(x1) - f(x2) )/(x1-x2); double x = x2 - f(x2)/slope; while(fabs(f(x)) > tol && count-->0){ x1 = x2; x2 = x; slope = (f(x1) - f(x2) )/(x1-x2); x = x2 - f(x2)/slope; } return x; 6/1/201911/15/05 Comp208 Computers in Engineering

13 Comp208 Computers in Engineering
01/06/2019 False position method The secant method retains only the most recent estimate, so the root does not necessarily remain bracketed. Combined with bisection in order to bracket the root. 6/1/201911/15/05 Comp208 Computers in Engineering

14 False position non-recursive
double falseposition(DfD f, double x1, double x2, double tol){ double slope = (f(x1) - f(x2) )/(x1-x2); double x = x2 - f(x2)/slope; while(fabs(f(x)) > tol){ if(f(x) * f(x1) < 0) x2 = x; else x1 = x; slope = (f(x1) - f(x2) )/(x1-x2); x = x2 - f(x2)/slope; } return x; 6/1/201911/15/05 Comp208 Computers in Engineering

15 Comp208 Computers in Engineering
01/06/2019 Newton-Raphson Much like secant, but using the real derivative x1 f(x1) x4 f(x4)≈0 f(x2) x2 x3 f(x3) 6/1/201911/15/05 Comp208 Computers in Engineering

16 Comp208 Computers in Engineering
01/06/2019 Newton-Raphson double newton_raphson_rf(DfD f, double x0, double tol) { double h = 0.001; double derivative; do{ derivative = centered3_diff(f, x0, h); x0 = x0 - f(x0)/derivative; } while(fabs(f(x0)) > tol); return x0; } 6/1/201911/15/05 Comp208 Computers in Engineering

17 How to calculate derivative
01/06/2019 How to calculate derivative Forward 3 points differentiation Backward 3 points differentiation Centered 3 points differentiation 6/1/201911/15/05 Comp208 Computers in Engineering

18 Comp208 Computers in Engineering
01/06/2019 Forward 3 points Mathematical formula: double forward3_diff(double x, double h) { return (-3*f(x) + 4*f(x+h) - f(x+2*h)) / (2 * h); } 6/1/201911/15/05 Comp208 Computers in Engineering

19 Comp208 Computers in Engineering
01/06/2019 Backward 3 points Mathematical formula: double backward3_diff(double x, double h) { return (f(x - 2 * h) - 4 * f(x - h) + 3 * f(x)) / (2 * h); } 6/1/201911/15/05 Comp208 Computers in Engineering

20 Comp208 Computers in Engineering
01/06/2019 Centered 3 points Mathematical formula: double centered3_diff(double x, double h) { return (-f(x - h) + f(x + h)) / (2 * h); } 6/1/201911/15/05 Comp208 Computers in Engineering


Download ppt "Comp 208 Computers in Engineering Yi Lin Winter, 2007"

Similar presentations


Ads by Google