Mathematical Methods (10/24.539) V. General Boundary Value Problems (BVPs)

Similar documents
NUMERICAL DIFFERENTIATION

Chapter 12. Ordinary Differential Equation Boundary Value (BV) Problems

Numerical Heat and Mass Transfer

CHAPTER 5 NUMERICAL EVALUATION OF DYNAMIC RESPONSE

Chapter 5. Solution of System of Linear Equations. Module No. 6. Solution of Inconsistent and Ill Conditioned Systems

Lecture 21: Numerical methods for pricing American type derivatives

NON-CENTRAL 7-POINT FORMULA IN THE METHOD OF LINES FOR PARABOLIC AND BURGERS' EQUATIONS

Difference Equations

EEE 241: Linear Systems

Module 3 LOSSY IMAGE COMPRESSION SYSTEMS. Version 2 ECE IIT, Kharagpur

Lecture 12: Discrete Laplacian

Introduction to Vapor/Liquid Equilibrium, part 2. Raoult s Law:

Errors for Linear Systems

Lectures - Week 4 Matrix norms, Conditioning, Vector Spaces, Linear Independence, Spanning sets and Basis, Null space and Range of a Matrix

LINEAR REGRESSION ANALYSIS. MODULE IX Lecture Multicollinearity

Structure and Drive Paul A. Jensen Copyright July 20, 2003

4DVAR, according to the name, is a four-dimensional variational method.

MMA and GCMMA two methods for nonlinear optimization

ME 501A Seminar in Engineering Analysis Page 1

More metrics on cartesian products

Transfer Functions. Convenient representation of a linear, dynamic model. A transfer function (TF) relates one input and one output: ( ) system

One-sided finite-difference approximations suitable for use with Richardson extrapolation

Inner Product. Euclidean Space. Orthonormal Basis. Orthogonal

Kernel Methods and SVMs Extension

FTCS Solution to the Heat Equation

CSci 6974 and ECSE 6966 Math. Tech. for Vision, Graphics and Robotics Lecture 21, April 17, 2006 Estimating A Plane Homography

Linear Approximation with Regularization and Moving Least Squares

2 Finite difference basics

Foundations of Arithmetic

New Method for Solving Poisson Equation. on Irregular Domains

DUE: WEDS FEB 21ST 2018

The Multiple Classical Linear Regression Model (CLRM): Specification and Assumptions. 1. Introduction

Econ107 Applied Econometrics Topic 3: Classical Model (Studenmund, Chapter 4)

Numerical Solution of Ordinary Differential Equations

The Finite Element Method

Appendix B. The Finite Difference Scheme

Integrals and Invariants of Euler-Lagrange Equations

Consistency & Convergence

For now, let us focus on a specific model of neurons. These are simplified from reality but can achieve remarkable results.

Linear Feature Engineering 11

Note 10. Modeling and Simulation of Dynamic Systems

Canonical transformations

Finite Element Modelling of truss/cable structures

Additional Codes using Finite Difference Method. 1 HJB Equation for Consumption-Saving Problem Without Uncertainty

3.1 Expectation of Functions of Several Random Variables. )' be a k-dimensional discrete or continuous random vector, with joint PMF p (, E X E X1 E X

Lecture Notes on Linear Regression

1 GSW Iterative Techniques for y = Ax

Psychology 282 Lecture #24 Outline Regression Diagnostics: Outliers

PHYS 705: Classical Mechanics. Calculus of Variations II

Analytical Chemistry Calibration Curve Handout

APPENDIX A Some Linear Algebra

College of Computer & Information Science Fall 2009 Northeastern University 20 October 2009

Generalized Linear Methods

1 Matrix representations of canonical matrices

THE CHINESE REMAINDER THEOREM. We should thank the Chinese for their wonderful remainder theorem. Glenn Stevens

Relaxation Methods for Iterative Solution to Linear Systems of Equations

6.3.4 Modified Euler s method of integration

A New Refinement of Jacobi Method for Solution of Linear System Equations AX=b

STAT 309: MATHEMATICAL COMPUTATIONS I FALL 2018 LECTURE 16

Grover s Algorithm + Quantum Zeno Effect + Vaidman

Global Sensitivity. Tuesday 20 th February, 2018

n α j x j = 0 j=1 has a nontrivial solution. Here A is the n k matrix whose jth column is the vector for all t j=0

Some modelling aspects for the Matlab implementation of MMA

Lecture 13 APPROXIMATION OF SECOMD ORDER DERIVATIVES

Problem Set 9 Solutions

Example: (13320, 22140) =? Solution #1: The divisors of are 1, 2, 3, 4, 5, 6, 9, 10, 12, 15, 18, 20, 27, 30, 36, 41,

Formulas for the Determinant

Lecture 10 Support Vector Machines II

A Hybrid Variational Iteration Method for Blasius Equation

Bézier curves. Michael S. Floater. September 10, These notes provide an introduction to Bézier curves. i=0

ALGORITHM FOR THE CALCULATION OF THE TWO VARIABLES CUBIC SPLINE FUNCTION

36.1 Why is it important to be able to find roots to systems of equations? Up to this point, we have discussed how to find the solution to

Workshop: Approximating energies and wave functions Quantum aspects of physical chemistry

Limited Dependent Variables

Solving Nonlinear Differential Equations by a Neural Network Method

Bezier curves. Michael S. Floater. August 25, These notes provide an introduction to Bezier curves. i=0

Implicit Integration Henyey Method

Hongyi Miao, College of Science, Nanjing Forestry University, Nanjing ,China. (Received 20 June 2013, accepted 11 March 2014) I)ϕ (k)

THE SUMMATION NOTATION Ʃ

Chapter - 2. Distribution System Power Flow Analysis

x = , so that calculated

Singular Value Decomposition: Theory and Applications

Report on Image warping

Salmon: Lectures on partial differential equations. Consider the general linear, second-order PDE in the form. ,x 2

= z 20 z n. (k 20) + 4 z k = 4

A new Approach for Solving Linear Ordinary Differential Equations

CME 302: NUMERICAL LINEAR ALGEBRA FALL 2005/06 LECTURE 13

Chapter 3 Differentiation and Integration

Fundamental loop-current method using virtual voltage sources technique for special cases

Application of B-Spline to Numerical Solution of a System of Singularly Perturbed Problems

Chapter 13: Multiple Regression

Chapter Newton s Method

Lecture 5 Decoding Binary BCH Codes

Some Comments on Accelerating Convergence of Iterative Sequences Using Direct Inversion of the Iterative Subspace (DIIS)

2.3 Nilpotent endomorphisms

Open Systems: Chemical Potential and Partial Molar Quantities Chemical Potential

MATH 5630: Discrete Time-Space Model Hung Phan, UMass Lowell March 1, 2018

Section 8.3 Polar Form of Complex Numbers

Chapter 6. Supplemental Text Material

Week 5: Neural Networks

Transcription:

Mathematcal Methods (10/4.539) V. General Boundary Value Problems (BVPs) Introducton In the prevous secton we focused on varous schemes (both analytcal and numercal) for solvng general IVPs. We now emphasze another mportant class of problems known as Boundary Value Problems (BVPs). The dfference between these two problem classes s related to the specfcaton of the n condtons needed to unquely determne the n arbtrary coeffcents n the general soluton to an n th order ODE. The n condtons are specfed at a sngle (ntal) pont for IVPs. However, for a BVP, the n boundary condtons are specfed at two or more ponts n the doman of nterest. Ths modfcaton results n completely dfferent algorthms for solvng these problems - especally n the numercal soluton schemes used for BVPs. Analytcal schemes for lnear constant coeffcent non-homogeneous BVPs have already been dscussed n Secton II - Lnear Dfferental Equatons. Exact soluton methods for general nonlnear BVPs are smply not avalable (although a number of trcks can be appled to specfc problems). Varable coeffcent lnear problems do have a general soluton procedure - referred to as the Power Seres Soluton Method - but t s often qute tedous and not overly useful for solvng general engneerng desgn problems. The Power Seres technque s qute powerful, however, and t s very useful for dervng analytcal solutons for several mportant problems. We wll dscuss ths method n some detal later n ths course (see Secton VII - Power Seres Soluton Method and Secton VIII - Specal Functons). Note also that Egenvalue Problems, a specal class of homogeneous BVPs, are dscussed later n these notes (see Secton IX - The Sturm-Louvlle Problem). For now, we are nterested n the soluton of non-homogeneous BVPs that occur n real engneerng applcatons, and the only general soluton method for these problems rely on numercal schemes. In partcular, n ths secton of notes we focus on two numercal technques for treatng general non-homogeneous BVPs wth a sngle ndependent varable - the Shootng Method and the Fnte Dfference Method. The Shootng Method s essentally an teratve applcaton of the numercal ntegraton technques used for IVPs. It s easy to apply n most cases, but t s not approprate for solvng BVPs wth more that one ndependent varable (PDEs are dscussed later n ths course). The Fnte Dfference Method takes a completely dfferent approach to the problem. It essentally converts the ODE nto a coupled set of algebrac equatons, wth one balance equaton for each fnte volume or node n the system. Ths technque s very powerful, and t s easly extended to multdmensonal and space-tme stuatons. Thus, ths method wll be used agan when dscussng the numercal soluton technques approprate for general PDEs (later n the course). The general notaton assocated wth BVPs and an overvew of the Shootng and Fnte Dfference methods are dscussed wthn the followng subsectons: General Notaton for BVPs The Shootng Method Basc Algorthm

Math Methods -- Secton V: General Boundary Value Problems (BVPs) Pckng the Next α Second Order BVPs Example 5.1 - A -pont BVP va the Shootng Method The Fnte Dfference Method Basc Concepts Dervatve Approxmaton usng the Taylor Seres Some Addtonal Dervatve Approxmatons Example 5. - A -pont BVP va the Fnte Dfference Method A Classcal BVP - Heat Transfer n a Crcular Fn Development of the Mathematcal Model Example 5.3A - Shootng Method Soluton to the Crcular Fn Problem Example 5.3B - Fnte Dfference Soluton to the Crcular Fn Problem The reader mght also vst my webste for my undergraduate Appled Problem Solvng wth Matlab course (see www.profjrwhte.com/courses.htm). There are some examples of both the Shootng method and the Fnte Dfference method. These are qute straghtforward snce the treatment n that course s qute ntroductory. However, these extra examples may be useful!!! Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 3 General Notaton for BVPs Boundary Value Problems (BVPs) dffer n one very mportant respect relatve to Intal Value Problems (IVPs). Ths dfference s related to the specfcaton of the n condtons needed to unquely determne the n arbtrary coeffcents n the general soluton to an n th order ODE. For a BVP, the n boundary condtons are specfed at two or more ponts n the doman of nterest (whereas the n condtons are specfed at a sngle pont for IVPs). Ths dfference gves rse to sgnfcant varatons n the algorthms used to solve these problems - especally n the numercal soluton schemes. For BVPs one needs to satsfy condtons at up to n boundary ponts. For example, we mght have,, y p1 = k1 y p = k y pn = kn where p 1, p, up to p n are the boundary ponts for an n th order system. More often, however, there are only two boundary ponts, p 1 and p, wth multple condtons at each pont. If we defne n 1 and n as the number of condtons at ponts p 1 and p, respectvely, then n = n 1 + n for an n th order system. The boundary condtons (BCs) at the two ponts can be wrtten n a very general form as Bj( p 1,z) = 0 for j= 1,, n 1 (5.1) Bk( p,z) = 0 for k = 1,, n (5.) where ( n 1) z = y y' y'' y. T The most common stuaton that arses n practcal applcatons, of course, s the nd order BVP. In ths case, there s a sngle boundary specfcaton for each of the two boundary ponts. If the BCs are lnear n y and y, then they can be wrtten as B p,y,y' = ay p + by' p w 1 = 0 (5.3) 1 1 1 1 1 B p,y,y' = a y p + b y' p w = 0 (5.4) If both w 1 and w are zero, the boundary condtons (BCs) are sad to be homogeneous. Also f both a 1 and b 1, for example, are nonzero, then ths boundary condton s referred to as a mxed BC. Varous combnatons can occur and a varety of specfc examples are treated n ths secton of notes and throughout the remander of the course. Note: In the techncal lterature, the varous BCs are often referred to as Drchlet BC: B( y, y ') B(y) only a functon of y Neumann BC: B( y, y ') B(y ') only related to the dervatve of y Robn BC: B( y, y ') B(y, y ') functon of both y and y (mxed BC) Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 4 The Basc Algorthm Shootng Method for -Pont BVPs The basc dea behnd the Shootng Method s llustrated n Fg. 5.1. Tral ntegratons that satsfy the boundary condton at one endpont are launched. The dscrepances from the desred boundary condton at the other endpont are used to adjust the startng condtons, untl boundary condtons at both endponts are ultmately satsfed. Fg. 5.1 Schematc for the Shootng Method. Ths rather smple descrpton can be formalzed n the form of an algorthm as follows: 1. Gven an n th order ODE, convert t to n 1 st order equatons wth n BCs by lettng ( n 1) z = y y' y'' y T (5.5). Set up the problem as an IVP wthn a standard ODE solver. In addressng the ntal condtons for the IVP, let n 1 condtons be specfed at x 1 = p 1 (consdered as the ntal pont for ndependent varable x). Therefore, we have n unknown or free parameters at x 1, n = n n 1 Let α be a vector of free parameters (n n length). Therefore, at x 1 = p 1, z( p ) z( p, ) = α, 1 1 (5.6) whch smply says that the state at p 1 s really not known but t can be wrtten as some functon of the vector of free parameters. Our goal s to fnd α n an teratve fashon such that the n boundary condtons at pont x = p are satsfed. 3. Guess 0 0 α, whch gves z( p 1) = z( p 1, α ) the IVP can be solved n the usual way.. Now we have a full set of ntal condtons so that m m 4. Now solve the IVP usng your favorte ODE solver to get z( p, ) z p = α where m s an teraton counter (.e. m = 0 for the frst guess). Because we only have a guess for some of the ntal condtons, ths IVP problem wll not be equvalent to the desred BVP. In partcular, the n boundary condtons at pont p wll not be satsfed exactly. Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 5 5. To quantfy the error, defne a dscrepancy vector, m k k m m m ε k ε, whose elements,, satsfy ε = B p,z for k = 1,, n (5.7) where, for the exact soluton, z, we have B p,z = 0. That s, t satsfes the exact BCs. k 6. Fnally, check convergence aganst a user-specfed convergence crteron, tol. In partcular, f m k ε < tol for all k, then one termnates the teratve process and edts the results. If, however, m k ε > tol for any k, then we guess a new go back to Step 4 to perform another teraton. m+ 1 α (recall that m s smply an teraton counter) and The above scheme s actually very easy to mplement and t represents a very powerful method for solvng -pont BVPs wth only one ndependent varable. Pckng the Next α The Shootng Method s a numercal scheme for BVPs n whch one teratvely mproves upon n free parameters wthn the ntal condton vector for a standard IVP. The error condton s related to the dfference between the desred BCs at the second boundary pont, p, and the actual values computed usng a guess for part of the ntal condton vector. Gven an error vector for m m+ 1 teraton m, ε, our goal s to choose a free parameter vector, α, such that the error tends towards zero. Wth ths goal, let s choose where m+ 1 α as m+ 1 m m α =α + α (5.8) m α s the soluton to m m ε A α = ε wth A = aj = α j Note that the error vector on the rght hand sde of ths equaton s negatve so that computed such that t tends to reduce the error on teraton m, ths expresson can be wrtten as ε α m m ε = ε = α j j m ε m α s (5.9), to zero. In component form, j (5.10) In practce, the partal dervatves of the error terms wth respect to the free varables are computed by a fnte dfference approxmaton, or ε α j (,,,, ) (,,, ) ε α1 α α j+ αj ε α1 α αn = α j A smple algorthm for computng ε α j s as follows: (5.11) Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 6 1. Compute the error vector assocated wth the current choce for the free parameters, or m m α ε.. Perturb only one element, the j th element, α j, and ths leads to a new error vector, droppng the teraton ndex notaton for convenence, we have Ths gves ε εj ε = α α j j ε α j for all and a sngle j. 3. Repeat Step for all the elements of α (.e. let j vary from 1 to n elements). Ths procedure can be repeated for each step n the teraton process. Fnally, t should be noted that, n many practcal applcatons, the farly general procedure outlned here s really much easer than t may seem. For example, for the very common nd order BVP, there s only one free parameter and one error component. Thus the above development smplfes consderably. Because of ts mportance n applcaton, ths case s hghlghted n the next subsecton. Second Order BVPs m ε j. Then, (5.1) Second order systems occur so frequently that t makes sense to llustrate the Shootng Method for ths specfc case. In dscussng ths partcular problem, let s try to follow, at least roughly, the basc algorthm outlned prevously for the Shootng Method. Step 1. Recall that a nd order system can always be wrtten as a set of coupled 1 st order equatons, or d z f x,z wth z y y' [ dx = = ] wth lnear boundary condtons of the form a y x + b y' x w 1 1 1 1 1 =0 ayx + by'x w = 0 T (5.13) (5.14) (5.15) where x 1 and x are the boundary pont locatons [f the BCs are nonlnear, one smply uses the more general form gven n eqns. (5.1) and (5.)]. For ease of dscusson, let s specalze ths to a partcular stuaton where the functon, y(x), s known at both endponts. In ths case, the boundary condtons become (note that ths s a specfc example) y x = w and y x = w 1 1 Step. Now we wrte the system as an IVP wth an unknown ntal value, d y( x ) z f ( x,z, ) wth z ( x 1) y' ( x ) dx 1 w1 = α = = 1 α Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 7 where y'(x ) s to be determned so that the second boundary condton, y x = w, s satsfed. α= 1 Step 3. Now guess α 1 and solve the IVP for y( x,α 1), and guess α and solve the IVP agan for ( y x,α ). Wth two values of α and ther correspondng endpont results, we can compute the errors n the second BC, or ε 1 = y x, α1 w and ε = y x, α w And, wth these error estmates, an approxmaton to ε αcan be constructed, or ε ε ε1 α α α 1 Step 4. Note that the desred change n error s the negatve of the error n the prevous stage. Therefore, ε ε ε = α = ε and α = α ε α whch gves a new ncrement n the free varable. Now we can compute α 3 as α 3 =α + α and solve the IVP agan, gvng, etc., etc. ε 3 Ths basc procedure s contnued untl the user-specfed convergence crteron s met (.e. untl ε < tol ). In practce only a few steps are usually requred for good accuracy n lnear or m mldly nonlnear problems (3-5 steps or less n most cases). Example 5.1 and Example 5.3A llustrate the shootng method for two specfc nd order BVPs. The frst example mplements both a manual and automated teratve scheme wthn Matlab to show how the basc method works, and the second sample problem represents a practcal applcaton to the classcal crcular fn heat transfer problem. Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 8 Problem Descrpton: Example 5.1 - The Shootng Method for -Pont BVPs Solve the followng -pont BVP usng the Shootng Method: y'' + 3xy' + 7y= cos(x) wth y( 0) = 1 and y( π ) = 0 Problem Soluton: Case 1 Manual Interactve Iteraton 1. To start ths problem, let s convert to a set of coupled equatons. Lettng = [ y' ] T z' = z 1 z ' = 3xz 7z1+ cos(x) or, n general terms, ths becomes z' f( x,z) =. z y gves. Next we set ths up as an IVP and use an ODE solver n Matlab to numercally ntegrate ths system, wth the ntal condtons gven by z 0 y 0 1 = = y' 0 α 3. In ths nteractve example, the user s prompted for an nput value for α and, after the call to Matlab's ODE routne, we compute the error at the second boundary pont as ε= z 1 ( πα, ) y( π) 4. Step 3 s repeated untl the correct value of α s determned. That s, untl ε s small (.e. less than the desred tolerance). Ths manual nteractve soluton procedure for Case 1 has been mplemented wthn the man Matlab scrpt fle shootm1.m and functon fle shootf.m. These fles are lsted n Tables 5.1 and 5., respectvely. The partcular mplementaton gven here s very smple and t assumes that the user wll do some auxlary computatons to help decde the next value of α to choose. Followng the frst two arbtrary guesses, the next guess should be made usng the gudelnes n the prevous secton. A typcal nteractve sesson wth shootm1.m has been saved wth Matlab s dary feature and later ths fle was annotated wth the nformaton used to compute the thrd (and last) value of α. A lstng of the annotated dary fle s gven n Table 5.3. Note here that very good convergence was acheved wth only three values of α. In addton to the sesson dary, the computed solutons, y (x), for the three dfferent values of α used n ths llustraton are plotted n Fg 5.. Ths set of curves was generated n a separate m-fle called shootm.m. Ths fle s gven n Table 5.4. The value of the ntal y that gves the correct soluton (.e. properly satsfes the second BC) s α = y (0) = -5.4715. The profles Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 9 for the frst two arbtrary guesses ( α= ±1) are substantally dfferent from the correct profle and, n partcular, they gve the wrong endpont value at x = π. Note that the endpont values are all qute smlar, so a relatvely tght convergence s needed. Notce, however, that t s relatvely easy to converge on the proper value of the sngle free varable. Fg. 5. Some tral solutons for Case 1 from Example 5.1. Tables 5.1-5.4 summarze the basc procedures assocated wth the Shootng Method, and the student should study these fles as needed to get a good understandng of ths method. The nteractve approach s gven here as a good mechansm for understandng the overall teratve scheme. In practcal applcatons, however, ths procedure should be automated to elmnate the manual computaton of α (and the possblty of error ntroduced va the hand calculatons). An automated procedure, as llustrated below for Case, s smply easer and more effcent for routne applcaton. Table 5.1 Lstng of the Matlab man program shootm1.m. SHOOTM1.M Example of Shootng Method for Solvng -Pont BVPs (Manual Iteratve Procedure) Ths demo solves a partcular nd order ODE usng an teratve form of the shootng method. The varable ALF s chosen nteractvely. Ths s dscussed as Example 5.1 Case 1 Manual Interactve Iteraton n the course notes for Math Methods (10/4.539). The goal s to fnd the value of the free parameter, ALF = y'(0) whch, upon soluton of the IVP, satsfes the desred boundary condton at x = p. also see SHOOTF.M - contans descrpton of gven dfferental equaton y'' + 3xy' + 7y = cos(x) wth y(0) = 1 and y(p) = 0 also see related fles: SHOOTM.M - shows multple curves of y(x) for dfferent ALF SHOOTA.M - solves same problem usng an automated teratve scheme Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 10 BVPSH.M - mplements Shootng Method for nd order BVP wth lnear BCs Fles prepared by J. R. Whte, UMass-Lowell (Aug. 003) gettng started clear all, close all set doman lmts and tolerance for ODE3 xo = 0; xf = p; tol = 1e-6; specfy desred BCs yxo = 1.0; yxf = 0; set format for edt to long words (more sgnfcant dgts) format long guess at ntal condtons (note that ALF s defned manually) ALF = nput('input ntal guess for free parameter (ALF) = ') start loopng (for varous values of ALF) optons = odeset('reltol',tol); whle ALF ~= 0 zo = [yxo ALF]'; call ODE3 [x,z] = ode3('shootf',[xo xf],zo,optons); plot results for vsual nspecton of soluton at each teraton plot(x,z(:,1),'lnewdth',),ttle('shootm1: Shootng Method for ODEs') xlabel('x values'),ylabel('y values'),grd edt error n BC at second boundary pont (x = xf) [nr,nc] = sze(z); eps = z(nr,1)-yxf; dsp(' ') dsp(' Error n nd BC'), eps ALF = nput('enter another guess for ALF (zero to qut)? ') end dsp('manual teratve procedure termnated by user...') end demo Table 5. Lstng of the ode3 functon program shootf.m. SHOOTF.M Functon fle for SHOOTM1.M, SHOOTM.M, and SHOOTA.M (used by ODE3) Gven dfferental eqn.: y'' + 3xy' + 7y = cos(x) Fle prepared by J. R. Whte, UMass-Lowell (Aug. 003) functon zp = odefle(x,z) zp = zeros(length(z),1); zp(1) = z(); zp() = -3*x*z() - 7*z(1) + cos(*x); end of functon Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 11 Table 5.3 Matlab dary fle from shootm1.m. Ths lstng llustrates the teratve process for the Shootng Method. The Matlab output has been edted n MS Word and the computaton of the thrd guess for α = ALF has been added.» shootm1 Input ntal guess for free parameter (ALF) = 1 (ths s α 1) Error n nd BC eps = -0.0347939571931 (ths s ε 1 ) Enter another guess for ALF (zero to qut)? ALF = -1 (ths s α ) Error n nd BC eps = -0.040408044781 (ths s ε ) --------------- Computaton for new choce of ALF (done outsde Matlab) Therefore, ε α ε ε α α 1 = = 1 5.3765e 3 ε.4041e 1 5.4715 ε α 5.3765e 3 α 3 =α = = --------------- Enter another guess for ALF (zero to qut)? ALF = -5.47150000000000 (ths s α 3 ) Error n nd BC eps = 4.088148677989e-007 (ths s ε 3 ) Enter another guess for ALF (zero to qut)? ALF = 0 (eps was small enough, so qut) Manual teratve procedure termnated by user... Table 5.4 Lstng of Matlab fle shootm.m. SHOOTM.M Example of Shootng Method for Solvng ODEs Ths s a follow up to demo SHOOTM1.M. Ths fle smply generates the plots obtaned for each of the guesses for the ALF used n SHOOTM1.M The goal was to fnd the ALF at x = 0 whch gves y(p) = 0. also see SHOOTF.M - contans descrpton of gven dfferental equaton y'' + 3xy' + 7y = cos(x) wth y(0) = 1 and y(p) = 0 Fle prepared by J. R. Whte, UMass-Lowell (Aug. 003) gettng started clear all, close all, nfg = 0; wth manual procedure, ALFs of 1, -1, and -5.4713 were used n SHOOTM1.M let's plot the functon for these dfferent guesses to see how ths works Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 1 gs = [1-1 -5.4715]; set doman lmts and solve IVP xo = 0; xf = p; tol = 1.0e-6; yxo = 1.0; optons = odeset('reltol',tol); [x1,z1] = ode3('shootf',[xo xf],[yxo gs(1)],optons); [x,z] = ode3('shootf',[xo xf],[yxo gs()],optons); [x3,z3] = ode3('shootf',[xo xf],[yxo gs(3)],optons); now plot results nfg = nfg+1; fgure(nfg) plot(x1,z1(:,1),'g-',x,z(:,1),'r-.',x3,z3(:,1),'b--','lnewdth',) ttle('shootm: Shootng Method for ODEs (Case 1 - Manual Iteraton)') xlabel('x values'),ylabel('y values'),grd legend('alf = 1','alf = -1','alf = -5.4715') end of demo Case Automated Iteraton The Shootng Method for a nd order BVP has been automated wthn Matlab fle bvpsh.m. Ths fle s lsted n Table 5.5 and, as apparent from the embedded comments, t s currently lmted to the case where lnear BCs of the form gven n eqns. (5.14) and (5.15) are specfed. Ths restrcton was made only because t was easy to generalze the codng for BCs n ths form. Note that, although t would be more dffcult to allow general nonlnear BCs, the exstng fle could easly be modfed for a partcular problem. The comments at the begnnng of bvpsh.m show how to use ths routne. In partcular, the user needs to supply nformaton about the lnear BCs for the problem of nterest wthn the zbc array, as follows: a1 b1 w1 zbc = a b w where the ndvdual coeffcents, a 1, b 1, etc. are defned n eqns. (5.14) and (5.15). Ths array replaces the usual IC array that s used wth Matlab s bult-n ode3 or ode45 IVP solvers. At the ntal boundary pont, x 1, a check s made wthn bvpsh.m to determne the knd of BC that s mposed and, from ths, t can determne the form of the IC vector for the teratve IVP soluton. In partcular, the three standard BCs can be represented as follows: Drchlet BC: f b 1 = 0, y(x 1 ) = w 1 /a 1 then z o w1 a1 = α α Neumann BC: f a 1 = 0, y'(x 1 ) = w 1 /b 1 then zo = w1 b 1 Robn (mxed) BC: f both a 1 and b 1 are nonzero, then z o α = (w1 a 1 ) b α 1 where α s the unknown parameter at the ntal pont, x 1, whch wll be computed as part of the teratve soluton. Note that the Neumann BC s just a subset of the Robn BC wth a 1 = 0, and t s mplemented as such wthn bvpsh.m. Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 13 The value of α s determned as part of the overall soluton such that the BC at x s satsfed to wthn some small tolerance. An arbtrary guess for α ntates the teratve procedure, gvng a frst estmate of the soluton wth partcular focus on the relatve error n the second boundary condton. If the relatve error at the second boundary pont s greater than a user-specfed tolerance, a 1 change s made n the current value of α, a new IVP soluton s obtaned, and a fnte dfference estmate for ε α s computed. Usng ths dervatve estmate, a new value of α s chosen wth the goal of reducng the relatve error to zero. Wth the new α, a new teraton s ntated, wth the process contnung untl the preset convergence crteron s met or the maxmum number of teratons s reached. Ths automated procedure was used wthn Matlab fle shoota.m (see Table 5.6) to solve the same problem as for Case 1. Ths fle calls bvpsh.m for general mplementaton of the Shootng Method for problems wth lnear BCs. The startng pont here s exactly the same as for Case 1. In partcular, we set the problem up as an IVP usng standard state space notaton (as n Steps 1 and from Case 1) and the same ODE functon fle, shootf.m, s used. In shoota.m, however, the bvpsh.m routne handles all the detals of the method. In ths case, the requred zbc matrx s smply zbc = [1 0 1; 1 0 0], and the rest of the problem s then no more dffcult to solve than a standard IVP that uses Matlab s bult-n ode3 routne. The fnal converged soluton from shoota.m s gven n Fg. 5.3 and the soluton s dentcal to the converged soluton from Case 1 usng the manual approach to the Shootng Method. Obvously, the automated methodology bult nto the bvpsh.m routne s the easer of the two approaches, and the automated technque llustrated here should prove to be useful for solvng a varety of BVPs of ths type. Fnally, we note that another example, Example 5.3A, has been provded at the end of ths secton of notes to further llustrate the use of the bvpsh.m functon. Ths problem deals wth conducton heat transfer n a crcular fn arrangement and t represents a more practcal applcaton of the basc methodology. Fg. 5.3 The converged soluton for Case from Example 5.1. Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 14 Table 5.5 Lstng of Matlab fle bvpsh.m. BVPSH.M Ths functon fle solves a nd order BVP wth lnear BCs va the Shootng Method Ths method solves the BVP usng an IVP solver wth an ntal guess for the unknown ntal condtons. An teratve scheme automatcally modfes ths guess untl the boundary condtons at the second boundary pont are satsfed. Ths teratve technque s called the Shootng Method. A general nd order ODE can be wrtten n the form y'' + a*y' + b*y = f(t) where, n general, a and b can be functons of y and t. We can also wrte ths as a set of two 1st order equatons n so-called state space form as dz1/dt = z and dz/dt = f - a*z - b*z1 where z1 = y and z = y' Now, f we had a set of ntal condtons, zo = [y(to); y'(to)], then ths state space system could be easly solved as an IVP. However, snce ths s a BVP, we do not have a complete set of ntal condtons. Thus, the basc dea of the shootng method s to guess at the unknown varable at the ntal state so that the soluton of the resultng IVP gves the desred fnal state for the BVP. Ths s done teratvely by convergng on the free parameter, alf, that s ncluded as part of the ntal condton vector such that the desred fnal state s acheved. For the case of a nd order -pont BVP wth lnear BCs, a general way to wrte the boundary condtons s as follows: at p1: a1*y(p1) + b1*y'(p1) - w1 = 0 at p: a*y(p) + b*y'(p) - w = 0 The coeffcents n these expressons are nput to ths functon n the zbc matrx as: zbc = [a1 b1 w1; a b w]. Ths format essentally defnes the BCs for the nd order BVP wth lnear BCs (note that the equaton can be nonlnear -- but not the BCs). At the ntal boundary pont, p1, a check s made to determne the knd of boundary condton and thus the form of the ntal condton vector for the teratve IVP soluton: Drchlet BC: f b1 = 0, y(p1) = w1/a1 then zo = [w1/a1; alf] (guess ntal slope) Neumann BC: f a1 = 0, y'(p1) = w1/b1 then zo = [alf; w1/b1] (guess ntal value) Robn (mxed) BC: f both a1 and b1 are nonzero, then zo = [alf; (w1 - a1*alf)/b1] (guess ntal value) where alf s the unknown parameter whch wll be computed as part of the teratve soluton. alf s determned such that the second BC s satsfed to wthn some small tolerance. Note that the Neumann BC s just a subset of the Robn BC wth a1 = 0, and t s mplemented as such n ths routne. The nputs to ths functon are smlar to those used wth Matlab's ode3 IVP solver (n ts smplest mplementaton) except that the zbc matrx replaces the ntal state vector. Wth zbc known, the zo vector s computed nternally as descrbed above. Also, there s an opton to nput a guess for alf, f desred func - name of functon fle that evaluates the RHS of the state equatons tspan - vector that defnes the range for the ndependent varable zbc - matrx contanng the BC coeffcents (as descrbed above) optons - standard optons structure for use n ode3 (optonal) alfo - ntal guess for alf (optonal) The outputs are: t - column vector contanng the ndependent varable z - soluton matrx wth two columns wth column 1 contanng z1(t) and column contanng z(t) Note #1: Extra parameters that may be needed n the ode functon fle must be passed to the functon fle va a global statement n the man program and the functon fle. The current, relatvely smple mplementaton of the Shootng Method does not use the varable nput arguments (varargn) that are Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 15 often used n the ode3 routne. Ths s not overly restrctve, however, snce t s just as easy to pass needed varables nto the functon va the global statement. Note #: Ths routne assumes that you are ntegratng from boundary pont p1 to boundary pont p. Ths normally mples that p > p1, but ths s not absolutely essental. In some cases, t may be approprate to ntegrate 'backwards' -- just be careful wth the orderng of the BCs f you try ths... Note #3: Nonlnear problems are sometmes senstve to the ntal guess for the free varable, alf, n the ntal condton vector. Here we have smply set alfo = 1 as default. The user can overrde ths as needed n partcularly dffcult problems. A poor ntal guess usually just means that a few more teratons may be needed. If the system does not converge wthn 10-0 teratons, you may want to change alfo va the last entry n the nput argument lst to see f ths helps. Ths s not usually necessary, however... Fle prepared by J. R. Whte, UMass-Lowell (March 003) functon [t,z] = bvpsh(func,tspan,zbc,optons,alfo) check to see f the optons structure s avalable f nargn < 4, optons = []; end check to see f there s an ntal guess for alfo f nargn < 5, alfo = 1; end extract BC coeffcents (for convenence) a1 = zbc(1,1); b1 = zbc(1,); w1 = zbc(1,3); a = zbc(,1); b = zbc(,); w = zbc(,3); set some other teratve parameters err = 1e10; tol = 1e-6; cnt = 1; cntmax = 5; start teraton whle abs(err) > tol & cnt <= cntmax determne ntal condton vector based on structure of zbc matrx f b1 == 0 zo = [w1/a1; alfo]; else zo = [alfo; (w1 - a1*alfo)/b1]; end solve IVP wth current guess for ntal condtons [t,z] = ode3(func,tspan,zo,optons); evaluate error n second BC e1 = a*z(end,1) + b*z(end,) - w; err = e1; fprntf(1,' \n') fprntf(1,'for teraton # : \n',cnt) fprntf(1,' Intal condtons are: 13.5e 13.5e \n',zo) fprntf(1,' Error n nd BC s: 13.5e \n',err) fprntf(1,' \n') estmate new values for alf (f necessary) f abs(err) > tol alfp = 1.01*alfo; f b1 == 0 zo = [w1/a1; alfp]; else zo = [alfp; (w1 - a1*alfp)/b1]; end [t,z] = ode3(func,tspan,zo,optons); e = a*z(end,1) + b*z(end,) - w; deda = (e-e1)/(0.01*alfo); alfn = alfo-e1/deda; cnt = cnt+1; alfo = alfn; end end end of teraton loop f cnt >= cntmax fprntf(1,' WARNING -- Ht maxmum teraton lmt!!! \n'); end end of functon Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 16 Table 5.6 Lstng of Matlab fle shoota.m. SHOOTA.M Example of Shootng Method for Solvng -Pont BVPs (Automated Iteratve Procedure) Ths demo solves a partcular nd order ODE usng an teratve form of the shootng method. The varable ALF s chosen automatcally as part of the teratve procedure. Ths s dscussed as Example 5.1 Case Automated Iteraton n the course notes for Math Methods (10/4.539). The goal s to fnd the value of the free parameter, ALF = y'(0) whch, upon soluton of the IVP, satsfes the desred boundary condton at x = p. The automated algortthm for the Shootng Method s mlemented wthn the BVPSH.M fle. also see SHOOTF.M - contans descrpton of gven dfferental equaton y'' + 3xy' + 7y = cos(x) wth y(0) = 1 and y(p) = 0 also see related fles: SHOOTM1.M - solves same problem usng a manual teratve scheme SHOOTM.M - shows multple curves of y(x) for dfferent ALF (manual case) Fles prepared by J. R. Whte, UMass-Lowell (Aug. 003) gettng started clear all, close all set doman lmts xo = 0; xf = p; set coeffcents for BCs zbc = [1 0 1.0; left BC --> y(xo) = 1.0 1 0 0.0]; rght BC --> y(xf) = 0.0 soluton va Shootng Method (numercal approx) tol = 1e-6; optons = odeset('reltol',tol); set tght tolerance for ODE soln [xs,zs] = bvpsh('shootf',[xo xf],zbc,optons); plot fnal functon values plot(xs,zs(:,1),'lnewdth',),grd ttle('shoota: Shootng Method for ODEs (Case - Automated Iteraton)') xlabel('x values'),ylabel('y values') end demo Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 17 Basc Concepts The Fnte Dfference Method The Fnte Dfference (FD) method essentally converts the ODE nto a coupled set of algebrac equatons, wth one balance equaton for each fnte volume or node n the system. The general technque s to replace the contnuous dervatves wthn the ODE wth fnte dfference approxmatons on a grd of mesh ponts that spans the doman of nterest. The boundary nodes are treated as specal cases. The coupled set of equatons that results can then be solved by a varety of technques (see Secton VI - Numercal Soluton of Algebrac Equatons). A smple algorthm for the basc FD method (for lnear systems) s outlned as follows: 1. Convert contnuous varables to dscrete varables. Note that the mesh grd or nodal scheme for the ndependent varable may be evenly spaced (usually gves a relatvely smple formulaton) or set on an uneven grd (resultant equatons are usually slghtly more complex), as necessary.. Approxmate the dervatves at each pont usng formulae derved from a formal Taylor Seres expanson usng the most accurate approxmaton avalable that s consstent wth the gven problem. 3. Treat the boundary ponts separately, beng careful to get the best approxmaton as possble to the desred BCs. 4. Solve the resultant set of coupled equatons usng ether drect or teratve schemes as approprate for the problem. If the orgnal ODE s nonlnear the above procedure s stll vald, except an addtonal outer teraton s needed n the overall soluton strategy. Hghly nonlnear problems are often dffcult to converge, but mldly nonlnear or varable coeffcent problems are usually solvable usng a smple outer teraton scheme. Ths secton of notes frst revews the basc Taylor Seres and the development of several dervatve approxmatons, and then llustrates the FD method va an example. Dervatve Approxmatons Usng Taylor Seres A key step n the Fnte Dfference method s to replace the contnuous dervatves n the orgnal ODE wth approprate approxmatons n terms of the dependent varable evaluated at dfferent mesh ponts n the regon of nterest. The bass for dong ths s related to the Taylor Seres expanson for a functon n the vcnty of some dscrete pont, x. To formalze ths dscusson, let s start by defnng a dscrete notaton for both the ndependent and dependent varables. For the ndependent varable we smply evaluate the contnuous varable x at dscrete ponts x. If the doman of nterest s broken nto evenly spaced nodes, then x = x + h and x = x h where h = x (5.16) + 1 1 For the dependent varable, f(x), the dscrete representaton becomes f x f, f x + h f, f x h f, etc. (5.17) + 1 1 Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 18 Then, from the Taylor Seres we have 3 f''h f'''h + 1= + + + + (5.18) f f f 'h! 3! 3 f '' h f ''' h 1= + + (5.19) f f f 'h! 3! These two equatons can be used to derve several common dervatve approxmatons, as follows: Frst Dervatve Approxmatons Forward Dfference Approxmaton - use eqn. (5.18) drectly to gve f' f+ 1 f = + Oh (5.0) h Backward Dfference Approxmaton - use eqn. (5.19) drectly to gve f' f f h 1 = + Oh (5.1) Central Dfference Approxmaton - subtract eqn. (5.19) from eqn. (5.18) to gve 3 f'''h f+ 1 f 1 f 'h 3! = + + f or + 1 f f' 1 ( = + Oh) (5.) h where we notce that the central dervatve formula s more accurate, wth truncaton errors on the order of h [whch s denoted as O(h )]. Second Dervatve Approxmatons Central Dfference Approxmaton - add eqn. (5.19) to eqn. (5.18) gvng (4) 4 f h f+ 1 f 1 f f ''h 4! + = + + + f or 1 f + f + f'' 1 ( = + Oh ) (5.3) h These frst and second dervatve approxmatons are the most common representatons used when convertng the contnuous ODE nto FD form. There are other formulae that are sometmes used (see the next subsecton), especally when nd order accuracy at the end ponts s desred. Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 19 Some Addtonal Dervatve Approxmatons Order x Approxmatons for Frst and Second Dervatves Forward Dfference Approxmatons: f' x f'' x f x = f ( x ) + 1 x + f x f x f x + 1 + = x Backward Dfference Approxmatons: f' x f'' x f x = f ( x ) x 1 + f x f x f x 1 = x (5.4) (5.5) (5.6) (5.7) Order Forward Dfference Approxmatons: f' x f'' x x Approxmatons for Frst and Second Dervatves 3f x + 4f x f x = x + 1 + + f x 5f x 4f x f x + 1 + = x Backward Dfference Approxmatons: f' x f'' x + 3f x 4f x f x = x 1 + 3 + f x 5f x 4f x f x 1 3 = x Central Dfference Approxmatons: f' x f'' x f x = f ( x ) + 1 1 x f x + f x + f x x 1 1 = The above dervatve approxmatons [eqns. (5.0) - (5.33)] represent a good nventory of formulae for use n the FD method. Many other formulae are also avalable, snce there are a varety of schemes and assumptons that can be used n the development of such formulae. (5.8) (5.9) (5.30) (5.31) (5.3) (5.33) As an example, let s derve the forward approxmaton for f wth error O(h ) as gven above n eqn. (5.8). We start wth Taylor Seres expansons for f +1 and f +, whch can be wrtten as Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 0 3 h h f+ 1= f + f ' h + f '' + f ''' + (5.34) 6 3 4h 8h + f = f+ f'h + f'' + f''' + (5.35) 6 Now let s elmnate the second dervatve term by multplyng eqn. (5.34) by 4 and subtractng eqn. (5.35) from the result, gvng 4 3 4f+ 1 f+ = 3f + f ' h f ''' h + (5.36) 6 Solvng ths expresson for f gves f' 3f + 4f f h + 1 + = + Oh whch s the same result as gven above. Ths s only one example - however, many specal purpose formulae can be derved as needed followng the general scheme llustrated here. Example 5. llustrates the use of some of these dervatve formulae as appled to the same - pont BVP as gven n Example 5.1 (wth soluton based on the Shootng Method). Ths FD example also emphaszes the mportance of usng the best dervatve approxmaton avalable -- for most applcatons one should use the dervatve formulae that are accurate to second order (rather than a frst order approxmaton). A practcal problem that nvolves heat transfer n a crcular fn geometry s also gven n Example 5.3B. Ths problem s smply another example of the use of the Fnte Dfference method for lnear BVPs (see Example 5.3A for soluton of the same problem va the Shootng Method). Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 1 Problem Descrpton: Example 5. - The Fnte Dfference Method (Example 5.1 Revsted) Solve the followng -pont BVP usng the FD method: y'' + 3xy' + 7y= cos(x) wth y( 0) = 1 and y( π ) = 0 Problem Soluton: 1. Let s start by defnng the dscrete geometry of nterest and by dscretzng the dependent and ndependent varables usng a unform mesh grd, x x and x = x + x + 1 y(x) y x = y, y'(x) y' x = y', etc. Here we want to be sure to start numberng the nodal ponts at the frst unknown value, as llustrated n the sketch. Note that, n ths case, snce both endpont values, y(p 1 ) and y(p ), are known, the number of unknowns s one less than the number of mesh ntervals. If we let N be the number of unknowns n the problem (N = 4 n the sketch), then the spatal ncrement can be determned by xf x h = x = N+ 1 o Also, for ths case, we can easly set the vector of ndependent varables usng Matlab s colon operator as x = xo+dx:dx:xf-dx where, of course, dx would be the Matlab varable for x, xo means x o, etc... Wth a dscrete geometry and dscretzed varables, we then replace the dervatves at dscrete ponts wth fnte dfference approxmatons (from the Taylor Seres). Here one should always use the most accurate representaton that s approprate for the problem at hand. Ths pont s llustrated below by comparng two dfferent approxmatons for y (Case A uses a hgh order approxmaton and Case B uses a low order representaton). 3. Fnally, we put the equatons for each node or mesh n matrx form and solve usng some lnear equaton solver (drect or teratve method). In the examples below we use Matlab s standard equaton solver (va the backslash operator), whch s an mplementaton of an LU Decomposton method (a drect elmnaton scheme). Case A -- Hgh Order Approxmaton For ths case let s use the central FD approxmatons n Step for both y and y, or y y + y y y y'' = and y' = x x 1 + 1 + 1 1 Therefore, substtutng these nto the dscrete form of the orgnal ODE, we have Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) y'' + 3xy' + 7y = cosx y 1 y+ y+ 1 y+ 1 y 1 + 3x + 7y = cos x x x Rearrangng the expresson, one has the followng relatonshp for each nteror node n the doman of nterest: ( x ) 3x x 3x x 1 y 1+ + 7 x y+ 1+ y+ 1= x cos Therefore, for the general nternal node (row corresponds to the balance equaton for the th node), we have 3x x ( ) = A, 1 1 = + = A, 7 x and b() x cos x 3x x ( + ) = + A, 1 1 where the A(, j) and b() notaton s consstent wth the matrx representaton used wthn the Matlab code. For the boundary nodes, we have to ncorporate specfc nformaton about the boundary condtons for the problem. Ths example s partcularly straghtforward snce the functon values at both endponts are specfed. Ths says that y 0 and y N+1 are known quanttes, where N represents the number of dscrete unknowns n the problem. Thus, the balance equaton for the frst and last nodal volumes can be wrtten as follows: Left Boundary: For = 1, y = y = yxo (n the code). Therefore, the balance equaton s wrtten as -1 0 1 ( 1) Rght Boundary: For = N, 3x x 3x x + + + = 1 1 7 x y 1 y x cos x 1 yxo y = y = yxf (n code). Therefore, the balance equaton becomes + 1 N+ 1 3x N x 3x N x 1 yn 1+ + 7 x yn= x cos xn 1+ yxf Now, for the specfc case of N = 4, we can wrte out the resultant four explct equatons, as follows: 3x1 x 3x1 x + 7 x y1+ 1+ y = x cos x1 1 yxo for = 1: Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 3 3x x 3x x for = : 1 y + ( + 7 x ) y + 1+ y = x cos( x ) 1 3 3x x 3x x for = 3: 1 3 y + ( + 7 x ) y + 1+ 3 y = x cos( x ) 3 4 3x x 3x x + + = + for = 4: 4 4 1 y3 7 x y4 x cos x4 1 yxf Fnally, these four coupled equatons wth four unknowns (or for the general case, N equatons and N unknowns) can be put nto matrx form and solved n Matlab. The fnal form of the equatons s Ay = b where y s the desred soluton vector. The Matlab fle fd.m mplements ths example and t also looks at the senstvty of the soluton to the number of mesh used (.e. mesh sze, x ). It also has a comparson to Case B, whch only uses a forward approxmaton to y. Case B -- Low Order Approxmaton Just to llustrate the strong dependence on the accuracy of the dervatve approxmatons used, we redevelop the above formulaton usng the forward dfference approxmaton for y whch s accurate to only frst order (nstead of the central approxmaton whch s accurate to second order). Thus, the dscrete representaton for y for ths case s y y' = y x + 1 Usng ths relaton, the dscrete form of the orgnal ODE becomes y-1 y+ y+ 1 y+ 1 y + 3x + 7y = cos x x x or ( + ) + ( + ) = ( y 3x x 7 x y 1 3x x y x cos x -1 + 1 Also for the frst node, = 1, we have ( 1 ) 1 ( 1 ) ( 1) + 3x x 7 x y + 1+ 3x x y = x cos x yxo and for the last node, = N, the balance equaton becomes ( N ) ( N) yn 1 + 3x x 7 x yn = x cos x 1 + 3x N x yxf These equatons are also mplemented nto Matlab fle fd.m and drect comparsons wth Case A are made. ) 3 Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 4 The Matlab Fles The above two FD formulatons for ths problem are mplemented nto Matlab fle fd.m. Ths fle s set up to loop over four dfferent values of N to evaluate the senstvty to mesh sze. For each N, both Cases A and B are solved, and the resultant soluton profles, y(x), are compared. The results for each value of N are plotted wthn one of four subplots. When complete, these four subplots represent a complete summary of the data for ths problem and they are dsplayed n a sngle Matlab plot n Fg 5.4. Fg. 5.4 Soluton profles for Case A and Case B for varous mesh structures. From the above fgure t s easy to see that the Case A and Case B results dffer, but they get progressvely closer as the number of mesh ntervals s ncreased ( x s decreased). Also t should be clear that Case A has essentally found the correct soluton wth about 40 mesh ponts, and that Case B s stll not exact wth 160 ponts. Ths should llustrate ncely the fact that the best approxmatons should always be used. In ths comparson, y has an approxmaton wth error O(h ) for Case A and O(h) for Case B, and clearly ths has a sgnfcant effect on the accuracy n the FD calculaton for a fxed mesh sze. The Matlab fles assocated wth ths example are gven n Tables 5.7 and 5.8. The base fd.m program s gven n Table 5.7 and the student s encouraged to study ths as an example of how to mplement the Fnte Dfference (FD) method wthn a Matlab scrpt fle. Also note that, n a typcal applcaton, only Case A would be mplemented and the loop over several dfferent values of N can be elmnated. Thus, wth only a few modfcatons, fd.m can be streamlned nto a very short and easy-to-follow example of the FD method n Matlab. Ths edtng has been done and Table 5.8 contans the resultant code wthn fle fds.m. The fnal soluton from fds.m for Case A wth 80 mesh ponts s plotted n Fg. 5.5, and ths s clearly the same soluton that Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 5 was obtaned va the Shootng Method (see Fg. 5.3 n the prevous subsecton). Ether Matlab fle, fd.m or fds.m, can be used as a typcal example of the Fnte Dfference Method appled to a second order non-homogeneous lnear BVP. The student s also referred to Example 5.3B whch represents another example of the fnte dfference method to lnear BVPs. Ths problem deals wth conducton heat transfer n a crcular fn arrangement. Fg. 5.5 Fnal soluton profle from fds.m for Case A wth 80 mesh. Table 5.7 Lstng of Matlab program fd.m. FD.M Example of Fnte Dfference Method for Solvng ODEs Ths demo uses the fnte dfference technque as the soluton method for a partcular ODE. The mesh nterval s constant. Two dfferent approxmatons for the frst dervatve are used. Ths comparson demonstrates that the best approxmaton for the dervatve terms leads to a more effcent numercal scheme. One should always use the best approxmaton avalable for the gven problem! The senstvty to mesh sze s also addressed by loopng over four dfferent values of N (number of unknowns n the problem). Descrpton of gven dfferental equaton y'' + 3xy' + 7y = cos(x) wth y(0) = 1 and y(p) = 0 A related Matlab fle, FDS.M, s also avalable (ths contans a smple verson of ths example - Case A only - wth no mesh senstvty study). Fle prepared by J. R. Whte, UMass-Lowell (Aug. 003). gettng started clear all, close all, nfg = 0; set doman lmts and boundary condtons Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 6 xo = 0; xf = p; yxo = 1; yxf = 0; loop over four values of N to evaluate senstvty to mesh sze NN = [0 40 80 160]; for n = 1:4 N = NN(n); compute nterval sze and dscrete x vector dx = (xf-xo)/(n+1); dx = dx*dx; x = (xo+dx):dx:(xf-dx); Approxmatons: y'' = [y(-1) - y() + y(+1)]/(dx^) error => (dx^) (Case A) y' = [y(+1) - y(-1)]/(dx) error => (dx^) setup matrx eqns. (treat boundary terms as specal cases) a = zeros(n,n); b = zeros(n,1); for = :N-1 a(,-1) = 1-3*x()*dx/; a(,) = -+7*dx; a(,+1) = 1+3*x()*dx/; b() = dx*cos(*x()); end left boundary a(1,1) = -+7*dx; a(1,) = 1+3*x(1)*dx/; b(1) = dx*cos(*x(1))-(1-3*x(1)*dx/)*yxo; rght boundary a(n,n-1) = 1-3*x(N)*dx/; a(n,n) = -+7*dx; b(n) = dx*cos(*x(n))-(1+3*x(n)*dx/)*yxf; solve system of eqns y = a\b; add boundary ponts to soluton for plottng za = [yxo y' yxf]; xa = [xo x xf]; Approxmatons: y'' = [y(-1) - y() + y(+1)]/(dx^) error => (dx^) (Case B) y' = [y(+1) - y()]/(dx) error => (dx) setup matrx eqns. (treat boundary terms as specal cases) a = zeros(n,n); b = zeros(n,1); for = :N-1 a(,-1) = 1; a(,) = -(+3*x()*dx-7*dx); a(,+1) = 1+3*x()*dx; b() = dx*cos(*x()); end left boundary a(1,1) = -(+3*x(1)*dx-7*dx); a(1,) = 1+3*x(1)*dx; b(1) = dx*cos(*x(1))-yxo; rght boundary a(n,n-1) = 1; a(n,n) = -(+3*x(N)*dx-7*dx); b(n) = dx*cos(*x(n))-(1+3*x(n)*dx)*yxf; solve system of eqns y = a\b; add boundary ponts to soluton for plottng zb = [yxo y' yxf]; xb = [xo x xf]; plot results for both cases t = '0+n'; subplot(eval(t)),plot(xa,za,'r-',xb,zb,'g--','lnewdth',) axs([0 3. - 1]); ttle(['fd: FD Method (',numstr(n),' pts)']) xlabel('x values'),ylabel('y values'),grd legend('case A','Case B') end end of demo Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)

Math Methods -- Secton V: General Boundary Value Problems (BVPs) 7 Table 5.8 Lstng of Matlab program fds.m. FDS.M Example of Fnte Dfference Method for Solvng ODEs Ths demo uses the fnte dfference technque as the soluton method for a partcular ODE. The mesh nterval s constant. Descrpton of gven dfferental equaton y'' + 3xy' + 7y = cos(x) wth y(0) = 1 and y(p) = 0 A related Matlab fle, FD.M, s also avalable (ths contans a more detaled verson of ths example - compares two dfferent approxmatons for y' and performs a mesh senstvty study). Fle prepared by J. R. Whte, UMass-Lowell (Aug. 003). gettng started clear all, close all, nfg = 0; set doman lmts and boundary condtons xo = 0; xf = p; yxo = 1; yxf = 0; user defned number of unknowns n problem N = nput('input number of unknowns n problem (N): '); compute nterval sze and dscrete x vector dx = (xf-xo)/(n+1); dx = dx*dx; x = (xo+dx):dx:(xf-dx); Approxmatons: y'' = [y(-1) - y() + y(+1)]/(dx^) error => (dx^) (Case A) y' = [y(+1) - y(-1)]/(dx) error => (dx^) setup matrx eqns. (treat boundary terms as specal cases) a = zeros(n,n); b = zeros(n,1); for = :N-1 a(,-1) = 1-3*x()*dx/; a(,) = -+7*dx; a(,+1) = 1+3*x()*dx/; b() = dx*cos(*x()); end left boundary a(1,1) = -+7*dx; a(1,) = 1+3*x(1)*dx/; b(1) = dx*cos(*x(1))-(1-3*x(1)*dx/)*yxo; rght boundary a(n,n-1) = 1-3*x(N)*dx/; a(n,n) = -+7*dx; b(n) = dx*cos(*x(n))-(1+3*x(n)*dx/)*yxf; solve system of eqns y = a\b; add boundary ponts to soluton for plottng za = [yxo y' yxf]; xa = [xo x xf]; plot results nfg = nfg+1; fgure(nfg) plot(xa,za,'r-','lnewdth',) axs([0 3. - 1]); ttle(['fds: FD Method for Case A (',numstr(n),' pts)']) xlabel('x values'),ylabel('y values'),grd end of demo Lecture Notes for Math Methods by Dr. John R. Whte, UMass-Lowell (updated Aug. 003)