E7_Lecture21_F08_Solution_Nonlinear_EQ

E7_Lecture21_F08_Solution_Nonlinear_EQ - 1 E7: INTRODUCTION...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 E7: INTRODUCTION TO COMPUTER PROGRAMMING FOR SCIENTISTS AND PROGRAMMING FOR SCIENTISTS AND ENGINEERS ENGINEERS Lecture Outline Function fzero fzero Finds the zero of a single-variable function the zero of single function Syntax: [x0,fval] = fzero( @function ,xini) • function: actual name of the function actual name of the function 2 1. Review of Matlab function fzero fzero 2. Iterative methods for solving algebraic nonlinear equations li 3. The bisection method 4. Newton’s method Copyright 2007, Horowitz, Packard. This work is licensed under the Creative Commons Attribution-Share 2007 Horowitz Packard This work is licensed under the Creative Commons Attribution Alike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. E7 L19 • @function: function handle • xini initial guess of zero location • x0 zero location ( zero location (ii.e. function(x0)= 0) • fval = function(x0) (should be zero) E7 L19 Using fzero fzero Example: Lets find the zero of the function Lets find the zero of the function −x 3 Using fzero fzero Plotting the function the function 4 f1 ( x) = x + 2e − x − 3 function y = f1(x) y = x + 2*exp(-x) - 3; y ( x) = f1 ( x) = x + 2e − 3 Matlab syntax: function y = f1(x) y = x + 2*exp(-x) - 3; Matlab syntax: two zeros >> x = linspace(-1,5); >> plot(x,f1(x)) E7 L19 E7 L19 Using fzero fzero Finding the zeros of the zeros of 5 Using fzero fzero Finding the zeros of the zeros of 6 f1 ( x) = x + 2e − x − 3 function y = f1(x) y = x + 2*exp(-x) - 3; f1 ( x) = x + 2e − x − 3 function y = f1(x) y = x + 2*exp(-x) - 3; Matlab syntax: initial guess for search >> xin = 0; xin 0; [x,fval] = fzero(@f1,xin) x= -0.5831 fval = 0 Matlab syntax: initial guess for search >> xin = 4; xin 4; [x,fval] = fzero(@f1,xin) x= 2.8887 fval = -4.4409e-016 two zeros zeros two zeros zeros E7 L19 E7 L19 How does fzero work? fzero 7 Iterative methods for solving algebraic equations • Given a prescribed function prescribed function 8 It uses a combination of iterative algorithms: uses combination of iterative algorithms: • Bisection • Secant • Inverse quadratic interpolation methods. quadratic interpolation methods Today we will explain two methods: • Bisection (linear convergence) • Newton’s (quadratic convergence) (quadratic convergence) E7 L19 f ( x) want to find root that: • we want to find a root x* such that: Iterative methods: methods: • Start from an initial guess xo f x∗ = 0 () • Generate, through “easy” calculations, a sequence {xn } = xo , x1 , x2 ,... such that lim xn = x ∗ n →∞ and f x∗ = 0 E7 L19 () Criteria for stopping Iterative methods How can one assess whether a member xk of the sequence has converged to the root x* to within a given tolerance? 9 Three useful termination criteria: 1. Absolute error less than tolerance tol: error less than tolerance tol 10 | xk − x* |≤ tol 2. Relative error less than tolerance tol: error less than tolerance tol xo , x1 , x2 ,..., xk stop xk +1 , xk + 2 ,..., xk − x * | x* | ≤ tol ( x* ≠ 0) • The choice of the tolerance depends on the machine epsilon and the desired accuracy. epsilon and the desired accuracy. • We will call this tol E7 L19 3. Value of the function less than tolerance tol: f ( xk ) ≤ tol E7 L19 Modified termination criteria: Generally, the root x* is not known in advance. Therefore, we replace x* by xk+1 11 Bisection Method: • Suppose that that 12 f ( xL ) < 0 Then, there must exist: f ( x) is continuous, assume that is continuous assume that and f ( xR ) > 0 1. Absolute error less than tolerance tol: | xk +1 − xk |≤ tol 2. Relative error less than tolerance tol: f ( x) xL ≤ x* ≤ xR f x∗ = 0 xL () xk +1 − xk | xk +1 | ≤ tol ( xk +1 ≠ 0) E7 L19 x* xR x E7 L19 Bisection method iteration: • Define and re-bracket the root. 13 Bisection method iteration: • Define and re-bracket the root. 14 x1 = f ( x1 ) × f ( xR ) < 0 xL + xR 2 x2 = f ( x2 ) × f ( x1 ) < 0 x1 + xR 2 f ( x) x1 ≤ x* ≤ xR xL x1 f ( x) repeat step until stopping criterion is met x1 ≤ x* ≤ x2 x1 x2 xR x E7 L19 xR x E7 L19 Bisection method: Simple pseudo-code pseudo- 15 Matlab examples of the bisection method function xmid = … showbisection(fhan,L,R,Xlimit,Ylimit) showbisection(fhan,L,R,Xlimit,Ylimit) where: • • • • • fhan L R Xlimit Ylimit : function handle function handle : xL f xL × f xR < 0 : xR : 2x1 vector of plot x axis limits :2x1 vector of plot y axis limits li 16 % Start with xL, xR and f(xL)* f(xR)<0) fL = f(xL); fR = f(xR); Stopping_Criteria_Not_Met = 0.5*(xL+xR); % compute midpoint = f(xM); fL*fM <0 % re-bracket the root xR = xM; % replace R with M else xL = xM; % replace L with M end end E7 L19 while xM fM if () () E7 L19 Matlab examples of the bisection method 17 Matlab examples of the bisection method 18 f ( x) = cos( x) − x 2 function y = myfun1(x) myfun1(x) y = cos(x) - x^2; 4 f ( x) = e x − 3 cos( x) function y = myfun2(x) y = exp(x) - 3*cos(x); >> showbisection(@myfun1,0, pi/2,[-1 2],[-3 4]) 2] 4]) >> showbisection(@myfun2,.2, 2 ,[0 2.5],[-3 7]) 7]) 6 2 4 2 0 0 -2 -1 0 1 E7 L19 -2 2 0 0.5 1 1.5 2 2.5 E7 L19 Bisection method: Convergence rate The bisection method has a linear convergence rate bisection method has linear rate • Let: 19 Quadratic convergence rate (faster than linear) Methods that have a quadratic convergence rate that have quadratic rate • Let: 20 ek = x * − xk ek = x * − xk and ek < 1 2 • Then, for very large n, • Example: the sequence en +1 ≤ C en • Then, for very large n, for very large n, • Example: the sequence en +1 ≤ C en {xn } = 3, 2.5, 2.25, 2.125, 2.0625,... converges linearly to 2 with C = 1 {xn } = 3, 2 1 2 , 2 1 4 , 2 116 , 2 1 256 , 2 1 65536 ,... converges quadratically to 2 with C = 1 2 {en } = 1, 0.5, 0.25, 0.125, 0.0625,... E7 L19 {en } = 1, 1 2 , 1 4 , 1 16 , 1 256 , 1 65536 ,... E7 L19 Quadratic convergence overtakes linear convergence 0.5 0.45 0.4 0.35 0.3 0.25 0.2 0.15 0.1 0.05 0 0 5 10 15 20 21 Newton’s Method • Also known as the Newton-Raphson method. known as the Newton method • At each step, the function and its derivative must be th it evaluated at a single point. • If it converges, the convergence is quadratic. • More complicated than bisection, but can work better better. • Generalizes to N equations in N unknowns. 22 en +1 = 0.5 × en en +1 = 0.1× en en +1 = 1× en 2 1.8 1.6 1.4 1.2 1 0.8 0.6 0.4 0.2 0 2 4 6 8 10 12 14 16 18 20 x 10 -6 2 en +1 = 0.5 × en en +1 = 0.1× en en +1 = 1× en 2 E7 L19 E7 L19 The straight-line approximation to f(x) at x0 straightapproximation 23 The straight-line approximation to f(x) at x0 straightapproximation 24 graph of h(x): the straight-line of h(x) straight approximation to f(x) at x0 f(x0) graph of f(x) of f(x) f(x0) graph of h(x): the straight-line of h(x) the straight approximation to f(x) at x0 graph of f(x) of f(x) x0 x x0 x Properties of h(x) : Function h(x) : functions are equal at x0 slope of h (everywhere) equals the slope of f at x0 graph of h is a straight line E7 L19 E7 L19 h ( x0 ) = f ( x0 ) h ' ( x ) = f ' ( x0 ) h( x ) = f ( x0 ) + f ' ( x0 )( x − x0 ) h ( x ) = ax + b Newton’s method: basic idea h( x ) f(x0) * xapp 25 Newton’s method: basic idea h( x ) f(x0) * xapp 26 f(x) f(x) x Approximate x* pp * h xapp = 0 * app * x0 by x*app where x x * x0 x Approximate x* by x*app where pp () h( x ) = f ( x ) + f ( x )( x ' 0 0 * app − x0 = 0 E7 L19 ) x * app f ( x0 ) = x0 − ' f ( x0 ) E7 L19 Newton’s method: iteration h( x ) f(xk) 27 Matlab examples of Newton’s method function XN = … shownewton(fhan,dhan,X,Xlimit,Ylimit) where: • • • • • fhan dhan X Xlimit Ylimit : function handle : function derivative handle : initial guess xo : 2x1 vector of plot x axis limits :2x1 vector of plot y axis limits 28 xk +1 f(x) x * xk x Repeat approximation replacing: x0 → xk x*app → xk +1 f ( xk ) xk +1 = xk − ' f ( xk ) E7 L19 E7 L19 Matlab examples of Newton’s method 29 Matlab examples of Newton’s method 30 f ( x) = cos( x) − x 2 function y = myfun1(x) y = cos(x) - x^2; cos(x) f '( x) = − sin( x) − 2 x function y = mydfun1(x) y = -sin(x) - 2*x; sin(x) f ( x) = e x − 3cos( x) function y = myfun2(x) y = exp(x) - 3*cos(x); exp(x) f '( x) = e x + 3sin( x) function y = mydfun2(x) y = exp(x) + 3*sin(x); exp(x) >>shownewton(@myfun1,@mydfun1,0.3,[-.2 2],[-3 3]) 3 2 1 0 -1 -2 -3 0 0.5 1 1.5 2 >>shownewton(@myfun2,@mydfun2,2,[0 2.5],[-3 7]) 6 4 2 0 -2 E7 L19 0 0.5 1 1.5 2 2.5 E7 L19 Newton’s method 31 Matlab examples of Newton’s method 32 f (x ) xk +1 = xk − ' k f ( xk ) • How do we compute the derivative numerically? f ( x) = tan −1 (1.3917x) function y = myfun4(x) myfun4(x) y = atan(1.3917*x); f ' ( xk ) f '( x) = • We will explain numerical differentiation in lecture 24 • Possible problem: Divergence! E7 L19 1.3917 1 + (1.3917 x) 2 function y = mydfun4(x) mydfun4(x) beta = 1.3917; y = beta/(1+beta*beta*x*x) >>shownewton(@myfun4,@mydfun4, 1,… [-1.5 1.5],[-1.5 1.5]) x(0) = 1 E7 L19 Matlab examples of Newton’s method 33 f ( x) = tan −1 (1.3917x) f '( x) = 1.3917 1 + (1.3917 x) 2 Matlab examples of Newton’s method 34 f ( x) = tan −1 (1.3917x) function y = myfun4(x) myfun4(x) y = atan(1.3917*x); x(0) = 1 1.5 1 0.5 0 -0.5 -1 -1.5 -1 f '( x) = 1.3917 1 + (1.3917 x) 2 function y = mydfun4(x) mydfun4(x) beta = 1.3917; y = beta/(1+beta*beta*x*x) >>shownewton(@myfun4,@mydfun4, 1.001,… [-1.5 1.5],[-1.5 1.5]) Converges slowly!! slowly!! 0 1 E7 L19 x(0) = 1.001 E7 L19 Matlab examples of Newton’s method 35 f ( x) = tan −1 (1.3917x) x(0) = 1.001 1.5 1 0.5 0 -0.5 -1 -1.5 -1 f '( x) = 1.3917 1 + (1.3917 x) 2 Summary What did we learn today? did we learn today? • Matlab’s function fzero find solutions of nonlinear algebraic equations (roots), using a combination of iterative linear (bisection) and quadratic algorithms. • Several termination conditions may be employed to termination conditions may be employed to end the iterative calculation of approximate roots. • Newton’s method exhibits a quadratic rate of convergence. However, the algorithm is less robust than the bisection algorithm and may not converge. 36 Diverges!! 0 1 E7 L19 E7 L19 ...
View Full Document

This note was uploaded on 11/01/2009 for the course ENGLISH 7 taught by Professor Sengupta during the Spring '09 term at University of California, Berkeley.

Ask a homework question - tutors are online