Numeial Modeling in Biomedial Systems BME 15:35 Letue 7 9/6/17
Nonlinea Systems Dunn Chapte 5 Nonlinea equations Root finding Baketing methods Open methods Gaphial Bisetion False Position Newton s method Fied-point iteation Newton- Raphson Bent s method
How do we solve this nonlinea equation to find? 3 7 + 6 = Plot f () = 3 7 +6 vesus and see whee it osses zeo? 1 f() -1 - f() = 3 7 +6 Roots nea = -3,, 1-3 -4-3 - -1 1 3 - Gaphial methods ae not vey auate but they an give us a lue about the egion whee the oot(s) lie - Guess a value fo, evaluate whethe f () =? - This will take a long time - Bette to develop systemati methods to seah fo f () = until ou equied level of auay is eahed
Baketing Methods We want to find the oots of f(), i.e. the values of whee f() = Whee to stat looking? Baketing methods use the fat that the value of f() hanges sign at eah oot f() 1-1 - + + - - + - f() = 3 7 + 6-3 -4-3 - -1 1 3 Stategy: Find two values of, ( 1, ) giving positive and negative values fo f() The oot must lie somewhee between these values of Naow the distane between ( 1, ) to lose in on the tue oot
Eample - Falling objet fom Letue m F up = dag foe mass, m F down = gavitational foe Using Newton s nd law leads to an analytial epession fo veloity v as a funtion of time t and system paametes mass (m) and dag oeffiient (): v gm / m t 1 e Given g,m,,t we an alulate v t But what if we ae given g,m,v,t and we want to alulate? v(t) 4 1 3 If an objet has mass m = 68.1 kg, what dag oeffiient () is neessay in ode to eah a veloity of v = 4 m/s at time t = 1 seonds? 1 t Poblem: We an t eaange the epession in the fom = f (g,m,v,t)
Reap gm / m How to solve a nonlinea equation? vt 1 e Eg., find when g = 9.8 m/s, m = 68.1 kg, v = 4 m/s, t = 1 s t Solve this nonlinea equation fo : 4 9.8 68.1 1 e / 68.1 1 What ae the value(s) of whih satisfy: 9.8 68.1 /68.1 1 1 e 4 Whee does the funtion f() equal zeo? f 68.1 / 68.1 1 e 4 9.8 1 These ae just diffeent ways of asking the same question
We need to find the oot(s) of f 68.1 /68.1 1 e 4 9.8 1 I.e. the value(s) of whih lead to f () = We an stat to make some guesses fo, and see what we get fo f (): Guess fo f() 4 34.115 8 17.653 1 6.67 16 -.69-8.41 f() 6 5 4 3 1-1 5 1 15 Dag oeffiient, / m 14.75 looks lose to zeo. v t gm 1 e t gives v = 4.59 m/s when = 14.75 Gaphial methods ae not vey auate, but they an povide us with useful initial guesses to use with othe methods
Bisetion Method In geneal: If f () is eal and ontinuous in the ange between l and u, and f( l ) f( u ) <, then thee is at least one oot in the ange l to u f() 6 5 4 3 1-1 5 1 15 Dag oeffiient, Step 1: Choose lowe ( l ) and uppe ( u ) guesses fo the loation of the oot. Step : If f( l ) f( u ) <, then the oot is estimated to lie at = ( l + u ) / Step 3: If f( l ) f( ) <, then the tue oot must lie between l and If f( l ) f( ) >, then the tue oot must lie between and u Retun to step with the new guesses fo the inteval, epeat until the inteval width is suffiiently small (i.e. the estimate fo the oot is within an aeptable toleane)
Repeat the pevious (falling mass) eample using the bisetion method: Eample - Bisetion Method 6 5 4 f 68.1 /68.1 1 e 4 9.8 1 Step 1: Make initial guesses fo l and u l = 1, u = 16 Chek: f (1) f (16) = 6.67 -.69 (i.e. < ) a oot eists in this ange Step : Estimate the oot as lying at the midpoint of this ange: = ( l + u ) / = 14 f() 3 1-1 5 1 15 Dag oeffiient, Step 3: Calulate f( l ) f( ) = f (1) f (14) = 9.517. I.e. >, the tue oot does not lie between l and the tue oot must lie between and u (between 14 and 16) Retun to step with the new guesses fo the inteval: new estimate fo = (14+16)/ = 15 test whethe the oot lies between 14-15 o 15-16
6 The tue value fo the oot is = 14.78 We an evaluate the eo in the value geneated by the bisetion method at eah iteation: As peviously when evaluating the eo in iteative methods, we an use the uent and pevious values to geneate an appoimate elative eo, a : a new new old 1% f() 5 4 3 1-1 5 1 15 Dag oeffiient, Iteation l u a [%] t [%] 1 1 16 14-5.79 14 16 15 6.667 1.487 3 4 5 6 14 14.5 14.75 14.75 15 15 15 14.875 14.5 14.75 14.875 14.815 3.448 1.695.84.4 1.896.4.641.19 - Afte 6 iteations, the appoimate elative eo a <.5% (and t is even lowe)
Compae the appoimate elative eo to the tue elative eo: Iteation a [%] t [%] 1-5.79 6.667 1.487 3 3.448 1.896 4 1.695.4 5.84.641 6.4.19 With the bisetion method, the tue elative eo is always less than the appoimate elative eo: (This is a good thing fo engineeing design / poblem solving) If we teminate the alulation when a < s, we an be onfident that the tue value is within ou stopping iteion Relative Eo [%] 8 7 6 5 4 3 1 Appoimate Eo Tue Eo 4 6 8 Iteation #
With the bisetion method, the tue elative eo is always less than the appoimate elative eo. Why is this the ase? Choose lowe and uppe guesses fo the oot: Tue oot: l u The bisetion method gives a new estimate at l Bisetion method: Tue eo, t u This tue eo must always be less than /: Ou estimated (appoimate) eo a is always equal to /: new old u l So ou elative appoimate eo will always oveestimate the tue eo: a new new old 1%
Funtions in Matlab Matlab has many built-in funtions eg. sin( ), ep( ), mean( ), binde( ), plot( ).., We an also define ou own funtions whih an be alled (by name) fom the ommand line, o fom within othe sipts / m-files
Anonymous Funtions in Matlab Suppose we want to find the minimum of the following funtion a when a = 1, b = -, = 1 f b f() 5 4 3 f()=a +b+, a=1,b=-,=1 lea all; lose all; a = 1; b = -; = 1; f = @()(a*.^ + b* + ); Ceate an anonymous funtion (@): 1-6 -4-4 6 = [-*pi:.1:*pi]; plot(,f()); % Plots funtion f() ove [-pi < < pi] title('f()=a^+b+, a=1,b=-,=1'); label(''); ylabel('f()'); hold on; % Find and plot the minimum minimum = fminbnd(f,-,); % We an pass ou funtion dietly to the minimization outine plot(minimum,f(minimum),'o'); % We an evaluate ou funtion at the value minimum Anonymous funtions an have moe than one vaiable eg: f = @(,y)(a*.^+b*y.^)
Eample: A evesible hemial eation involving eatants A and B foming podut C an be witten as: A + B C (Eg. H + CO CH 3 OH) and haateized by the equilibium elationship:, whee C i epesents the onentation of onstituent i. C K CaCb If the vaiable epesents the numbe of moles of podut C geneated by this eation, we an wite the onentation of podut C as (C, + ), whee C, is the initial onentation of C,. We an efomulate the equilibium elationship as K C, C C a, b,
K C, C C a, b, Question: If the equilibium onstant fo this eation is K =.16, and the initial mola onentations of onstituents a, b, and ae C a, = 4, C b, = 8, C, = 4, espetively, detemine the numbe of moles of podut C (epesented by vaiable ) geneated unde these onditions. We would like to wite = and plug in ou values fo K, C a,, C b,, C,, But we an t. Equivalently, ou poblem is to find the values of whih satisfy: f C, C C a, b, I.e. find the oot(s) of f() = K 4 4 8.16
Step 1: Plot the funtion and estimate whee the oot lies K =.16; Ca = 4; Cb = 8; C = 4; = linspae(,,5); % This is the funtion we ae inteested in (afte eaanging): f = @() (C+)./(((Ca-*).^).*(Cb-)) - K; figue; plot(,f(),'b-'); label('amount of C podued, [Mol]'); ylabel('f()'); ylim([-.5.5]); It looks like the oot is aound 16
Step : Implement the baketing method to get an auate estimate fo the oot Xl = 1; Xu = ; i = 1; i_ma = 1; Es = 1; while (1) X = (Xl + Xu)/; % Initial lowe and uppe guesses fo the oot % Iteation ounte stats at one % Ma # iteations allowed % Stopping eo (when % elative appo eo < Es) % New oot estimate fom Baketing Method if i > 1 % Ensue that i > 1 (an t alulate Ea on 1st loop) Ea = abs((x - X_old)/X)*1; % Calulate appo. elative eo (%) Ea_stoe(i) = Ea; % Stoe the appo. elative eo end test = f(xl)*f(x); if test < Xu = X; else Xl = X; end X_old = X; oot_stoe(i) = X; if i > 1 if Ea < Es i > i_ma beak end end i = i + 1; end % Test whethe f() hanges sign between Xl and X % If test <, oot must be between Xl and X % The alulated oot beomes ou new uppe guess (Xu) % If test is not <, oot must be between X and Xu % The alulated oot beomes ou new lowe guess (Xl) % Assign the latest oot to X_old fo use in the net loop % Stoe the latest oot fo plotting late % If we ae on the,3,4 iteation, hek the appo. eo % If eithe ondition is tue, eit the loop % Inement the iteation ounte fo the net loop
Step 3: Plot out the esults disp('calulated oot'); X disp('# of Iteations'); i disp('appoimate (Relative) Eo'); Ea figue; subplot(1,3,1); plot([1:i],oot_stoe,'-b'); label('iteation #'); ylabel('estimated Root, X'); ais squae; title('bisetion Algoithm fo oot of f()'); lim([1 i]); subplot(1,3,); plot([:i],ea_stoe(:end),'b-'); label('iteation #'); ylabel('\epsilon_a [%]'); ais squae; title('appoimate Eo, \epsilon_a [%]'); lim([1 i]); legend(['\epsilon_s = ',numst(es),'%']); subplot(1,3,3); plot([:i],ea_stoe(:end),'b-'); label('iteation #'); ylabel('\epsilon_a [%]'); ais squae; title('appoimate Eo, \epsilon_a [%]'); ylim([ 5]); lim([3.5 7]); legend(['\epsilon_s = ',numst(es),'%']);
Nonlinea Systems Dunn Chapte 5 Net time: False-position, Newton s method, Fied point iteation Nonlinea equations Root finding Baketing methods Open methods Gaphial Bisetion False Position Newton s method Fied-point iteation Newton- Raphson Bent s method
Open Methods fo Root Finding: Newton-Raphson The most widely used method fo oot finding 1) Stat with one initial guess fo the oot ( i ) f () Slope = f () f() ) Daw a line tangent to the point at [ i, f( i )] f ( i ) y = f ( i ) 3) Ou new estimate fo the oot oesponds to the -value whee the tangent fom [ i, f( i )] osses the -ais i+1 i = ( i i+1 ) The slope of the tangent line f ( i ) equals f i y i f i i1 Reaanging this in tems of the new oot estimate ( i+1 ): i1 i f i f i
Eample: Use the Newton-Raphson method to find the oot of f() = e - Given a funtion f(), its deivative f (), and an initial guess fo the oot ( i, i = ), the Newton-Raphson equation gives us a new oot estimate ( i+1 ): i1 i f f i i Funtion: f () = e - Deivative: f () = e - 1 Initial guess: = Use these epessions in the N-R equation: i1 i f f i i i1 i i e e i i 1
Oiginal poblem: Find the oot of f() = e -.5 f() = e - -.3 f() = e - - 1.5 (Tue oot =.567143).5. 1.15 y.5 y.1.5 -.5-1 -.5-1.5 -.5.5 1 1.5 -.1.4.45.5.55.6 Newton-Raphson Iteation i t [%] 1 1.5 11.8.566311.147 3.567143.1-5 4.567143 < 1-8
Tanspot Eample: Kogh Tissue Cylinde Model fo studying tanspot of metabolites fom apillaies to suounding tissue Tissue z Capillay The itial adius it is the distane at whih the metabolite onentation is edued to zeo: C m C it vaies with distane along the apillay aoding to: it R ln R R 1 R 4D C T t m D 4 V T DT R 1z R 1 K whee R it t m.
Question (i) : What is the itial tissue adius ( it ) whee gluose is no longe supplied to ells, at a distane z =.5 m unde the onditions listed in the table? 1 1 4 4 1 ln R K D z R V D t R C D R R R T T m T. m it t R whee We annot eaange this equation in the fom it = We need to find the oot of f ( it ) =. (The value of it whih satisfies f ( it ) = ). 1 ln R Dz B A R R 4 m T t R C D A 1 K D B T 4 T V D D Let s simplify this nonlinea epession: whee (A, B, and D ae just onstants whih an be found fom values given in the table) and z is given in the question.
Let s make this epession even simple: Let = R R ln R A B DzR 1 A B Dz ln 1 If we solve this nonlinea equation fo (find the oots of f ( ) ln( ) A E( 1) ) then we an easily onvet the answe fom R it : E B Dz Wite Matlab ode to implement the Newton-Raphson method to solve this poblem: Fist steps: 1) Define the elevant onstants ) Define the funtion we want to solve (find the oot(s) of) 3) Plot out the funtion to obtain a ough idea of whee the oot(s) lie
Net steps: 4) Define additional onstants (initial guess fo the oot ( 1 ), stopping eo ( s ), ) 5) Set up a loop to keep geneating and stoing new estimates fo the oot, using the Newton-Raphson method i1 6) Estimate and stoe the eo ( a ) at eah iteation of the loop (if i >1). Eit the loop if this eo is small enough ( a < s ) 7) Plot out the oot estimates and eo estimates as a funtion of iteation numbe i f f i i
Question (pat ii): You just alulated it fo a single aial loation of z =.5 m. Tissue z How does it vay as z anges fom 1 m to.15 m? Capillay Modify you ode fom pat (i) to alulate it at lots of z values between 1 m and.15 m Stoe the values alulated fo it at eah z value. Plot it vesus distane z.
Poblems with the Newton-Raphson method: (1) Slow Convegene Eample: f () = 1 1 Find the oot fom an initial guess of =.5 1 116 -.99 1.8 117 8 6 -.99 -.994 -.996 1 1.6 1.4 y 4 y -.998-1 y 1. 1 1 3 4 5-1. -1.4..3.4.5.6.7.8.8 1.6 49 5 51 5 53 The new guess is the value whee the tangent fom f( ) osses the -ais Iteation i.5 1 51.65 46.485 3 41.8365 The algoithm is slowly onveging on the tue oot ( = 1) Due to (1) the natue of the funtion f () () ou initial guess fo the oot,
Poblems with the Newton-Raphson method: () No Convegene f () f () 1 1 Point of infletion nea the oot Tapped at loal ma / min f () f () 1 1 Slope of f() nea oot Slope of f() = at estimated oot
Poblems with the Newton-Raphson method: No Convegene Thee is no geneal onvegene ule fo the Newton-Raphson method Convegene depends on the natue of f() and the initial guess Computational methods using N-R should inlude some safety featues: 1. Substitute the final estimate fo the oot bak into f (). Chek whethe the esult is lose to zeo. Limit the numbe of iteations to some maimum value 3. Chek whethe the slope f () = at any iteation 4. Make the pogam plot the estimated oot at eah iteation fo inspetion
Nonlinea Systems Dunn Chapte 5 Nonlinea equations Root finding Baketing methods Open methods Gaphial Bisetion False Position Newton s method Fied-point iteation Newton- Raphson Bent s method