You are on page 1of 64

NumericalSolutionsof

Ordinary DifferentialEquations

7.1 Introduction

Differentialequationsexpress relationships
betweenvariablesin termsof their deriva-
tives,in contrastto the algebraicequationscoveredin Chapters2 and 3 whereno
derivativesoccurred.The needto solvedifferentialequationsarisesin a greatmany
problemsof mathematical modelling,because physicallawsin engineering and scienc€
areoftenexpressed in termsof thederivativesof variablesratherthatjust the variables
themselves.
The solution to a differential equation can sometimes be arrived at by analytical
integration. For example, the simplest type of differential equation is of the form

dy
, : Jlx) (7.1)
ox

where/(x) is a givenfunctionof x, and y(x) is the requiredsolution.Provided/(x) can


be integrated, the solution to eq. 7.1 is of the form
I
v: lf(*)dx+c (7.2)
J

whereC is an arbitraryconstant.In orderto find thevalueof C, someadditionalpiece


of informationis required,suchas an initial valueof y correspondingto a particular
valueof x.
Differential equationscan be presentedin many different forms however,often
involving functionsof x snd y. Beforedescribingdifferent solution techniquesthere-
fore,we teed to definesomeimportant classesof differentialequations,as this may
'influenceour method of tackling a particular problem.
;ii:"

7.2 Definitionsand typesof differentialequations


Differential equationsfall into two distinct categoriesdependingon the number of
independentvariablesthey contain. If there is only one independentvariable,the
derivativeswill be 'ordinary',and the equationwill be calledan'ordinary differential
equation'.If morethanoneindependent variableexists,the derivativeswill be'partial',
and the equationwill be calleda'partial differentialequation'.

210
S O L U T I O N SO F O R D I N A R YD I F F E R E N T I AELO U A T I O N S 211

Althoughordinary differentialequationswith only one independentvariablemay


be consideredto bea specialcaseofpartial differentialequations,it is bestto consider
the solution techniquesfor the two classesquite separately.The remainderof this
chapteris devotedto the solutionof ordinary differentialequations.
The 'order' of an ordinary differentialequation correspondsto the highest
derivativethat appearsin the equation,thus

l" -3y'+4:y is secondorder (7.3)

dY d2v
tion
wherewe usethe nota'- y'' : --77etc.
dx, t"

In the sameway

#.(:i)':' is fourth order (7.4)

A 'linear'equationis onewhichcontainsno productsof the dependentvariableor


its derivatives,thus eq.7.3is linearwhereas7.4is nonlinear,due to the squaredterm.
A generalnth order linear equationis givenas

e^1flfi+A,-,(x)# . " ' * '41(x) : R1v;


oq+'4o1x;v
(7.s)

wherethe .Ai'sand R are functionsof x.


Sometimesthe degreeof an equationis also referredto, and this representsthe
powerto whichthehighestderivativeis raised,thuseqs7.3and7.4areboth first degree.
A consequence ofthis is that all linearequationsarefirst degree,but not all first degree
equationsare linear,for example
y" +2y' *y:0 is secondorder,first degree,linear (7.6)

but

l" *2y'+y2:g is secondorder,first degree,nonlinear (7.7)

Nonlinear equationsare harder to solve analyticallyand may have multiple


solutions.Numerical solution of nonlinear boundary value problems will require
iterativeapproachesas will be shownin a later section.
The higher the order of a differentialequation,the more additional information
must be suppliedin order to obtain a solution. For example,eqs 7.8-7.10are all
equivaientstatementsmathematically,but the secondorder equation requirestwo
additionalpiecesof informationto beequivalentto the first orderequationwhichonly
requiresone additionalpieceof information.
y" -y*X*1, -0,
),(0) :0
.1,''(0) (7.8)
y' :y*x, l0):0 (7.e)
-V:€t-x- I (7.10)
212 CHAPTER
7

The third equation, 7.10,is purely algebraicand representsthe solution to the two
differential equations.
In general, to obtain a solution to an nth order ordinary differential equation such
as that given in eq.7.5, n additional piecesof information will be required.
The way in which this additional information is supplied greatly influences rhe
method of numerical solution. If all the information is given at the same value of the
independentvariable,suchas in eqs 7.8 and 7.9,the problem is termed an 'initial value
problem'. If the information is provided at different valuesof the independent variable,
such as in the secondorder systemgiven by eq.7.I l, the problem is termeda 'boundary
value problem':

y ," +, -.y 1 , | 6
'--y:-, 1 , ( l ) : I , y ' ( 1 . 5 ) :- I (7.1 )
x x - x -

All bbundaryvalueproblemswill beat leastof secondorder.It shouldalsobenoted


that as first order equationsonly requireone pieceof additionalinformation,all first
order equationsmay be treatedas initial valueproblems.
The solutiontechniques for initial and boundaryvalueproblemsusuallydiffer
substantially,so they will be consideredseparately.

7.3 Initial Yalue problems

We will limit our discussionfor now to the numericalsolutionof first orderequations


subjectto an initial condition,thus

wirhflxo):yo
fl:f{*, i, (7.r2)

where/(x, y) is any functionof x and y. The equationin this generalform could be


It linearor nonlineardependingon thenatureof thefunctionf (x, y).It may benotedthat
if the equationis linearit can oftenbe solvedanalyticallyby meansof an integrating
t ;
factor.
If the function f (x, y) is nonlinear,the analyticalapproachis greatlylimited,and
may evenproveimpossible.Only a limitednumberof nonlineardifferentialequations
can be solvedanalytically.
All the numericaltechniques for solvingequationssuchas eq.7.l2involvestarting
at the initial condition(xo,yo)and steppingalongthex-axis.At eachstep,a newvalue
of y is estimated.As more stepsare taken the form of the requiredsolution flx) is
obtained.
The effecton y of a changein x is given by eq. 7.13

li+ r -'':['.'#o' (7.l 3)

where (x;, y1)is the initial condition, and -1li


* r is the new estimate of y corresponding to
S O L U T I O N SO F O R D I N A R YD I F F E R E N T I AELO U A T T O N S 213

x : xi * t . Thesteplengthin x is undertheuser'scontrolandis usuallydefinedas


fr,thus
Xia 1- Xi: 11
(7.14)
Rearrangementof 7.l3 leads to

frr+ r dV
yi*r:/i+J,, (7.rs)
*ot
or alternatively
new intesration-l
I o.valuel:
fY J ["tO .""]".-l* f-nur.ri.ut (7.16\
L L ofy J I pr ocess J
Equation 7.16 gives the general form for all numerical solution techniquesfor initial
value problems. In the previous chapter we integrated under a curve of y vs.
x to
compute an area, but in this chapter we will be integrating under a curve of (dy/dx) vs.
x to compute the change in y. Many of the methods of numerical integration described
in the previous chapter still apply. The main difference here is that the function to be
integrated may depend on both x and y; thus we will need to modify our methods
slightly to account for this.
There are two main approachesfor performing the integration required by eq. 7.15:

(a) one-step methods, which use information from only one preceding point,
( x i , y i ) t o e s t i m a t et h e n e x t p o i n t ( x , * r , , I i * r )

(b) Multi-step methods or predictor-corrector methods, which use information


a b o u t s e v e r a lp r e v i o u s p o i n t s , ( x , , y t ) ,( x i _ r , I i _ r ) . . . e t c . t o e s t i m a t et h e n e x t p o i n t
(xi * r , yi * 1). These methods have the ability to refine the initial prediction iteratively
by
repeated application of a 'corrector' formula.

One-step methods are self-starting with only the initial condition, whereas
multi-step methods require severalconsecutivevalues of x and'y to get started. These
initial values can be provided by a one-stepmethod if not provided in the initial data.
We will conc€ntrate initially on the numerical solution of a single first order
equation- It will be shown subsequentlyin Section 7.3.1.6that this is not a limitation,
because a higher order equation can be broken down into systems of first order
equations which can be solved by the same methods.

7.3.1 Onestep methds

One-step methods are so called, becauseinformation about one previous step only is
used to generate the solution at the next step. This makes the one-step methods
relatively simple to implement in a computer program. There are several one-step
methods of increasing complexity, and as is often the casein numerical methods, the
more work that has to be done at eachstep,the greater the accuracy.The trade-offto be
z t q C H A P T E R7

soughtis betweenincreasingthe work per stepand decreasing


the numberof stepsto
spana given range.

7.3,1.1Euler'smethod
The simplestone-stepmethodhaslimitedaccuracyand is certainlynot recommended.
However,it forms the basisfor all subsequent methods.The numericalintegrationof
eq.7.l5 is performedusingthe rectanglerule (seeSection6.2.2)wherethe derivativeis
'sampled'
at the initial condition.Given the differentialeouation
y':f(x, y) with y(xo;:yo (7.17)
Euler's method estimates the new value of y using the expression

It:lo*hf(xo,Io) (7.l 8)
where , h:Xr-Xo (7.re)
This is the simpleststatementof eq.7.16.The methodis then repeatedusingthe,new'
initial conditions(xr,y,) to estimatey2and so on. For a generalstepi, we get

li+t:li*hf(xi, y) (7.20)
where ft: x;., 1 - x, (7.2r)

Errorafter2 steps

_t
I
x2, f2)

G r a d i e n=t f ( x r , y r )
( x s ,f s )
Gradient= t ( x o , f 6 )

xo x1
Fig. 7.1 Euler'smethod(two steps)
S O L U T I O N SO F O R D I N A R YD I F F E R E N T I AELO U A T I O N S

Figure7.1showshow Euler'smethodop€rates. The approximatesolutionis assumed


to follow a straightline correspondingto the tangentat the initial point. Clearlyan
error is introducedby this assumptionunlessthe actualsolutionhappensto be linear.
Errors tend to accumulateat eachstepby this methodasshownin Fig. 7.1.The local
error at eachstepcan be reducedby makingh smaller,but thereis a limit to how smali
h can be madefrom efficiencyand machineaccuracyconsiderations.
It should be noted that Euler'smethodfrom eq. 7.18is equivalentto the Taylor
seriestruncatedafter two terms.i.e.
Truncatedby Euler'smethod
h2 htv"' (1 ))\
y(xs+ h): y(xe)+ fiy'(xo) *;y"(xo) *--:-(xo) + ..'
z! Jt

E4ample7.1

Given

''
Y -l- rt
y':^ withl2):2,
x

estimatef2.5) usingEuler'smethod.kt (a) h:0.25 and (b) ft:0.1.

Solution7.1
(a) Two stepswill be requiredlf h:0.25

-+: :
- 2+ 0.2s1'
/'l -l-'l\

)42.2s) | 2.s
\ L /

/2.2s
+z.s\:3.028
12.5)=2.5+0.25[ ) )5
''aJ
t
\ /

(b) Five stepswill be requiredif h:0.1

/ )'+l n!\l : z z
A 2 '. t ) - 2 + o . r (
\ 2 /

A22\=2.2*o r(1t??) : 2.40s


\ 2 . 1 /

- 2.40s
A2.3) * o:(2!!2-1) : r.uro
\ - ' " /

2}!}9!) :
= z.sr++ o:(
s{2.4) r.r23
\ 2 . 3 I
216 C H A P T E R7

ilzs)- z.8zs
. o.t(Y#) : r oou
The exactsolutionis givenby y:x[l +ln(xl2)] hencey(2.5):3.058.

7.3.1.2ModinedEuler'smethod

A logicalrefinementto Euler'smethodis to includemore termsin the Taylor seriesof


eq.7.22.The modifiedEuler methodis analogousto'the trapeziumrule describedin
Section6.2.3and is equivalentto the Taylor seriestruncatedafter three terms.
Given the differentialequationand initial condition
y':.f (x, y) with flxo):yo (7.23)

the nrodifiedEuler methodestimatesthe new valueof y using the sequence


Ko:hf (xo.yd (7.24)
Kr:hf(xo*h, y6*Ks) (7.2s)

Ir:to*I(Ko+ Kr) (7.26)

where Ke is the change in y based on the slope at the beginning of the step, and K1
is the change in y based on the slope at the end of the step.

h
I
li ;
act solution

\___-

I
I
I
Gradient=f(xr,yo+Kol

(xo,/e) T

xo

Fig. 7.2 Modified Euler method


S O L U T I O N SO F O R D I N A R YD I F F E R E N T I AELO U A T I O N S
217

As shownin Fig. 7.2' theapproximatesolutionfollows


the exactsolutionmore closely
than the simpleEuler methodbecausethe integration
of eq. 7.r5 is performedby
samplingthe derivative-f(x, y) at the beginning andend
of the step.The calculationof
Ko in eq. 7.24is equivalentto a simpleEulerstep,and
this is usedto computeK,
leadingto a'modified'Eulerapproach.

Example7.2

Given

, x+v
wirh 12):2,

e.stimate
12.5) usingthe modifiedEuler method.Let h:0.25.

Solution7.2

Two stepswill be requiredif h:0.25

/"1 t-'t\
I(o:0.2s{= ):0.500
\ 2 . /

x,- : o.zs(2'2!^
* : o.rx Step I
\ -'-"
))\
=2's)
I
\ /

)42.25):2+| (0.500
+0.528):2.514

2'2t
-1'='tto):
- : o.zr( 2
*o
. 2 s / o.
- -r29
-'
\

K,' : o2s(2::!:Y1) : o.rro Step 2


\ )s l
\ - ' " /

y{2.5): 2"'
514+ + (0.529+ 0.554): 3.056
(cf.exactsolution 3.058)

73.1t3 Midpoinr method

Another simple one-stepmethod is basedon the midpoint rule


of integration. It may be
recalled from Section 6.3.2 that this is also the one-point Gauss-kgendre
rule.
Given the differential equation and initial condition

y':.f(x, y) with y(xo):yo


0.27)
218 C H A P T E R7

the midpoint methodestimatesthe new value of y usingthe sequence

K6: hf (xs, ys) e.2S)


Kt:hf(xo*Lh, yo+irKo) e.2g)
!r: lo* Kt (7.30)

As shownin Fig. 7.3,the gradientof the solutioncurveis estimatedat the midpoint of


the rangeand this 'average'gradientis assumedto act acrossthe full range.

Exact solution

\_

Gradient
I go+
|h, vo* )x; \.. l-
I
K1

( x s ,f s )
_ 2

t-
!x^w
I
tun----4- ---+J
ln
xo

Fig. 73 Midpoint rule

Example 7.3

Given the differential equation

x + v"
'v ' - x with v0l:2

estimate {2.5) using the midpoint rule. I - e th : 0 . 2 5 .


S O L U T I O N SO F O R D T N A R YD I F F E R E N T I A E
LOUATIONS 215

Solution 7.3
/'l -t-'l\
I(o :0 .2 5 {# ) :o.so )
\ L /
I
I

/z.tzs+2.25\
--#l:o.stqt
K r:0 .2 5 (

il2.25): Z+ 0.5147: 2.5147


/
)
i StepI

/z.zs+z.sut\
Ko:0.25('#l:o.szgq
\ 4 . u J / II
" o): o.roru
*,' : o.rr(''t"=t^?'1
Step2
'2.J't5
\ /
:3.0573
,,Q.r): r.r, O7+ 0.5426 )
(cf.exactsolution3.058)

73.1.4 Runge-Kuttamethods
.Runge-Kutta' methodsrefer to a large family of one-stepmethodsfor numerical
solutionof initial valueproblems,and includethe Eulerand modifiedEuler methods'
'order' of a method
which are first and secondorder Runge-Kutta methods.The
indicatesthe highestpower of h includedin the equivalenttruncatedTaylor series
expansion.
The generalform of all Runge-Kuttamethodsfor advancingfromstep'i'to'i+ I'is
as follows:
.- I

/i* r :/i* I
j=o
wix,f'p"w, (7.31)

wherethe Wi areconstantweightingcoefficients and r is the order of the method-The


K;'s are estimatesof the changein evaluated r locationswithin the rangeh'
y at
A third order Runge-Kuttamethodwould beanalogousto Simpson'srule but this
will not be coveredhere.A surprisingly simplefourth order Runge-Kutta method has
receivedwidespreaduse,and is sometimesreferredto as the Runge-Kutta method.
The Runge-Kutta fourth order method takes the following form. Given the
differentialequationand initial condition
y' :f(x, y) with {x6):y6 (7.32)

then Ks: hf (xs, Y6)


Kr:hf(xo*\h, Yo+}Ko) (7.33)
Kz:hf (xo*Ih' )'o+lKt)
Kt:hf (xo+h, Yo+K2)
C H A P T E } t7

and tr:to+t(Ko +2Kt+2Kr+Kr) (1.34)

The simplicity and accuracy of this method make it the most popular of all one-step
methods lor numerical solution of linear or nonlinear first order differential equations.
Derivation of eqs 7.33 and7.34 is beyond the scopeof the presentwork, however.

Example7.4

Given the differentialequation


''
t' :^ w i t hl 2 ) : 2
x
estimatey(2.5)usingthe fourth order Runge-Kuttamethod. Let fi:0.5.

Solution7.4
/'l- =
t-'l\- l : r
Ko:0.51
\ / /
/z.zs+z.s\
r(,:0.5[ )rs ,l:l.os6
\ -'-" /
/l 'tst- r srR\
K,:0.5(T
/) : r . o o z

K"- o.ses
'^5 ''-\
+ 3'062)
:,.,,,
)s I
/
t { 2 . 5 ) : 2 a ; u + 2 ( 1 . 0 5 61+. 0 6 2 )1+. l l 2 l
:3.058
(cf.exactsolutiori3.058)

frogram 7.1.One-stepmethodsfor a singlefirst order equation


The first program in this chapterobtainsa numericalsolutionto the initial valueproblem
y':f(x, y) with lro):yo
using a one-stepmethod of the user'schoice.Input data initially definethe method to be use4
whereITYPE: I grvesEuler'smethod,ITYPE: 2 givesthemodifiedEulermethod,ITYPE : 3
givesthe midpoint method,and ITYPE:4 givesthefourthorder Runge-Kuttamethod.Other
data consistof the numberof steps(NSTEPS),the steplength(H) and the initial valuesof x and
y (X and Y).
The function /(x, y) is evaluatedby the user-suppliedfunction F which will bc changed
from one problem to the next. With the exceptionof simpleintegercounters,the variablescan
be summarisedas follows:

ITYPE Defines method to be used (seeabove)


NSTEPS Number of steps
SOLUTIONS OF ORDINARY DIFFERENTIALEOUATIONS 221

PROGRAM P71
c
C PROGRAM 7.1 ONE-STEP METHODS (SINGLE EQUATION)
C
c ITYPE: 1 (EULERTS METHoD) ITYPE= 2 (MoDrFrED EULER's METHoD)
c ITYPE: 3 (MrD-PoINT RULE) ITYPE= 4 (4TH oRDER RUNGE-KUT1.A)
c
READ (5, *) ITYPE,NSTEPS,H
READ (5,*) X,Y
WRITE (6,*) (r*********** o N E - S T E P M E T H O D S* * * * * * * * * * r )
WRITE (6,*)
co To (L,2,3,4) ITYPE
1 I{RITE (6r*) (r************ E U L E R S M E T H O D S* * * * * * * * * * * r )
WRITE (6, *)
WRITE (6,*) (' X Y')
DOlOI*O,NSTEPS
wRrTE (6,1OO) X,Y
RKO = H*F(X,y)
X : X + H
1 O Y - Y + R K O
. GOTO50
2 WRITE (6,*) (r******* M O D I F I E D E U L E R SM E T H O D* * * * * * * * r )
WRITE (6, *)
wRrrE (6,*) (, X Y')
Do20I=o,NSTEPS
wRrTE (6,1OO) X,Y
RKO = H*F(X,Y)
RKl = H*F(X+H,Y+RKO)
X = X + H
20y= y+0.5* (RKO+RKI)
co To 50
3 WRITE (6, *) ( | *********** MID-POTNT RULE ************ |
)
WRITE (6, *)
WRITE (6, * ) (. X Y')
Do30I=O,NSTEPS
WRITE (6,r,OO) X,Y
RKO : H*F(X,y)
RK1 : H*F(X+O. 5*H, Y+O. S*RKO)
X : X + H
3 0 Y = Y + R K l -
GO TO 50
4 WRITE (6,*) (r******** 4 T H O R D E R R I J N G E - K U T T A* * * * * * * * r )
I{RITE (6,*)
WRITE (6;r) (r X Y')
Do 4V I = O,NSTEPS
\.. /WRITE (6,100) X,Y
' --____,-,-/ RKO = H*F(X,Y)
RK1 : H*F(X+O.5*H,Y+O. 5*RKO)
RK2 : H*F(X+O. 5*H,Y+0. 5*RK1)
RK3 = H*F(X+H,Y+RK2)
X : X + H
40 Y = Y + (RKO+2.*RK1+2.*RK2+RK3)/6.
50 CONTINUE
10o FoRMAT (2El3.5)
STOP
END

FUI.ICTIONF(X,Y)
c
C T H I S F U N C 1 T T O NP R O V I D E S T H E V A L U E O F F ( X , Y )
C AND WILL VARY FROM ONE PROBLE}4 TO THE NEXT
c
P = (Y+X) *'t2
RETURN
END
222 C H A P T E R7

H Step length
X Initial and updated value of x
Y Initial and updated value of y
RKO
RKl Intermediate values of hf(x, y)
RK2
RK3

To illustrateuseof the programthe followingproblem is to be solvedusingthe fourth order


Runge-Kuttamethod.Given the nonlinearequation

,' :(x + il2 with )'(0): I


estimate{0.5) using5 stepsof h:0.1
The input dataand output from Program7.1aregivenin Figs7.4{a)and (b) respectively
and
the functionf(x,y):(x+y)2 has beenwritten into function F.

One-stepmethod ITYPE
to be used 4
Number and size NSTEPS H
of steps 5 0.1
Initial valuesof X Y
x and y 0.0 1.0
Fig. 7.4(a) Data for Program7.1

h ***t*t***r*

********

X
{TH
oNE-STEP HETHODS i*********

ORDER RIINGE-KItrITA ********

Y \
.0000oE+oo .100008+0L
.100008+oo .112308+01 I
.2000oE+oo .13085E+01 I
.300008+oo .15958E+01 I
.4ooooE+oo .20649E+01 |
.500008+oo .29078E+01
t; Fig. 7.a(b) Resultsfrom Program7.1

As shownin Fig. 7.4(b),the methodgivesthe approximatesolution),{0.5)-2.9078to four


decimalplaces.For comparison,the analytical solution to this problem is given by

,:,-(.*i)-,
leadingto an exactsolutionof )'(0.5):2.9082

7.3.1.5Accuracyof one-stepmctho^

In assessingthe accuracyof one-stepmethods,the dominant error term givesuseful


informationregardingthe influenceof the stepsizeh. The dominanterror term is the
term with the lowestpowerof h not includedin the truncatedTaylor series.It can be
S O L U T I O N SO F O R D I N A R YD I F F E R E N T I AE
LO U A T I O N S 223

assumedthat this €rror term is the largestand thereforethe mostimportant,provided


h is'small'.Thedominanterror term resultsin a localerror associated with a singlestep
of the numericalprocess.The local errorsaccumulatestepby stepleadingto a global
error after seYeralsteps.
From eq.7.22,Euler'smethodhasa dominanterror termof !h2f"(x6).The ftzterm
impliesthat if the steplength is reducedby a factor of 2, the local error will be reduced
by a factorof 4. The globalerror however,will only be reducedby a factorof 2 because
twiceasmany of the smallerstepswill be requiredto reachthe samevalueof x, some
distancefrom the initial condition at x6.
Thenextmethodof increased sophistication
is themodifiedEulermethod,whichis
basedon the trapeziumrule of integration,i.e.
y\xo+ h)- t4xd+ Ihl y'(xo) + y'(xo+ h)l (7.35)

\oting that the second derivative of y at xe in finite differenceform can be written (see
Section7.4.1)

y'(xs* h)- y'(xs)


v"(xd= (7.36)

we get after substitutioninto eq. 7.35,the following truncatedTaylor series:

flxo+h)-flxs)+fty'(xe) +\hzy"(xs) (7.31)


Hence.the dominant error term of the modified Euler method is of the form
th'y-(xo). Halving the step length in this instancewill reducethe local error by
a factgr of 8, but due to the doubling of the number of stepsrequiredto reachthe
requiredsolution,the global error is only reducedby a factor of 4.
In Spmmary,if the dominanterror term in a one-stepmethodbasedon a truncated
Taylor/seriesinvolvesht*l, then the global error at a particularvalue of x will be
appro/imatelyproportional to fi&.Although one-stepmethodson their own do not
usua|! give convenienterror estimates,the property describedabove can be usedto
g;/'Jliatethe necessarystep length to achievea particular level of arcuracy as will be
shownin Example7.5.
A more pragmaticapproachto error analysis,and probablythe one most often
usedin practice,is to repeata particular calculationwith a differentstep size.The
sensitivityof the solution to the value of h will often give a good indicationof the
accuracyof the solution.

Example7.5

Giventhat dyldx:(x+y)2 with {0): l, estimatethe valueof y(0.5)usingthe modified


Euler method with
(a) 5 stepsof 0.1
(b) 10 stepsof 0.05
Useyour solution to estimatethe valueof fi requiredto obtaina solutionaccurateto
five decimalplaces.
r

224 C H A P T E R7

Solution7.5

UsingProgram7.1
5 stepsof h:0.1 gives1'(0.5):2.82541
10 stepsof ft:0.05 gives10.5):2.88442
The globalerror in this methodis proportionalto ft2,hence
I : C(0.I )2
y,,^"r-2.8254
-
y..u", 2.88402:C(0.05)'
whereC is a constantof proportionality.
Theseequationscan be solvedfor C to give
C:'1.81477
In order for the solutionto be accurateto five decimalplaces,the error must not be
greatJrthan 0.000005, hence
:7.81467h2
0.00000s
and li :0.0008
A final run of Program7'1 with 625stepsof h:0'0008 givesl0'5): 2'90822which is
accurateto five decimalplaces.

73.1.6 Reduction of high order equations


I
n All the examplesconsideredso far have involved the solution of first order equations.
This does not prove to be a restriction to solving higher order equations, becauseit is
it easily shown that an nth order differential equation can be broken down into an
equivalent system of n first order equations. In addition, il the n conditions required to
obtain a particular solution to the nth order equation are all given at the same value of
the independent variable, then the resulting set of initial value problems can be solved
li using the same methods as described previously.
Consider an nth order differential equation arranged so that all terms except the nth
derivative term are placed on the right-hand side, thus

#:t(',,'*,#,':;Jl (7.38)

with initial conditions given as

. dy . d 2 v. d'- 'y
y(xo):,qo,*.(xo): At, (xo): 42,... (xo): A^_, (7.3e)
dl d{

We now replace all terms (except x) on the right-hand side of eq. 7.38by simple
variables.i.e.

dv dzv dn-'y (7.40)


let !:lo, "'6ra- r:-1"- t
*:Yr, 4)j:12,
S O L U T I O N SO F O R D I N A R YD I F F E R E N T I AELO U A T I O N S a') tr,

., .dy dyo d'y - dy, dn-ry dyn-,


ancl notlng that --- :- ---il :-i;- etc.,
ox dx' dx2 dx'"'dx" ' ox

the n first order equations together with their initial condition can be written thus:

dYo
d:r' vo@dA
: o

dYt
t:r, vr6d:Ar
: : fl'411
dyn-,
ln-2{xo): A,-z
*t:1,-t

dy'-,
,t#:f(r, lo,lr,..-,-rn-r) yn-L(xo):An-r

All the derivativesup to n-1 are simply treated as dependentvariables.This


proress of reducing a higher order equation to severalfirst order equations always
results in n-l equations with trivial righGhand sides,and one equation closely
resembling the original differential equation.
Before attempting to solve any high order differential cquations, they must first be
'standard
arranged in the form' of eq.7.41. This may not always be a straightforward
process.

Example 7.6

Reduce the following third order equation to standard form:


,{3., ,{,,
i, 1a2l:2e, wirhflxo):1
ox" ox
Y'(xd: B
Y"(xo): C

Solution7-6

dry
I-ety:ys, lz
*,:rr, 4rz:

then ff:r,
dYt
Y'
Yo@d:A

yr (xo): B
I
I

Standard form
E: I
d r ,: r " , _ r r , I
yz(xo): C I
dx
226 C H A P T E R7

first orderequations
7.3.1,7Solutionof simuitaneous
In general,a systemof n first order equationswill be of the form
dy,
i:0, 1,2,...,n (7.42)
a ; : / r ( t ' l o ,! r , - - - , l , - )
with n initial conditionsy,(xo): Ai, i:0, l, 2,. . . , n
Considerthe systemof two equationsgiven below
dy
z) J'txo) : _Yo
dx: J \x, Y,
(7.43)
dz
, :g(x, y,z) z(x6):zs
ox

We mly advancethe solution of y and z to new valuesBt x1 : ao * h using any of the


one-stepor Runge*Kutta methods described previously.
In general our solutions will be advanced using expressionsof the form

lx,):y(x6)*K
(7.44)
z(xr):71*o'1*7
where the nature of K or L dependson the method being applied. The modified Euler
method leads to the expressions

6:|(Ko+K1) and L:+(Lo+L) (7.4s)


where

Ks: hf (xs, lo, zo)


Ls :hg(x6, yo, zo)
(7.46)
K t: hf (xo* h, ys * Ks, zo* Lo)
Lt :hg(xo+h, ys* Ks,zs* L6),

the midpoint method leads to the expressions

K:Kr a n dL : L r (7.47)
where

K s : h f ( x 6 ,! o , z o )
L s : h g ( x 6 , y o ,z o )
(7.48)
Kt:hf (xo*Ih, yo*}Ko, zs+!Ls)
Lt :hg(xo+lh, yo+|Ko, zo+rLo\,

and the fourth order Runge-Kutta methodleadsto the expressions

,, K o + 2 K t+ 2 K z + K 3 , L o * z L r + 2 L 2 +L 3
and (7.4e)
6 6
S O L U T I O NO EL
DIFFERENTIA
SFORDINARY O U A T I O N S 227

where

Ko:hf (xo, yo,zo)


Ls :hg(xq, yo,zo)
Kt:hf (xo+Ih,yo+iKo,zo++Lo)
L r : hg(xo+ Lh,yo+ iK o,zo+ rLo) (7.50)
K z: hf (xo* IIr,yo+ IK r, zo+ +L)
L2 : hg(xs+ !h, yo+ lK r, zo+ +Lr)
Kt:hf (xo+h,ys* K2,zo+ L2)
Lt : hg(xo* h, ys * K2, zp't L2)

&xample7.7
Given the equationd2yldxz:x-/ with initial conditionsl0):l and dy/dx(0):0,
estimatey(0.5)using
(a) midpoint rule (lr:0.25)
(b) fourth order Runge-Kutta (ft:0.5)

Solution7.7

Firstly reducethe problem to two first order equationsin standardform, i.e.

rlu
?:z
ox
r,(o):I
Az
7:x-y z(o):0
dx

A matrix layout is usefulfor hand calculation-

(a) Midpoint rule (fi:0.25)

:{il,.i^{!},
{;},.-
: o{;:},.-
{l},., {;},.
.Tl*}
{;}",,,:{l}
r ]
: t -o.12sJ
L

:{l}.o',{-l
{;}.,. lli-,} : <
f o.sorl
?

[ -o.2re
j
'T
228 C H A P T E R7

fyl f o.rool o.zsf-o.2tef o.e4l


I :l-o.lorJ I
1,Jo..,,:i-o.zuj* i r 0.25-0.e6ei
p] : t o e 6 e }" +
-"
o z s [ - o 3 o]e: I 0 . 8 e 2 ]
lzjo s [-0.21eJ l. 0.375-0.e4lJ l-0.360J
therefore{0.5):0.892

(b) Fourth order Runge-Kutta(ft:0.5)


dv
y,z), ).(0):I
fi:z:l(x,
dz
t - y : g(x,y, z), z(0):0
d*:
Ko:0.5(0) : 0
Lo :0.5(0- 1): -0.5
Kr :0.5(0-0.25): -0. 125
L, :0.5[(0+0.25)-(+ l 0)]: -0.375
Kz : 0.5(0-0. 1875)
: - 0.094
Lu :0.5[(0+0.25)-(l -0.0725)]: -0.344
Kr : 0.5(0- 0.3M): - 0.t'72
Lr :0.5[(0+0.s)-(l -0.095)]: -0.203
therefore

{'} :l'J.*u
:{'} *1[{^'}*rf*'}+2lK'}+ix'll
I'J.., Llt.l-'\r, t-'\r,I-lr. iJ
:|oJ* -o0 ]*rJ-o.rzs]*rJ-o.os+'|,
_Jrl,1[I " '\-o:uJ"f-o.rzz'll
oLl rl *'l -orzsJ i -ozorJJ
therefore

fy'l f o.sssl
1,1",:i-o.lszj
10.5)-0.8e8
Theexactsolutionin thiscaseisgivenby y : *. x - sinx + x, hence : 6.393.
f,(0.5)

Program 7.2"One-stcpmethodsfor a systemof first order cquations


This program is an extensionof Program 7.1 and allows us to obtain numericalsolutions to
a systemof n equationsof the form

I r ' : f t $ , l r , ! 2 , . . . ,! , ) y l i . ( x dA
: r
l z ' : f z $ , t r , ! 2 , . . . ,t ^ ) yz$i: Az
: :
J / " : f , ( x ,y - r , l z , - . - , t ^ ) y"(xo):A"
230 C H A P T E R7

X = X + H*.5
CALL FUNC(K1,Y,X)

43
Do 43 I = 1,N
Y ( I ) = Y 0( I ) + K 1( r ) * . 5 * H
C A L L F U N C( K 2 , Y , X )
I
DO 44 I = lrN
44 Y(I) = Yo(I) + K2(I)*H
X = X + H * . 5
CALL FUNC(K3,Y,X)
DO 45 I = 1rN
4s Y(I) = Yo(I) + (K0(I)1-2.* (K1(I)+K2(I))+K3(I))/6.*H
4O CONTINUE
5O CONTINUE
1Oo FoRMAT (10813.s)
2 0 0 F O R M A T ( ' X Y ( I ) , I = L , " I 2 )
STOP
END

S U B R O L T T I N EF T ' N C( F , Y , X )

c .- THIS SUBROUTTNE PROVIDES THE VALUES OF F(X,Y) FOR EACH


fFROM ONE PROBLEM TO THE NEXT
c EQUATION AND WILL VARY
c
REALF(*),Y(i)

F(1) = 3.*x*y(2)+4.
r (2) = x*y(1)-y(2) -exp(x)
RETIJRN
END

The samefour methods available in Program 7.1 can be used,and the choicc is left to the user
through the input parameter IryPE. The number of equations to be solved is read into the
v a r i a b l e N . T h e a c t u a l f o r m o tf h e e q u a t i o n s l ( x ,! t , 1 2 . . . y , ) f o r i : 1 , 2 , . . . n i s e v a l u a t e d b y

F'
l
subroutine ruNc which will be changed by the user from one problem to the ncxt. It is rcadily
seen that Program 7.1 is a special case of Program 7.2 with N:1.
With the exception of simple integer @unters, the variables can be summarised thus
t ITYPE Defines method to be used (see main program)

lr;
I
N
NSTEPS
H
X
Y
Number of equations
Number of steps
Step length
Initial and updated values of X
l-d array holding initial and updated values of depcndent variables
I F l-d array holding function values

I
l ill
i rc'I Working space for advancing the solution by one step
I x, I
Kr)
PARAMETERrestrictionMEQ >N.

To illustrate use of the program, tbe following problem is to be solved using the fourth order
Runge-Kutta method. Given that

dv
-1:3xz *4. v(0):4
dx
S O L U T I O N SO F O R D I N A R YD I F F E R E N T I A E
LOUATIONS 231

dz
-z-e" z(0):1
dx:xl

estimatey(0.5)and z(0.5)usingfive stepsof 0.1.


The input data and output from Program 7.2are gtrveninFigs 7.5(a)and (b) respectively.
The
functionsrepresenting dy/dx anddz/dx havebeenwritteninto user-supplied subroutineFUNcat
the foot of the main program.
As shownin Fig. ?.5(b)thecomputedresults{0.5) = 6.2494and 10.5)=0.6739areobtained.

One-stepmethod ITYPE
to be used
Number of N
equations 2
Number and size NSTEPS H
of steps 5 0.1
hiitiat vatueof x X
0.0
Initial valuesof Y(I),I: I, N
dept'ndentvariables 4.0 1.0
Fig. 75(a) Data for Program 7.2

*****r* sYsTEl,ls oF EQUATIONS **********


*t*****r 4rH oRDER RUNGE-KIIITA r**rt***
X Y ( I ) . I = L , 2
. oooooE+oo . {ooooE+o1 .100008+01
. 100008+00 .44 132E+O1 .82536E+OO
.20000E+oo .484718+01 .70295E+OO
.300008E+00 .52967E+Or .63519E+OO
.40000E+oo .57623E+01 .624598+OO
.sOOOOE+OO .62494E+O1 .67386E+OO
Fig. 7.5(b) Resultsfrom Program7.2

7.3.1.8O-Methodsfor linear equations


All of themethodsdescribedsofar for numericalsolutionof initial valueproblemshave
beensuitablefor both linear or nonlinear equations.If the differeniialequationis
linear,a slightlydifferentone-stepapproachis possibleinvolvinglinear interpolation
of derivativesbetweenthe beginningand end of eachstep.
Considerthe first order linear equation
y':f (x,y):l(x)v+(x) with v(xo):Yo (7.51)

where l(x) and k(x) are functions of x.


Writing the differential equation at xs and x1, distance h aPart, and using an
abbreviatednotation whereby (x;):;, and k{x;):k;, we get

* ko
Io': lo-Yo
(7.s2)
(7.53)
It' :lrJt * kr
C H A P T E R7

we now introduce the parameter 0 which can be varied in the range

0<0<l
(7.s4)

and write our one-step method as follows:


- (7.55)
I r : f o+ h[(1 O)ys'+ 0y'rf
The paramei:r 0 acts like a weighting coefficier,ton the gradients at the beginning
and end of the step.When g:0, the simple Euler method is obtained,where only the
gradient at xo is included whereaswhen 0:l only the gradient at x, is taken into
account.
The most popular choiceis 0:0.5, which givesequal weightsto the gradientsat x6
and xr, and is equivalent to the trapezium rule of integration. In the solution of
time-dependent systems of differential equations, the use of 0:0'5 is sometimes
referred to as the'Crank-Nicolson'method. These0 methods are popular becausethey
can be usedeven when extra coupling of the derivative terms means that the equations
cannot easily be reducedto the'standard form' of eqs 7'41'
As the differentialequation is linear, eqs7.52and7.53can be substitutedinto 7.55
and rearranged to give

yo+ft[(l -0X/o/o+ko)+0k'] (7s6t


lt - | -hol,

which for a single equation is an explicit formula for y, in terms of yo and the
functions I and ft. For systems of equations the denominator can become a matrix
'implicit'.
in which case the system is

Example7.8

Given the differentialequation


y':y+x with f'(0):0
estimatey(0.5)usingeq.?.56with 0:0.5- l*t h:0-25.

Solution7.8
+0)+0.5(0.25)]
0 +0.25[0.5(0
i(0.25): 1-0.25(0.5X1)
:0.036

+ 0.25)+ 0.5(0.s)]
0.036+ 0.25[0.s(0.036
f \"'-' I -0.25(0.5X1)
:0.148
(cf.exactsolution0.149)
S O L U T I O N SO F O R D I N A R YD I F F E R E N T I AELO U A T T O N S 233

Secondorder linearequationscanalso be solved by linear interpolation using


the
parameter0.
Given the equation
y":m(x)y'+l(x)y+k(x) (7.s7)
with initial conditions y{xo):yo, y,(xo): yo,,the equation can be written at x6
and x1,
distance h apart, to give

yo":moyo'*l6ys+k6
(7.58)
Ir":mtyr'*Irlt*kr (7.se)
We now obtain the following expressionsfor y1 and y, ' using0 to 'weight'the
derivativesat x6 and xr, hence

| : I o + hl(l - 0)yo' * 0 y'rl (7.60)


1
y r' : yo'+ ft[(l - 0)yo,,
+ 0yif (7.61)
Elimination of yo", yl and y," from eqs 7.5gto 7.61leadsto thefollowingexplicit
formula for y;

yol - h9mr)* hys'fl- h9m,(t - g)l +h2010- 0)(msys,


* Ioyo+k)+ lkrl
!r: (7.62)
1-h0m1-h202ll

Having obtainedj/r, an expression


for the first derivative follows from eq. 7.60,
hence
-lo (l -0)
.. ,_-rr -
Yt:
h0 e lo (7.63)

A value of 0:I remains the most logical choice as it givesequal weight to derivatives
at
each end of the step. It may also be noted from eq.7.63 that the method would fail for
0:0.
For a single secondorder equation,the fourth order Runge-Kutta method is
considerablymoreaccuratethan thetrapeziumrule with B: I described in thissection.
Howeverfor largeengineeringsystemsinvolving equationswith coupledderivatives,
the linear interpolationmethodsinvolving g are still frequentlyusedbecauseof their
simplicity.

73.2 Predictor--correctormethods

Predictor--corrector
methodsuseinformationfrom severalpreviousknown points to
computethenextasindicatedin Fig.7.6.A disadvantage of themethodsis that theyare
not self-starting
andmay oftenneeda one-stepmethodto generate a fewpointsin order
to get started.The attraction of the methodsis that more efficientuse is made of
existinginformation in order to advanceto the next step.This is in contrastto the
fourth order Runge-Kuttamethodwhereat eachstep,four functionevaluationsare
requiredwhich are neverusedagain.
234 C H A P T E R7

.f
-D++ h**h*

Xo x.1 xz x3

Fig. 7.6 Predictor/correctormethods

Predictor-corrector methods make use of two formulae; the predictor formula


extrapolates existing data to estimate the next point and the corrector lormula
improves on this estimate. In some cases the corrector formula can be applied
repeatedly until some convergencecriterion is satisfied.
The flowchart in Fig. 7.7 describesa typical predictor--corrector algorithm.

G i v e ny ' = ( x , y l
y (xsl= fo

Use 1-stepmethod
to get started

ls solution
hardly
changing?

Record new value


ot y and
move on lo
next step

Fig. 1.7 Typicat flowchart for predictor-corr@tormethods


O F O R D I N A R YD I F F E R E N T I AELO U A T I O N S
SOLUTIONS 235

Predictorformulaeestimatethenewvalueof y111by integratingunderthecurveof


y'vs. x usingsamplingpoints3t x;, x;-1, xi-2 €tc.Any numericalintegrationformula
which does not requirea prior estimateof /t*r is suitablefor use as a predictor.
Formulaeof this typehavealreadybeendiscussed in Section6.4.6,wherethe sampling
pointsare outsidethe rangeof integration.
Correctorformulaeimproveon thepredictedvalueof y;* t by integratingunderthe
curveof y' vs.x usingmoreconventionalsamplingpointsat )c;a 1, xi, Xi- t etc.Note that
the correctorformulais ableto sampleat xi1 1because a valueof f i* t is now available
from the predictorstage.Any numericalintegrationformula which requiresa prior
estimateof y'i*, is suitablefor useas a corrector.

7.3.2.1Euler-trapezoidal
method

This is the simplestpredictor-correctormethod,and is equivalentto the modified


'one-step
Euler method describedunder methods'.The only differenceis that the
trapezoidalcorrector formula can be applied severaltimes in order to refine the
solution.
Euler's method is the predictor, and is applied once, followed by several
applicationsof the trapezoidalcorrector,i.e.,
given y' : f (x, y) with y(xo;:yo (7.64)
Euler'smethod(predictor) y,(o):yo *hf (xs, ys) (7.6s)
Trapezoidalmethod(corrector) y ,(') : yo* IhLf (xo,yo)+-f(xl,yl(o))l (7.66)
y r ( t ) : y o +l h l f ( x s ,yo)+.f(x r , y r(r))l (7.67)
etc.
where xr:xo*lt (7.68)

Example7.9

Given y':2x2 +2y with },(0): I


estimated0.2) usingthe EulerI tr apezoidalpredictor-correctormethod.I*t h : 0.2.

Solution7.9
Predictor y{0.2)-| +0.2(2):1.4
Corrector y{0.2)-I +0.1[2+2(0.42+20.41f:1.488
y\0.2)-I +0.1[2+2(0.2)2
+2(1.488)]:1.506
10.2)::I +0.1[2+ + 2(1.506)]:1.509
?tr0.2)'
I +0.1[2+ 2(0.42
),(0.2)- 1.510
+ 2(1.509)]:
Although'convergence'hasbeen achieved,the result has not convergedon the exact
C H A P T E R7

solutionof 1.498.A difficultywith repeatedcorrectorapplications,especiallyin a low


order methodsuchas this,is that the resulttendsto convergeon the finite difference
approximationto thedifferentialequationratherthan the actualdifferentialequation.

7.3.2.2Milne's method

This methodusesa formuladueto Milne asa predictor,and thefamiliarSimpson'srule


as a corrector.The methodis fourth order,i.e.the dominant error term in both the
predictorand correctorincludes/r5,and requiresfour initial valuesofy to get started.
Note thesmallererror termassociatedwith thecorrectorformulaascomparedwith the
predictor.

Milne's formula (predictor)


ah )9,
tyt"(O
.r,*,(0): l i - t + ; l2f (xb y ) - f (x, -r, yi - r ) * 2f (r, - r, l t - )l+ *a t (7.69)

Simpson'srule (corrector)
h l
/,*,(t):I,- t + , [ / ( x r - , , y i - ) ) ' l 4 f ( x 6 y ) + f ( x 1 + 1 , y i + r ( o ) ) -Jn O h t y k ) ( O (7.70)

h
yt * r('\: I i - r + i lf $, - r, yi - ) * 4f (x1,y;)+ f (x;+1,yi + r(t)1 etc. (7.7
r)
J

Milne's predictorintegratesunderthe curveof y' vs.x betweenlimits of xt-. and


x;*, usingthreecentrallyplacedsamplingpointsas shownin Fig.7.8.
A dangerin usingMilne'smethod,or indeedany methodwhichusesSimpson'srule

t; H a t c h e d? r e a = f 1 * ,

Xi-3 Xi-z Xi-t xi

i t t
SamPling
points
Fig. 7.8 Milne's predictor
S O L U T I O N SO F O R O I N A R YD I F F E R E N T T AELO U A T I O N S

as a corrector, is that errors generatedat one stage of the calculation may subsequently
grow in magnitude(seeSection7.3.4).For this reasonother-fourthorder
methods have
tended to be more popular.

7.3.2.3 Adams-Moulton-Bashforth method

A more stable fourth order method, in which errors do not tend to grow so fast is
based
on the Adams-Bashforthpredictor, together with an Adams-Moulton corrector,i.e.,

Adams*Bashforth predictor

h
yr+r(o):yi+ yi_)-9.f(xi_r, yi_:)l
f i l S s f { * , , y ; ) - 5 9 f ( x ; - r , . ' , i _ r +) 3 7 f ( x ; _ 2 ,
. 251
-_L rt
i 2 o ' t 6s,,t"11
Y \\' (7.72)

Adams-Moulton corrector
h
l, * r(tt: I i * ) ff (x,- r, I i _ ) - 5f (x,_r, yi_ r ) * l9f (x;, y,)+ 9f (x,*,, y,* r(o))l

l9
- .r^h' Yt"(1) g.lly

h
!,*r('t:li*)lI$,-z,li_)-5-f(r,_r,yi_r)* l 9 f ( x i , y ; ) + 9 f ( x , * , , y , * r ( r ) ) l( 7 . 7 4 )

etc.
The improvedstabilityis obtainedat a costof somewhatlargererror termsthan in
Milne's rnethod,although the dominant terms still indicate that the corrector is
considerablymore a@uratethan the predictor.
The Adams-Bashforthpredictor integratesunder the curve of y, vs. x between
limits of x, and x,* ,, usingsamplingpointsshiftedto theleft of the rangeof integration
as shown in Fig. 7.9.

Example7.10

RepeatExample7.9 using the Adams-Moulton-Bashforthmethod


Given y':2x2 +2y
with );(- 0.6):0.1918
l-o'+;:6'4146
r( - 0'Z;: 9'6655
r40) :1.0000
C H A P T E R7

xi-z xi-z Xi-t


t t t
Sampling
. points

Fig. 7.9 Adams-Bashforthpredictor

Solution7.10

: | +T{ss(z)- ssl:2(o.42+
Predictor)40.2) 40.6655)l
+ 37l2(0.q2 I 40)l- 9l2(0.6)'+ 2(0.I 9I 8)l) : r.4941
+ 2(0.4

: | +T{12{0.4)'
Corrector il0.2) n) - 512(0.42+
+ Ko.qt 2(0.66s
s)l+ rs(2)
+ 4r.494r)l\ : r.497
+ 912(0.42 6
r(0.2;:1.4931 (2nditeration)
il0-2):1.4982 (3rditeration)
(cf.exactsolution 1.498)

73.2.4 Accuracyof predictor-+orrectormethods

An attractivefeatureof predictor-rorrectormethodsis that they tead to a simple


methodfor estimatingthe error in the computedsolutionafter the first applicationof
the correctorformula.
Theestimateis basedon theerror termsassociated with thepredictorand corrector
formulae.L"t y,* r(o)and 1lr*r(t) representthe valueof yi* r computedby a predictor
and thefirst applicationofa correctorrespectively. Ifflx;a 1)is theexactsolution,then
by Milne's methodfor examplewe can write

),(x,*,) - y, *,(o): 38h5y(')((,


) (7.7
s)
S O L U T I O NOSF O R D I N A RD
Y I F F E R E N T IEAQLU A T I ON S 239

and

- - # hty(")((2) (7.76)
.1,(x,*,) y,* r(t):

Assumingthat y("{(r):y(')({r), (7.77)

then eqs 7.75 and 7.76lead to

hs y.'t(0=B(y, * r(t)- y,* r(o)) (7.78)

and after substitution into 7.76 we get

]'(xi*, ) - Y,* r(t' =


- iv(Y,* r(t)- Yr* r(o)) (7.7e)

Similar operationon the Adams-Moulton-Bashforthmethod leadsto eq.7.80


a

y ( x ,* r ) - y , * r ( t ) = - * ( Y , * , ( t ) - Y , * r ( o ) ) (7.80)

Equations 7.79 and 7.80indicate that the error in the first corrected value of y;* 1 is
proportional to the differencebetweenthe predicted and corrected values.Subsequent
applications of the corrector are thereforeonly of marginal value as they give no further
information on the error size. It is recommended that when using Program 7.3, the
tolerance is set to the required accuracy,and the step length reduced until convergence
is achievedin one iteration.
The ability to estimatethe error at each step of the solution pro€ss means that the
step length can be adjusted to give the desiredlevel of accuracy.If required, a program
could take account ofthe error, and adjust the stepsizeaccordingly. Ifthe error was too
big, one strategy would be to keep halving the step sizeuntil the accuracy criterion was
met. At a subsequentstageof the calculation, the previously obtained step length might
become excessivelysmall, in which caseit could be increasedsystematically.It should
be noted however, that if the step length is changed during the calculation, it will b€
necessaryto recall someone-stepstarting procedure in order to generateenough points
at the new step length for the predictor-corrector algorithm to proceed.

Program 73. Predictor-rorrector using Adams-Moulton-Bashforth fourth order method


This program obtains a numericalsolution to the initial value problem

y' : f (x, y) with y(xs): ye

using the fourth order Adams-Moulton-Bashforth predictor-corrector method. The method


requiresfour initial valuesofx and y in order to proceed.The first piecc of data read by the
programis the integerNIC. If NIC:4 then the four data pointsshouldbe read immediately.
Atternatively,if NIC:1, only one initial condition should be provided,and the remaining
three are generatedautomatically using the fourth order Runge-Kutta one-stepmethod.
Once the four initial conditionshave bcen either read or generated,the predictor and
correctorformulaeare applied.After the first applicationof the correctorformula an error
240 C H A P T E R7

PROGRAM P73

c P R O G R A M7 . 3 P R E D I C T O R C O R R E C T O RH E T H O D U S I N G
4TH ORDER ADAHS/BASHFORTH/MOULTON

c FouR INITIAL VALUES oF X AND Y PRoVIDED (NIC= )


c
ONE INITIAL VALUE PROVIDED AND THE REMATNING THREE
GENERATED USrNG 4TH ORDER RUNGE-KUTTA (NIc=1)

REALX(s),Y(5)

READ (s, *) NIC,NSTEPS,H,TOL


WRITE (6,*) (r********* P R E D T C T O RC O R R E C T O R M E T H O D* * * * * * * * * r )
wRITE (6, *)
wRITE (6, *) ( r * * * * * * * * * * A D A H S / B A S H F O R T H / H O U L I i O* *N* * * * * * * * * r )
wRITE (6, *)
IF (NIC.EQ.4) THEN
WRITE (6,*) (r********** 4 TNITTAL VALUES PROVIDED *********r)
V T R T T E( 6 , * )
r WRITE (6,*) (
+ r X y ERR ITERATIONS I )
D O 1 0 I = 1 , 4
READ(s,*) X(r),Y(r)
10 wRrTE (6,LOO) X(I),Y(I)
ELSE
WRITE (6,*) (r,'********* 1 INITIAL VALUE PROVIDED ********** I
)
wRrTE (5,*) (r*** R E } I i \ I N I N G 3 FROH 4TH ORDER RUNGE-KUTTA *** I )
wRrTE (6, * )
wRIrE (6,*) (
+ I X Y ERR ITERATIONS I
)
READ(s,*) X(1),Y(1)
wRrTE (6,l"oo) x(1) ,Y(1)
D O 2 O I : 1 , 3
RKo = H*F(X(I),Y(I) )
RKl = H*F(X(I) +0-5*H,Y(I)+0.5*RKo)
t; RK2 = H*F(X(I)+0.s*H,Y(r) +o. s*RK1)
RK3 = H*F(X(I) +H,Y(I)+RK2)
x ( I + l ) : x ( I ) + H
Y(I+1) = Y(I) + (RKo+2.*RK1+2.*RK2+RK3)/6.
20 wRrTE (6,100) X(r+1),Y(r+1)
END TF
DO30I=I,NSTEPS
I Y ( 5 ) : Y ( 4 ) + H l 2 4 . * ( 5 s . ' . F ( x ( 4 ) , Y ( 4 )) - s e . * F ( x ( 3 ) , Y ( 3 ) ) +
r Il
37.*F(X(2) ,y(2)) -9.*F(X(1),y(1) ) )
' I YHoLD : Y(s)
x ( s ) = x ( 4 ) + H
ITERS - O
40 ITERS=ITERS+1
Y ( 5 ) = Y ( 4 ) + H / 2 4 . * ( e . * F ( x ( s ) , Y ( s ) ) + 1 e . * F ( x ( 4 ) , Y ( 4) )-
5 . * F ( x ( 3 ) , Y ( 3 )) + F ( x ( 2 ) , Y ( 2 )) )
IF (ITERS.EQ.1) 8RR= -t-/L4.* (Y(s)-YHoLD)
r F ( A B S ( ( y ( 5 ) - Y H o L D ) / y H o L D ) . L E . T O L )c o T o s o
YHoLD: Y(s)
GO TO 40
50 wRrTE (6,100) X(s),Y(s),ERR,TTERS
D o 6 0 J = r , 4 .
Y(J) = Y(J+1)
60 x(J) = x(J+l)
30 CONTINUE
lo0 F O R M A T( 3 8 1 3 - 5 , I 8 )
STOP
END

FUNCTIONF(X,Y)
S O L U T I O N SO F O R D I N A R YD I F F E R E N T T AELO U A T I O N S 24'l

THIS FIINCTION PROVIDES T H E V A L U E O F F ( X , Y ) )


c AND WILL VARY FROM ONE PROBLEM TO THE NEXT

F : X * X + Y * Y
RETURN
END

estimateis madefrom eq.7.80.If required,thecorrectoris appliedrepeatedlyuntil thechangein


y;a 1from oneiterationto the nextis lessthana toleranceTOL readin asdata.The conversence
criterion is dimensionless,
and can be statedas follows:

<TOL

then the value of y;*, is printed,togetherwith the numberof iterationsrequiredto achieve


@nvergence. Also printed,is the error estimateafterthe first applicationof the corrector.It is
recommended that oncea valueof TOL isdecidedupon,thevalueof thesteplengthH shouldbe
reduceduntil convergence is achievedwith a singleapplicationof the corrector.
With the exceptionof simpleintegercounters,the variablescan be summarisedas follows:

NIC Number of initial conditionsprovided


NSTEPS Number of steps(predictor/correctorstage)
H Steplength
TOL Convergence tolerance
RKO )
RKr I Intermediatevaluesof hf(r, y) for fourth order Runge-Kutta
RK2
{
RK3 '
ITERS Iterationcounter
ERR Error estimatesafter first correctorapplication
YHOLD Old valueof -l'i*r
xi l-d arraysholding5 valuesof x and yfrom i-3 to i+l
Y J

The function /(x, y) is evaluatedby user-suppliedfunction F which will be changedby the


userfrom one problemto the next.

Number of initial NIC


conditions I
Number* and sizc NSTEPS H
of steps 7 0.05
Convergence TOL
tolerance 0.0001
Initial value(s) X Y
o f x a n dy 0.0 0.0
*Number of stepsduring predictor-{orrector
stageonly
Fig. 7.10(a) Data for Program7.3
C H A P T E R7

*** ****** P R E D I C T O R C O R R E C P O RH E T H O D r * * * * * * * *
* * * * * * * * * * ADAf,lS/ BASHFORTH/HOIILTON ***********
******t*** I INITIAMLUE PROVIDED ****'r*****
*** RE}'{JNING 3 FROH 4TH ORDER RUNGE-KU:ITA ***
X Y E R R TTERATIONS
.00000E+oo .00000E+00
.500008-01 .41667E-O4
.1000oE+oo .333338-03
.1500oE+oo .11250E-02
.200008+oo .266598-02 -,36{21E-08 i

.25000E+00 .52093E-02 -.781658-08


.300008+oo .90035E-02 -.]-3770E-o7 I
.350008+0o .14302E-01 -.2L287E-07 I

.400008+oo .213598-01 -.30734E-07 1


.4500oE+oo .304358-01 -.42043E-07 I
.50000E+00 -il1791E-01 -.55513E-07 1

Fig. 7.10(b) Rcsultsfrom Program 7.3

To i[ustrate useof thi program,thefollowingproblemis to bc solved.Giveny' : vz .uyz *i11t


l0) : 0, estimate10.5) usingthe fourth orderAdams-Moulton-Bashforthmethodwith ft :0.05.
As only one initial valueis providd MC is setto I and the threeother points are generated
usingthe Runge-Kutta method.The input data and output from Program 7.3 are gtrvenin Figs
7.10(a)and ?.10(b)respectively. The functionx2+y'has beenwritten into function F at the
foot of the main program.As shownin Fig- 7.10(b),with a tolcranccvaluc of TOL:0.0001 the
result10.5):0.&179 wasachievcd.As only I iterationwasrequiredat eachstep,no iterations
on the corrector were necessary, and the error estimateERR appliesdirectly to the computed
result.In this case,the error estimateon {0.5) is -0.558x 10-7.

733 Stitr equations

Certaintypesotdiffercntialequationsdo not lendthemselves to numericalsolutionby


the techniquesdescribedsofar in this chapter.Problemscan occurif the solution to the
systemof equationscontainscomponentswith widely different 'time scales'.
For example,the solutionto a secondorder differentialequationmight be of the
form

{x):Cre-'*Crs-roo' (7.81)
wherethe secondterm decaysvery muchmore rapidly than the first. Sucha systemof
equationis said to be 'stiff'and solutionsare unreliablewhen treatedby traditional
methods.Any steppingmethodusedto tacklesucha problemnumericallymust have
a step length small enough to accountfor the 'fastest-changing'component of the
solution,and this stepsizemustbemaintainedevenafterthe'fast'componenthasdied
out. As has beendiscussedpreviously,very small steplengthscan havedisadvantages
from efficiencyand accuracyconsideration.

7.3.4 Error propagarion- numericglstability

All numericalmethodsare approximatein nature,and someof the sourcesof these


errors werediscussedin Chapter 1.
S O L U T I O N SO F O F D I N A R YD I F F E R E N T I A E
LOUATIONS 243

Whensolvingdifferentialequationsby repetitivealgorithmswhereerrorsintroduced
at one stageare carried on into subsequentcalculations,the question arisesas to
whether these errors will propagate with increasedmagnitude or remain within
acceptablelimits.
An 'unstable'processin the context of numericalmethodsis one in which a small
perturbation introducedinto the calculationwill grow spontaneously.Severalsources
of instability' can occur.
One sourceof instability' can be in the differentialequationitself rather than the
numericalmethodbeing usedto solveit. For example,the secondorder equation

y"-3y' - l0y:s (7.82)

with

J.(0): I and y'(o): -2

has the exactsolutiony:e-l', which decaysas x increases.


If a small pcrturbatione is introducedinto one of the initial conditions,i.e.

l,(0): I +e (7.83)

then the new solutionis given by

y : e - 2 ' + e 1 ]e - 2 ' + 4 e 5 1 (7.84)

which tends to infinity as x becomesvery large.


The small perturbation hascauseda hugechangein the solution for large x, hence
this differentialequationis said to beunstable.When tackling'unstable'problemssuch
as this by numericalmethods,any of the sourcesof error describedin Chapter 1 could
contribute to an initial perturbation of this type and lead to completely erroneous
solutions.
Another sourceof instability in the solutionof differentialequationscan comefrom
the differenceformula itself.As mentionedearlier in this chapter,Simpson'scorrector
formula (7.70)can lead to instability. It can be shown that this formula doesnot cope
well with differentialequationsof the form

y' : cy wherec is negative (7.85)

In thesecases, theerrorsto growexponentially.


a spurioussolutioncauses Spurious
solutionscan occurwheneverthe order of the differenceformula is higher than that of
the differential equation being solved.
In somemethodsthespurioussolutionscancauseproblems,whereasin otherstheir
effectsdie away.For example,the Adams-Moulton-Bashforthfamily of predictor-
correctorformulaedo not sufferfrom instabilityproblems.
The interestedreaderis referredto Section7.6tor further readingon this topic.
244 C H A P T E R7

7.3.5 Concluding remarks on initial value problems

No single method for obtaining numericalsolutionsto initial value problems can be


recommended for all occasions.Fourth order methods are to be recommended on the
grounds of accuracy without undue complexity. The question still remains as to
whether to use one-step or predictor-rorrector methods.
The following points should be consideredbeforedeciding on a particular method.
(a) Predictor--corrector methods are not self-starting, and must actually rely on
a one-stepmethod in order to generateenoughpoints to get started.Ifa changein
step sizeis made during the solution proc€ss,a temporaryreversionto the one-step
method is usually required. Changesin the step sizeare very easy to implement in
a one-stepmethod.
(b) One-step methods are of comparable accuracy to predictor-corrector methods of
the same order. However, the predictor-<orrector methods provide a simple error
estimate at each step, enabling the step size to be adjusted for maximum amuracy
and efficiency.No such estimateis usually availablewith a one-stepmethod, hence
the step size ft is often made smaller than necessaryto be on the'safe side'.
(c) To advance one step using the fourth order Runge-Kutta method requires four
evaluations of the function f (x, y) which are usedjust once and then discarded.To
advance one step using a fourth order predictor-corrector method usually requires
just one or two function evaluations for convergence.In addition, function
evaluations in a predictor--correctormethod can be stored and used again, as only
whole intervals of x are required.
The fourth order methods of Runge-Kutta and Adams-Moulton-Bashforth
are the preferred one-step and predictor-corrector approaches that have been
described in this chapter. The fourth order Runge-Kutta method has the great
advantage of simplicity, and for this reason alone is probably to be recommended
for most engineering applications.
Many methods exist for numerical solution of differential equations, including
newer hybrid methods which take advantageof both the simplicity of the one-step
methods and the simple error-estimatesof the predictor-corrector methods. These
refinements are only obtained at the cost of greater complexity and more function
evaluations per step. The reader is referred to more advanced texts on numerical
analysis to learn of such developments.

7.4 Boundaryvalueproblems
When we attempt to solve ordinary differential equations of second order or higher
with information provided at different values of the independent variable, we must
usually resort to different methods of solution from those described in the previous
section.
'time'
The initial value problems covered previously often involved as the
independent variable, and solution techniques required us to 'march'along in steps
until the required solution was reached.The domain of the solution in such casesis not
S O L U T I O N SO F O R D I N A R YD I F F E R E N T I AELO U A T I O N S 245

finite, because in principle we can step along indefinitely in either the positiye or
negativedirection.
Boundary value problems usuallyinvolve a domain of solution which is finite,and
we wish to find solutionsto the equationwithin that domain.The independentvariable
in such problems is usually a coordinate measuringdistancein space.A typical second
order boundary value problem might be of the form

v" : f (x,y, y') (7.86)


with flA) : y 1 and y(B) : y ".

The domain of the solution, assuming.B>.4,is given by valuesof x in the range


,4 ( x ( B and we are interestedin finding the valuesof y corresponding to this range of
x.
Numerical solution methods that will be consideredin this chapter fall into three
categories:

(a) Techniques that replace the original differential equation by its finite difference
eouivalent.

(b) 'Shooting methods', which attempt to replace the boundary value problem by an
equivalent initial value problem.

(c) Methods of 'weighted residuals', where a trial solution satisfying the boundary
'guessed',
conditions is and certain parameters within that solution adjusted in
order to minimise errors.

7.4.1 Finite differencemethods

The finite differencemethod replacesthe derivative terms in the differential equation by


their'finite difference'equivalents.As will be shown, if the differential equation is linear
this process leads to a system of linear simultaneous equations, which can be solved
using the methods described in Chapter 2.
Initially, we need to define some finite difference approximations to regularly
encountered derivatives. Consider the solution curve in Fig. 7.1I in which the x-axis is
subdivided into regular grid points, distance h apart.
we may expressthe first derivative of y with respectto x evaluated at xi, in any of
the following ways:

yi=|t;!'forward' difference (1.87)

vi =Y-t#l'backward' difference (i.88)

u,'-'o#a'cenrral' difference (7.8e)


C H A P T E R7

*11 **11 h

Xi-2 Xi-t Xi Xi*t Xi*2

Fig. 7.11 Finite differencegrid points

It appearsthat the required derivativc is approximated by the slopeof a straight line


joining points in the vicinity of the function at x1. For smooth functions, the central
difference form is likely to be the most accurate, being the average of the forward and
backward difference versions. The second derivative at xr can be estimated by taking
the difference of first derivatives. i.e.

lt.
, ,- - y i - y ' t - t
t t -
h

li+r-ft li-li-t
h h
h (7.e0)

Y i *t - 2 ! r * l i - r
It -
h2
(7.er)
which is a central difference formula" being symmetrical about x1.
Similarly a central difference formula for the third derivative at x; is given by

, r . u-,! " i * l - l " t - ,


t t -
2 h

- lt- zt2yt-,-2yr* r* !t+z


(7.e2)
zh"
and for the fourth derivative at x1 by

i - t - 2 Y " i * Y "t * t
t, , i " t. - l " - h z

li- z- 4!i- 1* 6Yi- 4Yt*t* !t+z


(7.93')
S O L U T I O N SO F O R D I N A R YD I F F E R E N T I A E
L OUATIONS

Forward and backward differenceversionsof thesehigher derivativesare also


readily obtained, and by including more points in the differenceformulae, greater
accuracy can be achieved.A summary of the coefficientsfor forward, central and
backwarddifferenceformulae,was presented in Section5.3.
Backwarddifferenceformulae are simply mirror imagesof the forward difference
formulae, exceptfor odd numberedderivatives(i.e- y',y"') where the signs must be
reversed.
For example,a four-point forward differenceformula for a first derivativeat xe
from Table 5.2 is given by
I
y'o:d( 1 l y 6* 1 8 y 1 - 9 y 2 + 2 y ) - [ h 3 y i " (7.e4)

and the correspondingbackward differenceformula from Table 5.4 by


a
I
y'o :
Ei(llye
- 1 8 y - r 1 - 9 y- z - 2 y - r ) + l h 3
f" (7.es)
Thesolutionto a'two-point'boundary valueproblemsuchasthat givenin eq.7.86
involvessplittingthe rangeof x for whicha solutionis requiredinto n equalparts,each
of width Ir. Figure 7.12givesan exampleof this with n:4.

Known
solution
y' = f (x, y, y' )

--!'!':-l:::-'!)::: - \
Known
solution\
-----t-,---/ )
I

A r r x 2 x 3 B
Fig. 7.12 Two-point boundaryproblem(n:4)

Hence, if the boundary conditions are given at

x:A and x=8,


let xt:xo{ ih, i:1,2,...,n
where xo:A and xn:p.

The differential equation is then written in finite difference form at each of the
internal points i:1,2,..., n - L If the differential equation is linear, this leads to n - I
s i m u l t a n e o u sl i n e a r e q u a t i o n s i n t h e u n k n o w n v a l u e s! t , 1 2 , . . . , ! , - r . The more
248 C H A P T E R7

subdivisionsmadein the solutiondomain,the more simultaneous equationsto be


solved,but the greater
the detailand accuracyof the solution.

Example 7.1I

Given y" :3x * 4y subject to boundary conditions },(0): 0, l,( 1): I , solve the equation
in the range 0(x( I by finite differencesusing ft:0.2.

Solution 7.11

Firstly we write the differential equation in finite differenceform. From Tables 5.2-5.4
we have a choice of formulae for the required second derivative. It is usual to use
the lowest order central differenceform, unless there is a particular reason to use the
less accuiate forward or backward difference forms. hence from eo.7.91.

I
y i ' = - ( y , _ ,- 2 y , * y , * r )
n-

and the differential equation can be written as

I
, - 2 Y , * Y , *1 ) : 3 x 1+ 4 y ; .
or(1,-

The solution domain is split into five equal strips,eachof width h:0.2 as shown in Fig.
7.13.The finite differenceequation is then written at each of the grid points for which
a solution is required. Known boundary conditions are introduced at this stage,with
typical valuesas shown in Table 7.1.
These four equations in the four unknown values of lr, !2, y3 and lr can be solved
using one of the methods described in Chapter 2. A summary of the finite difference

v
1.0
f"=3x+4Y
y ( 0 ) =0 y ( 1 ) = 1

v
n=5
h=0.2

t l

| -- |
(0,0)

Fig. 7.13 Finite differenceExample 7.ll


S O L U T I O N SO F O R D I N A R YD I F F E R E T . I T I A
E LO U A T I O N S 249

Table 7.1. Difference equations in E,xample7.ll

xi Finite difference equation

-' o): o'6+ 4v,


oL*rr, r,+
Q.2

-rr r* 1',): 1.2


+ 4y2
0.4 ft ,r,
-rr, +vr): 1.8
+4v3
0.6 of ,r.
0.8 - r r , *v ) : z -+4 4 v 0 ,
fto

solutionscomparedwith the exactsolutiongivenby


7(e2'-e-2') 3
'V : - - - X -')
4{e'-e 4
is presentedin Table 7.2.

Table 72 Exact and finite difference solutions


from Example 7.ll

/cucr h'o

0.0 0.0 0.0


0.2 0.0482 0.0495
0.4 0.1285 0.13r0
0.6 0.2783 0.2814
0.8 0.5472 0.5488
1.0 1.0 1.0

Clearlythe accuracyof the finite difference solutioncould havebeenfurtherimproved


by taking more subdivisions in the range0<x<1, at the expenseof solvingmore
equations.It should be noted that the equationcoefficientsin this classof problem
havea narrow bandwidth (seeSection2.4).
Example7.11highlightsa problemthat would be encounteredif one of the higher
order finite differencerepresentationsof y" had beenused.For example,if we had used
the five-pointcqntraldifferentformula for y" (seeTable 5.3),valuesof y outsidethe
solution domain would have beenrequiredin order to expressy1" and ya". Further
informationwould then be requiredin order to solvethe system,becausetherewould
be more unknownsthan equations.
The needto introducepoints outsidethe solution domain, at least temporarily,
is encounteredquite frequentlyand is resolvedby incorporating the appropriate
boundaryconditions.
250 CHAPTER7

Considera boundary value problemwhere


y" : f (x, y, y') (7.96\
with ),(,{):0/t andy'(B):y',
In thiscase,y(B)remainsunknown,so thefinitedifference versionof thedifferential
equation will need to be written at x:8. Even when using the simplestcentral
difference formulafor y" givenby eq.7.91,a valueof y correspondingto x: B + trwill be
introducedasshownin Fig. 7.l4.In this case,we have5 unknownsbut only 4 equations.

y"=f(x,y,y,)
Y(Al=fa Known1stderivative
1)z
r'(B)=r'a fs- /z
V^ =
2h
fs-2Yo+ Yt
t B - A

Known
solution
)
/
/

irt

; lI
,*l-,*l
l-,-+-,-+-,-+-
A x t x 2 B=xa
;
Fig. 7.14 Derivative boundary condition

The fifth equationcomesfrom the dedvativeboundaryconditionwhich can also be


written in finite differenceforrn, i.e.,using central differences

(7.e7)

Example7.12
Given xzy"+2xy'-2y:x2
{ 1 ) : 1 , / ' ( 1 . 5 ) :- 1
solvefor y in the range 1(x( 1.5usingfinite differences.
S O L U T I O N SO F O R D I N A R YD I F F E R E N T I A L
EOUATIONS 251

Solution7.12
Let tr:0.1 and write the equationin finite differenceform using centraldifferences,
hence

'fr
xr2 ^ ..xt,
0 t., - 2yi t y,-1)+ f (ri* r - | i - r)- 2y,:x,'

leadingto the equationsindicatedin Table 7.3.


As shownin Fig. 7.15,y6:),(1.6)hasbeenintroducedinto the systemof equations.
The derivativeboundaryconditionis now usedin orderto providethe sixth equation,

Table73. Differencc
equations
in Example7.12
t ,, Finite differenceequations

1.1
ffi 0,-zt,+rt+|{(r,- t) -2y,
: r.t2

1.2
ffi r* -zt,+y,)+# 0,- r ) -2yz:r.z2
1.3
ffi lt,- zyr+yr)+H (y.- !) - Zyt:1.32
1.4
ffi {t, - zt,+y.)+*1(y,- r ) -2vt: r.42
1.5 - li -Zys:r.52
ffi {* -ztr+y.)+H(yu

vzy"+2xy'-2f = x2

),(1)=1,y'(t.5)=-1

Fig. 7.15 Finite differenceExample7.12


252 CHAPTER 7

hence

lo- l* |
0.2

The solution to thesesix linear equations,together with the exact solution given by

135 l4lx x2
'u : - _ - -', | . _
86x2 172 4

is presented in Table 7.4.

Table 7.4. Exact and finite differencesolution


from Example7.12

.)y'crect ho
1.0 1.0 1.0
1.1 0.7981 0-7000
t.2 0.4774 0.4791
1.3 0.2857 0.2902
1.4 0.t432 0.1487
1.5 0.0305 0.0379
1.6 -0.0584 - 0.0513

The finite differencesolutions given in this section have performed quite well with

F.
F
relatively few grid points. This will not always be the case,especiallyif quite sudden
changesin the derivatives occur within the solution domain. In caseswhere no exact
t"
t solution is available for comparison, it is recommended that the problem should be
I
solved using two or three different gradations of the solution domain. The sensitivity of
the solutions to the grid size parameter fi, will often indicate the accuracy of the
[' solution. From the user's point of view, ir should be made small enough to enable the
;i desired accuracy to be achieved,but no smaller than necessary,as this would lead to
excessivelylarge systems of banded equations.

7.4.2 Shootingmethods

Shootingmethodsattempt to solveboundaryvalueproblemsas if they wereinitial


value problems.Considerthe following secondorder linearequation:
y" : f t(x) y' + f z@)y + f {x) (7.e8)
with 1,4):yt and 1\B):y".
Form the equivalentset of first order equations,i.e.
let y' -- z (7.99)
hence 7,:fr(x)z* fz(x)y+/r(x) (7.r00)
S O L U T I O N SO F O R D I N A R YD I F F E R E N T I A E
LOUATIONS 253

In order to solve this problem in the range A<x<B as an initial value problem,
we need to make assumptionsabout the gradient at the initial point x:l-
Hence let z(A):y'(A):ao where a6 is a 'guess' as to the gradient at x:A.
Sometimes, Qs czn be estimated from graphical or physical interpretations of the
solution.
Knowing the initial conditions, we can solve for y and z by any one-stepmethod
using stepsof h, until the solution at x: B is reached.Let this 'solution'for y be given by
yo(x).
We now repeatthe procedureusing a differentinitial gradient,z(A):1/(A): st,
leadingto a new'solution'fory, namell yr&).
If yo(B):bo and y,(B):b, then providedbo*bt, the solutionto the boundary
value problem can be written as follows:
I
,pr1: ----:- l0 u - b r)yo(x)+ (bo- y')yl (x)l (7.101)
oo-Dr

This approximate solution is a linear combination of the two 'incnrrect' solutions


ys(x) and y,(x) ensuring that both boundary conditions are satisfied.

Program7.4. shooting method for a secondorder linear boundaryvatueprobrem


This program obtainsa numericalsolution to the linear secondorder differentialequation
y" : f ,(x) y' + f zg) y + f sg)
subjectto boundaryconditions

AA):y^ and y{.B):yu.


The shooting method describedpreviouslyis used,leadingto a linear combinationof the
solution to two initial value problems.
Initially, the secondorder equationmust be brokendown into two first orderequations,i.e.

2': f ,(x)z+ f z(x)y +/3(x)


which arecomputedby user-suppliedsubroutineFUNc A peReurrrR statementdefinesthe
constantMAX, which must alwaysbe greaterthan the requirednumberof stepsin the solution
domain,henceMAX>NSTEFS+ l.

PROGRAM P74
c
c PROGRAM 7.4 SHOOTING METHODS FOR LINEAR 2ND ORDER
c BOTJNDARY VALUE PROBLE!{S
c
c ALTER NEXT LINE TO CHANGE PROBLEM STZE
c
PARAMETER (l,tAx:2s)
c
REAL Y (2r,Yo(MAX),Y1 (MAX),Ys (MAX)

READ (5. *) NSTEPS,H.XA,YA,XB,YB,AO,A1


254 C H A P T E R7

I{RITE (6,*) (r******** S H O O T T N GM E T H O D F O R * * * * * * * * * * r )


I{RrTE (5r*) (r******** LTNEAR BVPftS USING ***********')
vIRITE (6,*) ('******* 2 rRIAL INTTIAL GRADIENTS *****')
WRITE (5, *)
X = ) G
Y(r1 = Yn
Y(2) = a6
Yo(1) = Y1
D O 1 0 I = 2 , N S T E P S + 1
C A L L R I J N G 4( Y , X , H )
10 Yo(r) = Y(1)
Bo = Yo(NSTEPS+I)
X - x A
Y(1) : YA
Y(2) : A1
Y1(1) = Ya
D O 2 0 I - z , N S T E P S + t
C A I , L R T J N G 4( Y , X , H )
20 v1 (I) = Y(1)
81 = Y1(NSTEPS+I)
f r n r t n 1 e ,* ; ( ' x Y ,)
X = ) G
D O 3 0 f : I , N S T E P S + 1
Ys(r) - ((YB-B1)*Yo(I)+ (Bo-YB)*Y1(I))/ (Bo-81)
wRrTE (6,1O0) X,YS(I)
3 0 X = X + H
100 FoRMAT (10813.5)
STOP
END

S U B R O U T I N E R I J N G 4( Y , X , H )

ADVANCES- TWO 1ST ORDER EQUATIONS

r
c USING THE 4TH OFDER RUNGE-KU:TTA UETHOD
c
R E I A Ly ( * ) , K 0 ( 2 ) , K 1 ( 2 ) , K 2 ( 2 ) , K 3 (2),y0(2)

CALL FtNC(KO,Y,X)
D O 2 I = L , 2
I; 2
Yo(I) = Y111
Y(r) = Yo(I) + Ko(I)*.s*H
X = X + H*.5

$'
h;
3
cAtL FUNC(K1,Y,X)
DO 3 I = 1,2
Y(I) = Yo(I) + K1(r)*.srH
CALL FUNC(K2,y,X)
DO 4 I = 1,2
4 Y(I) = Yo(I) + K2(I)*H
X : X + H*.5
CALL FUNC(K3,Y,X)
D O 5 I = 1 , 2
5 Y(I) = Yo(I) + (Ko(r)12.* (xl(I)+K2(I))+K3(I))/6.*H
RETIJRN
END

S U B R O U T I N E F T J N C ( F ,Y , X )
c
c THIS SUBROUTINE PROVIDES THE VALUES OF F(X,Y) FOR EACH
c EQUATION AND WIT.T. VARY FROH ONE PROBLW TO THE NEXT
c
REAL F(2),Y(2)
c
F(1) = Y12;
F(2) =3.*x+4.*Y(1)
RTURN
END
S O L U T I O N SO F O R O I N A R YD I F F E R E N T I A E
LOUATIONS

With the exceptionof simpleintegercounters,the variablescan be summarisedas follows:


NSTEPS Number of stePs
H Steplength
xa)
Boundaryvalue(A,ya)
;; I
XB]
Boundaryvalue(B,ys)
fi l
a n )
Trial gradients
de and al at x:A
;; l
X x-coordinate
Y ld array holding initial and updateddepcndentvariables
F ld array holding functionvalues
Y0 td array holding first solution
Yl ld array holding secondsolution
B0 Valueof Y0 at x:8
B1 V a l u eo f Y l a t x : B
YS 14 anay holding final solution
The fourth order Runge-Kutta method for two first order cquationsis given in subroutine
nuNc 4.
Toillustrateuscoftheprograrn,tbefollowingproblemistobcsolvcd.Givcnthaty":3x*4y
with l0):0, ),(l): I solvefor y in the range0(x( I usinga shootingmethod.
The sccondorder cquation has been reducedto two first order equations,leading to the
following two initid value problems.

!':z ),(0):0
z':3x*4y z(0)=0
!':z ),(0):0
z':3x*4y z(0): 1
The initial gradientsinput as A0 and Al, correspondingto z(0),havearbitrarily beengiven the
values0 and I respectively.Input and output to the program are shown in Figs 7.16(a)and (b)

Number and sizc NSTEPS H


of steps 5 0.2
Boundaryvalues XA YA XB YB
0.0 0.0 1.0 1.0
Trial gradients AO A1
0.0 1.0
Fig. 7.16(a) Data for Program 7.4

1r***t** SHooTING I{ETHODFOR ***t******


*****rr* LINEAR BVPrS USING ***********
*****r* 2 rRIAI INITIAL GIU{DIENTS*****
X Y
.000008+oo . oooooE+00
.20000E+oo .482158-0:
.400008+oo .12857E+OO
.60000E+oo .278398+OO
.800008+oo .5{529E+OO
.10000E+o1 .10000E+o1
Fig. 7.16(b) Results from Program 7.4
C H A P T E R7

f"=3x+4x
y ( 0 ) =0
y(1],=1

0.5 1.0
Fig. 7.17 Intermediate and final solutions by shooting method

respectively.The final solutionwhichis printedin stepsof h:0.2 is in goodagreement


with the
analyticalsolution(seeExample7.11).The intermediatesolutionsYl and Y2, togetherwith the
final solutionare showngraphicallyin Fig. 7.17.

The shootingmethodcanalsobe usedto obtain solutionsto nonlineardifferential


equations,but we will not beableto uselinearcombinationsof two solutionsaswe did
previously.An iterativeapproachwill berequiredto tacklethenonlinearproblem,and
extra caremust be takenas multiple solutionsmay be present.
Considerthe nonlinearsecondorder differentialequation

i; v": f(v', v'x) (7.t02)

with boundaryconditiony{A): y^, y{,8): y".


of initial value problemsof the form
We will solvea sequenc€

y" : f (y', y, x) with AA): y^, l'(A): ai

By varying the initial gradient ai in a methodical way, we can eventually reach


a solution at x: B that is sufficientlycloseto the required boundary value ys. There are
severaldifferentstrategiesfor'homing'in on the required solution, and theseare similar
to methods for finding roots of nonlinear algebraic equations (Chapter 3).
Once we have two initial gradients, say as and a1, which give values of y at x:B
which straddle the correct solution ys, linear interpolation can then be used to obtain
an'improved' value, as was done in the'false position' method (seeSection 3.3.2).
S O L U T I O N SO F O R D I N A R YD I F F E R E N T I AE
LO U A T I O N S 257

v"=t(y',y,x)
YIA)=le
Y(B)=fa

Y B - Y o )( a t - a o )
a ' = a "^ + (
(y.,-yo)

--------a
Requiredsolution

Fig. 7.18 Shooting method for nonlinear equation

Figure 7.18 shows an example where

gradient co gives y{B):yo and


gradient ct givesy(BJ:y,
where h<!a<lr.

By linear interpolation, an improved value of the gradient is given by

^ +- -u -o T - - ., ( y a - y o ) @ r - a o ) (7.103)
u -
lt-lo

whichgivesy{,8):y*.
258 CHAPTER7

If y* >yt,c* replacescr and y* replacesyr, else


if y* <yu,c* replacesa6 and y* replacesys.
Linear interpolationthen proceedsas before,ensuringthat the two solutions
operatingat any time,straddlethetruesolution.Iterationsstopwhensuccessive
values
of {B) becamesufficientlycloseto the requiredvalueys.

Program75. Shootingmethodfor a secondorder nonlinearboundaryvalueproblem


The programobtainsa numericalsolutionto the secondordernonlineardifferentialequation

v":f(v',v,x)
subjectto boundaryconditionsy{,A):y^and y(B):y". A scquence of initial valueproblemsare
solvedby systematically altcringtheinitial gradientuntil both boundaryconditionsaresatisfied.
Initially,the sccondordere.quation mustbc brokendown into two first ordercquations,i.e.
'
!':z
7': f(2, y, x)

PROGRAH P75
c
c PROGR,AH7.5 SHOOTTNG HETHODS FOR NONLINEAR 2ND ORDER
c BOT'NDARY VALUE PROBLE!{S
c
c ALTER NEXT LINE TO C}IANGE PROBLN,{ SIZE

PARAHETER (l{.AX=25)

REAL Y (2),YO (UAX) .Y1 (MAX),YS (HAX)

h
t
f
r
READ (5, *) NSTEPS,H,XA,YA,XB,YB,AO,Al,TOL,ITS
WRITE (5,*) (r******** S H O O T I N G I . { E T H O D SF O R * r * * * * * * r r )
WRITE (6,*) ('r******* NONLINEAR BVPiS USING ********')
fl wRrTE (5,*) (r******** LINEAR INTERPOLATION r********')
I{RITE (6, *)
X = X A

&'
r;
Y(r1 = Y1
Y ( 2 1= s g
Yo(1) = Yn
D o 1 0 I : 2 , N S T E P S + 1
cAr,L RI'NG4 (Y,X,H)
I 10 Yo(I) = Y111
I B0 : YO(NSTEPS+1)
I X = X A
I Y(1) - YA
Y(2) = a1
Y1(1) = Y1
D O 2 0 I = 2 , N S T E P S + 1
C A I , L R I J N G 4( Y , X , H )
20 Y1(I) = Y11;
81 : Y1(NSTEPS+I)
rF ((B0-YB)* (81-YB).GT.o.) TrrEN
t{RrTE (5,*) ('rRY NgW GRADTENTS')
STOP
END IF
ITERS = O
30 I T E R S = f T E R S + 1
AS = AO + (YB-80)* (A1-AO)/ (81-Bo)
X = X A
Y(1) = Y1
SOLUTIONS OF ORDINARY DIFFERENTIAL EOUATIONS 259

Y(z; = 35
Ys(1) = Ya
D O 4 0 I = 2 , N S T E P S + 1
C A L L R { J N G 4( Y , X , H )
40 Ys(I) : Y(1)
BS : YS(NSTEPS+1)
rr ( (BS-YB) * (80-YB) .GT. o. ) THEN
A o = A S
D O 5 0 I = 2 , N S T E P S + 1
50 Yo(I) = v511;
B 0 = B S
ELSE
A 1 : A s
D O 6 0 I = 2 , N S T E P S + 1
60 Y1(I) = Ys(I)
81 =. BS
END IF
rF (ABS((BS-YB)/YB).GT.ToL .AND. IrERS.LT.rTs) Go To 30
WRITE (6,*) (' X Y' )
X = X A
' wRrrE (6,too) x,Ys(1)
D O T O I = 2 , N S T E P S + l -
X = X + H
70 WRrTE (5,100) X,YS(I)
!{RITE (6,*)
WRITE (6, *)'AFTER" ITERS,'ITERATIONS'
100 FoRMAT (10El3.s)
STOP
END

S U B R O U T I N E R U N G A( Y , X , H )
c
c ADVANCES TWO 1ST ORDER EQUATIONS
c USING THE 4TH ORDER RUNGE-KU:TTA HETHOD
c
REALY1*),KO(2),Xl (2),K2(2),K3(2),vo(2'

CALL FUNC(KO.Y,X)
DO 2 I = 1,?
Yo(r) - Y(r)
2 Y(I) = Yo(I) + Ko(I)*.s*H
x = x + H*.5
CALL FUNC(Kl,Y,X)
DO 3 I = 1,2
3 Y(I) = Yo(I) + K1(I)*.s*H
C A L L F U N C( K 2 , v , X ' )
DO 4 I = 1,2
4 Y(I) = Y0(I) + K2(I)*H
X =.X + H*.5
CALL FI,'NC(K3,Y,X)
DO 5 I = 1,2
s y(I) = Yo(I) + (Ko(I)42.* (Kl(I)+K2(I))+K3(I))/6.t'H
RETTJRN
END

SI'BROUTINE FUNC (F, Y, X)


c
c THIS SUBROUTINE PROVIDES THE VALUES OF F(X,Y) FOR EACH
c EQUATION AND WILL VARY FROM ONE PROBLEI{ TO THE NEXT
c
REAL P(2),Y(2)

F(1) = Y121
F(2) = -2*Y(2)**2/Y(7)
RETURN
END
260 C H A P T E R7

which are computed by user-suppliedsubroutineFUNc. A PARAMETERstatementdefinesthe


constant MAX, which must be alwaysgreaterthan or equal to the required number of stepsin the
solution domain, hence

M A X > N S T E P S +1
The input dataareverysimilarto thoserequiredfor Program7.4excxptfor the convergence
toleranceTOL. Computationwill stop,and a solutionwill be written to the output channel
if the followingconditionis satisfied:
lv*-v"l
l--l<TOL
I la I
or alternativelyif a maximumof ITS iterationis reached.Clearly,if a very strict toleranceis
required,more iterationswill be needed.
With the exceptionof simpleintegercounters,the variablescan be summarisedas follows:

NSTEPS Number of steps


H Step length
XAI
Boundary value (,4,y'r)
YAJ
XB.)
' Boundary value (8, yr)
YBJ
AOJ
Initial trial gradientsas and ar at x:A
AIJ
TOL Convergencetolerance
ITS Maximum number of iterations
AS Interpolated gradient at
X x-coordinate
Y l-d array holding initial and updated dependent variables
F l{ array holding function values
YO l-d array holding 'one' solution
Y1 l-d, anay holding 'the other' solution
YS l-d array holding interpolated (final) solution
i ; BO Value of Y0 at x: B
BI Valueof Yl at x:B
BS Value of YS at i(: B

The fourth order Runge-Kuttamethodis usedto solvethe initial value problemsusing


subroutinenuNcl listedat theendof themainprogram.If theinitial gradientsA0 and A I chosen
as data,leadto valuesof y(B)whichdo not lie on eithersideof the correctboundaryvalue,the
programstopsandnewvaluesshouldbetried.Unlessinformationis availableto suggestsuitable
startingvaluesfor y'(,4),the selectionof A0 and Al is a trial and error process.
To illustrate use of the program,the following problem is to be solved: given that
y": -4y')'ly with y(0): l; y(l):\solve for y in therange0<x< 1 usinga shootingmethod.
The secondorder equationhas beenreducedto two first order equationsleadingto the
followinginitial valueproblems:

1(01trI
z(01:1
)'
S O L U T I O N SO F O R D I N A R YD I F F E R E N T I A E
LOUATIONS 261

t':z {0): I
a-2
z(0):3
I

The two initial gradients input as A0 and A1, corresponding to z(0), have arbitrarily been
given the values 1 and 3 respcctively.Input and output to the program are shown in Figs 7.19(a)
and (b) respectively.The convergedsolution was obtained after 5 iterations with a tolerance of
0.0001.A step length of ft:0.1 was used to obtain the solutions which agree well with the exact
solution given by
y:(7x+ 1)t/3.

Number and NSTEPS H


size o[ steps l0 0.1
Boundary values XA YA XB YB
0.0 1.0 1.0 2.0
Trial gradients AO AI
1.0 3.0
Convergence TOL ITS
Tolerance 0.0001 25
Fig. 7.19(a) Data for Program7.5

*r****** SHooTING I{ETHODS FOR *r*******


******** NONLINEAR BVPilS USING ********
******** L T N E A R T N T E R P O L A T I O N* * * r * * * * *

x Y
. oooooE+oo . 1 0 0 0 0 8 + 0 1
.100008+00 .11932E+01
. 2 O 0 O O E + O O . 13 387E+01
- 3000oE+oo .1{5798+01
.40000E+00 .1560{E+01
.500008+oo .15509E+O1
.60000E+oo .17324E+O1
. 7000oE+oo .18070E+01
.80000E+00 .187588+01
.9000oE+oo .193998+O1
. 100008+01 .20000E+o1

AFTER 5 TTERATIONS
Fig. 7.19(b) Resultsfrom Program7.5

7.4.3 Weightedresidualmethods

Thesemethodshavebeenthe subjectof a greatdeal of researchin recentyears.The


methodscanform the basisof thefinite elementmethodwhichis now the most widely
usednumericalmethodfor the solution of large boundaryvalueproblems.
The startingpoint for weightedresidualmethodsis to'guess' a solution to the
differentialequationwhich satisfiesthe boundaryconditions.This'trial solution'will
contain certain parameterswhich can be adjusted to minimise the errors. Several
differentmethodsareavailablefor minimisingtheerror or'residual',suchthat the trial
solutionis as closeto the exactsolutionas possible.
262 CHAPTER 7

Considerthe secondorder boundaryvalueproblem

l" :.f (l', l, x) (7.1M)

with y{,A):y^ and y(.B):yo

The differential equation can be rearranged as

R:r" -f(y', y,x) (7.105)

where R is the 'residual' of the equation. Only the exact solution 1l(x)will satisfy the
boundary conditions and causeR to equal zero for all x.
Consider a trial solution f(x) of the form

: r(x) + Crt r@)+ Cfli 2g)+ ...


@(x) (7.106)

where 6V):y^ Q@):ya and F(x) can be zero iI required.

The trial solution, which is made up of trial functions rltr,tltr, etc., satisfies the
boundary conditions of the differential equation, and contains a number of 'undeter-
mined parameters'Cr,Cr, etc. The trial solution (7.106)is differentiatedtwice, and
substituted inro (7.105)to give

R:Q" -_f(Q' ,6,x) (7.t07)


b
r l
We wish to find the valuesof Cr, C2,etc.,which minimiseR in the solution domain
A<xSB. Various methodsfor minimising R will be described.
1 Clearly the choice of a suitable trial solution is crucial to the whole process.
,
Frequently the trial functions * r,*r, etc.,are simple polynomials, although transcen-
Er dental functions such as sine,cosine,log. etc.,could also be used.Sometimcsthe physics
r l
h; of the problem points to a 'sensible'choice of trial functions.
There is no limit to the number of undeterminedparametersthat may be included in
the trial solution. The more undetermined parameters,the more accurate the final
solution, but the more work that must be done in order to find their valucs.

Example7.13

Given the boundaryvalueproblem

y":3x+4y subjectto boundaryconditions


]/(o):0,11): I

obtain an expressionfor the residualusing a trial solution with one undetermined


parameter.
S O L U T I O N SO F O R D I N A R YD I F F E R E N T I A E
L QUATIONS

Solution7.13
the boundarycondition,wecould derive
In order to find a trial solutionwhichsatisfies
a Lagrangianpolynomial(seeSection5.2.1)which passesthrough the points

x r
0 0
0.5 g

I 1

The valueof thepolynomialat x:0.5 remainsvariableat this stageand will be related


parameter.
to our singleundetermined The resultingpolynomialis of secondorder,and
given by

6G):r'(z-4a)-x(l -4a)

which can be rearrangedin the generalform given by eq.7.106to give

6&):F(x)+ Crtr$)
where F(x):2rz - t
Ct': -4a
*'(xl:x2 -x

In order to find the residualof the equation,rearrangeit thus

R:1" -3x-4y

and differe4tiatethe trial solution twice to give

d' :2x(2 - 4a)- (l - 4a)


i.e.

6":2(2-4a)

The trial solutionand its derivativesarenow substitutedinto the differentialequation


replacingy, y' andl" by Q,$' and {". In this examplethereis no first derivative,but
after rearrangement and substitutionfor C1 we get

R : - 4 x 2 ( 2 +C r ) + x ( l + 4 C ) + 2 . ( 2 + c )

Example7.14

Obtain a trial solutionto the problemof Example7.13involving two undetermined


parameters.
C H A P T E R7

Solution 7.14

Derive a Lagrangian polynomial which passesthrough the following points

Y
v
0 0
I
3 a
1
3 b
I I

The values of the polynomial at x : { and I : } remain variable at this stageand will be
related to our two undetermined parameters.
The resulting polynomial is cubic, and given by

6 $ ) : \ l x 3 1 2 t a - 2 7 b+ 9 ) - x 2 ( 4 5 a - 3 6+b9 ) + x ( l 8 c- 9 b+ 2 ) l
which can be rearrangedin the generalform given by eq.7.106,i.e.

Q$): F(x)+ C1! {x) + Crt r@)


where
F(x) :|(9x3 -9x2 +2x1
Cr:!(a-b)
rlt,(x): xt - x'
Cr: -](2a-b)
* '(x): x' - x

Four of the best-knownmethodsfor minimisingthe residualR are now covered


briefly.In eachcase,the trial solution and residualfrom Example7.13,i.e.

$(x) : (2x2- x) + C r(xz- x) (7.108)


R : - 4x2(2+c, ) + x(l + 4c ) + 2(2+ c t) (7.r0e)
involving one undeterminedparameterare operatedon.

7.4.3.1Collocation

In this method,the residualis madeequalto zaro at as many pointsin the solution


domain as thereare unknown C;'s.Hence,if we haven undetermined parameters,we
write
R(x,):9, i:1,2,...,n (7.110)
where xr , x2, . .., xn are n points within the solution domain. This leads to r simultan-
eous equations in the unknown C;'s.
S O L U T I O N SO F O R D I N A R YD I F F E R E N T I A L
EOUATIONS 265

Example 7.15

Use collocation to find a solution to the differential equation of Example 7.13.

Solution7.15

Usingthe trial solutionand residualfrom SolutionT.l3we haveone undetermined


parameterC1.It seemslogicalto collocateat the midpoint of the solutiondomain,
x:0.5, hence
R(0.5): -4{0.s)' (2+ C, ) +0.5(r+ 4C) + 42 +C, ) :0
whichgivesCt: -2.
. The trial solution is thereforegivenby
g(x):(2x2 -x)-f(x' -x)

hence
x
6$):6(7x- l)

Note: If our trial solution had two undetermined parameters such as that given in
Example 7.l4,we could have collocated at x: * and x:3 to give two equations in the
unknown C, and C2.

7.43.2 Subdomain
In this method,the solutiondomainis split into as many 'subdomains'or partsof the
domainasthereareunknownCis. We thenintegratetheresidualovereachsubdomain
and set the resultto zero.Hence,for n undeterminedparameterswe have

fr' 1,, fr"


I na":0,
Jto
I n a x : 0 , . . . , JIt . - ' n d x : o
Jt'
( 7 . 1I 1 )

where the solution domain is in the range

xo (x (xn

and x1 Xz...xn-, are points within that range.


This leads to n simultaneous equations in the unknown Cis.

Example7.16

Use the subdomainmethodto find a solutionto the differentialequationof Example


7.13.
C H A P T E R7

Solution7.16
Using the trial solutionand residualfrom Solution7.13we haveone undetermined
parameterC1. In this casewe requireonly one'subdomain'whichis the solution
domain itself,hence
fr
1z+c r)+Ix'$ + qc) +2x(2+c,)li
I n ax: 1- gx3 :0
which givesCt: -lI
The trial solutionis thereforegivenby

$(x) : (2x2-x) - td(x' - x)


- x
hence Q$):- (Zlx - 5)
. lo

Note: If our trial solution had two undeterminedparameterssuchas that givenin


Example7.14,we would have neededto integrateover two subdomains,e.g.
fo.s f|
I Rdx:0 and I nax:O
Jo Jo.s
to give two equations in the unknown C, and C2.

7.43.3 Least squares

In this method we integrate the squareof the residualover the full solution domain. We
then differentiatewith resp€ctto each of the undeterminedparametersin turn, and set
the result to zero. This has the effect of minimising the integral of the square of the
residual.
In generalwe write
$; a fB - fB aR
d x : 2 J rR A d x : o r o ri : 1 , 2 , . . . , n (7.112)
*, )^R2
wherethe solutiondomainis givenby A<x<8.
Hencefor n undeterminedparameters,we have
fB aR lB aR fB AR
lR#dx:0,
J,t QLr
l R fo,Ldz x : 0 , . . . , l n l OaCxn : O (7.1l3)
Jt )t
which leadsto n simultaneousequationsin the unknown C,'s.

Example7.17

Usetheleastsquaresmethodto find a solutionto thedifferentialequationof Example


t. r3.
S O L U T I O N SO F O R D I N A R YD I F F E R E N T I A E
L OUATIONS 267

Solution7.17
Using the trial solutionand residualfrom Solution7.13we haveone undetermined
parameterC1. Henceset
f l ^ D

I nildx:o
dcr
Jo

fr
i.e. I [ - q * ' Q + c , ) + x ( l + 4 c t ) + 2 ( 2 + c r ) ]( - 4 t ' + 4 x + 2 ) d x : 0
Jo

which givesCr:i$f
The trial solutionis thereforegivenby
- x)
Qg) : (2x2- x) - itt(x'
x
hence,0(u-):i(35x-8)

Note: If our trial solutionhad two undeterminedparameterssuchas that given in


Example7.14,we would havewritten
fL AD fl Ap
I n l :a ':o and I P- - ) dx:o
Jo OCr oCz
Jo

to givc two cquations in the unknown C1 and C2.

7.43.4 Galerkin'smethod

In this method,whichis themostpopularin finiteelementapplicationswe'weight'the


and set the integralsto zero.
residualby the trial functionsry'r(x),rltr(x),...,ry'n(x),
Hencefor n undeterminedparameters, we have
lB lB fa
(7-rr4)
l^/,(*)dx:0, l n r / ' ( r ) d x : 0 , . . . , JlAn r 1 l " ( r ) d x : 0
Jt J,t

wherc the solution domain is given by A<xS8. This leads to n simultaneous


equationsin the unknown Cis.

Example7.18
UseGalerkin'smethodto find a solutionto thediflerentialequationof Example7.13.

Solution7.18

Using the trial solutionand residualfrom Solution 7.13we haveone undetermined


C H A P T E R7

parameter C'. Hence set

t ^
I R / , ( r )d x : 0
Jo
i.e.

t '
-x)dx:0
I l - q r ' Q * c 1 ) + x ( l+ 4 c ) + 2 ( 2 + c 1 ) l ( x '
Jo
w h i c h g i v e sC r : - l

The trial solution is therefore given by

Qg):(2x2-x)-/(x2-x)
x
f{x):tr(5x- l)

Note: If our trial solution had two undetermined parameters,such as that given in
Example 7.14,we would have written

ft rl
lnry',(r)dx:o and lnpr(r)dx:o
JO JO

to give two equations in the unknown C1 and C2.

7.43.5 Concluding remarks on weighted residual procedures

The problem considered in the preceding sectionswas

y":3x*4y with ),(0):0 and J'(1): I (7.r15)


and a summaryof the trial solutionsusingone undetermined
parameteris asfollows:

Collocation l:[{lx-tl
; ;
Subdomain y:f6{Zt*-S)

Leastsquares y:
]OS*-tl (7.116)
Galerkin t:f,{St- t)
7(e2'-e-2') 3
Exact r r - -
t- - - Y
-e-r)
41sz 4'
The error associatedwith eachof the trial solutionsis shownin Fig. 7.20.lt is clear
that for this example,no single method emergesas the 'best'.Indeedcach of the
methodsis the'most accurate'atsomepoint within the solutiondomain0<x<1.
For simpleproblemssuchas the onedemonstrated here,the collocationmethodis
the easiestto apply,because it doesnot requireany integration.For finite element
S O L U T I O N SO F O R D I N A R YD I F F E R E N T I AELO U A T I O N S 269

q -y

0.05
0.04
0.03
o.o2
0.01
0
-o.01
4.O2
-{.03
-{.04
-{.05
-o.06

a
Fig. 7.20 Error due to variousweightedresidualmethods

methods however, the Galerkin method is most often employed, becauseit can be
shown that use of the trial functions (or shapefunctions) as 'weighting' terms leads to
desirable properties such as symmetry of simultaneous equation coefficient matrices.
For comparison, the trial solution with two undetermined parametersdevelopedin
Example 7.14, has been derived using two-point collocation. It is left to the reader to
show that the resulting trial solution becomes

088):+(9xt -9x2 + 2x) - 3.483881x3


- x2)+ 0.70348(x2
- x) (7 -117)

leading to considerably reduced errors as shown in Fig. 7.20.


All the four methods describedin this section for optimising the accuracy of a trial
solution employ the philosophy that weighted averagesof the residual should vanish.
The difference between methods therefore,lies only in the way the residual is weighted.
In general, if we have n undetermined parameters,we set

r W{x)Rdx:0 i : 1 , 2 , . . .n,

where the solution domain is.4<x<.B. In the previous exampleswith one undeter-
(7.1l 8)

mined parameter, the weighting t€rm W; was of the following form:

( w,&\:o x*)
Collocation (
I tY,(r):1 *:L
Subdomain Wyg):r (7.1
re)
kast squares W { x ) : - 4 x 2+ 4 x + 2
Galerkin W1(x):f -1

w h e r e t h e s o l u t i o n d o m a i n w a s i n t h e r a n g e0 < x < 1 .
These weighting functions are summarisedin Fig.7.21.
C H A P T E R7

squares

\ \

n-l

Fig. 7.21 Weightingfunctions

7.5 Exercises
1 Given l':xysiny with },(l):l; estimatey(1.5)using Euler's method. L,ct (a) h:0.25,

h
I
t
( b )f t : 0 . 1 .
Answer:(a) 1.56a3,(b) 1.6963

( 2 Given y:1x2+y2)ll with ),(l):l cstimate{0.5) usingthe modified Euler methodwith


i (a)h: -0.1 and (b) fi: -0.05.
l Answer:(a) 0.1869(b) 0.1923
Fr
l t 3 Rework Exercise2 using the midpoint rule.
Answer:(a) 0.1952O) 0.1943

4 ReworkExcrcise2 usingthe fourth ordcr Runge-Kuttamethodwith two stepsof -0.25.


Answer:0.1941

Use Euler'smethod to solve numericallyeach of the following:


(a) y':y, ),(0):1 find (0.2) usingfi: 0.05
{b) l:x+y, r,(0):0 find 10.5) usingl'r: 0.1
(c) y':3x-y,),(l):0 find y(0.5)usingft: -0.1
Answer:(a) 1.2155,(b) 0.1105,(c) - 1.5
Exact 1.2214, 0.1487 - t.5

Sincethe cquationsin Excrcisc5 are lincar,rcwork them usinga'0-method'with 9:*.


Answer:(a) 1.2215,(b) 0.1494,(c) - 1.5

Given y' : (x + y)2 with d0): l, estimate(0.2) using a suitable numericalmethod.


S O L U T I O N SO F O R D I N A R YD I F F E R E N T I AELO U A i I O h S :11

Answer 1.3085with fourth order Runge-Kutta(h:O.z)


Exact 1.3085

Given the equation


y':x+y+xY+l with (0):2
estimatcy(0.4)usingthe modifiedEuler methodwith steplengths0.1and 0.2,and usetheseto
make a further, imProvedestimate.
Answer:3.8428,3'8277,3.847 8

Giventhe equation
t :l - x
lt + (v)'l't'
with {0):y'(0):0, cstimatey(0.5)usingthe midpoint rule with h:0.25 and fr:0.05.
a
Answer:0.1 124,0.1087

l0 Giventhe equation
v " + v + ( y ' ) 2= 0
subjecttoinitiatconditions)'(0):-l,y'(0):lestimatel0'5)andy'(0'5)usingthefourth
order Runge-Kuttamethodwith a steplengthof h:O'25'
Answer:-0.5164, 0.9090

ll Given the equation


y" -21 I Y:21 with dl)=0,.Y'(l):0
find l0.a) using a suitable numericalmethod.
Answer:0.1900, fourth order Runge-Kutta(ft: -0'15)

1 2 The angular accelcrationof a pendulum is given by

d: -9sin 0
L
withinitialconditionAQl:rA,d(o):o'IfL:l0musethefourthorderRunge-Kutta
method to find the angular position of the rod after 0.5s.
Answer:0.7 (h:0.25)

l 3 Given the equation

t" *y:x with l0):1 and Y'(0):0'


flnd y(0.5)using a suitable numerical method.
Answer: 0.8982,fourth order Runge-Kutta(fi:0'25)

l 4 Use the modificd Euler method to estimatey'(0'2) for thc equation'


y": -xly' with l0):y'(0):1.
Answer:0.9824([:0.05)

l 5 Given the equation


y" -2xy'+(3+x')Y:0
272 CHAPTER
7

with {0):2,y'(0) :0, estimate10.1) using one step of the fourth order Runge-Kutta
method.
Answer:0.1970

l6 The differential equation

/':secx+ytanx h a s t h e f o l l o w i n gs o l u t i o n si n t h e r a n g e0 < x < 0 . 5 .


x v
0 0
0.1 0.1005
n 1 0.204t
0.3 0.3140
0.4 0.4343
0.5 0.5697
continue the solution to estimate y(0.7) using the Adams-Bashforth-Moulton predictor-
corre&or method.
Answer: il0.6):9.7279, (0.7):6.9153 with l0-a tolerance

t 7 Rework Exercise 16 using the Milne-Simpson predictor-corrector method.


Answer:)40.6):0.7270, {0.7):9.9152 with 10-a tolerance

l 8 Use polynomial substitution to derive the weights in the predictor formula

Ia+l:-)/a-3-t L wili
a=n-2

where the sampling points are equally spaced, distance h apart.


Use the formula to estimate y(0.4) given the differential equation:
y': tan(ry)

and the initial values

r ( .v
0 0.1
. , 0.1 0.1005
0.2 0.1020
0.3 0.1046
Answer - 0.1083
: Adams-Bashforthpredictor),(0.4)

l 9 Given the boundaryvalueproblem


2 2
y :-y *5}J-sln x
x x -

with ),(l): l, )421:2, use a shooting method to esrimate y(1.5).


Answer: 13492

20 Given the nonlinear boundary value problem


2 y y "- ( y ' ) 2 + 4 y 2 : 0
274 C H A P T E R7

Nore: Use a program from Chapter 2 to solve the equations.


Answer:10.2):1.220, ^0.4):1.471, A0.6):1.743, ),(0.8):2.025,t(1.0):2.307

26 Estimate the value of the cantilever tip deflection from Exercise 22 using a method of
weighted residuals with the trial solution

l:Ctx2 1C2x3.
Answer:All methods give exact solution 0.333.

27 Given the equation

l" *n2Y:a
subjectto boundary condition l0) : l, ),(1): - 0.8987,estimate10.5) using the trial solution

y:1- 1 . 8 9 8 7 x +| c,x'11-x) with n:2


i= I
a
Use (a) collocation and (b) Galerkin.
Answer: (a) 0.0508,(b) 0.M95

28 Apply the Galerkin method to

t" + Ay:g with i,(0):11):0


using the trial solution
y: Cr x(l - x) + Crx2(l - x)2

and hence estimate the smallest eisenvalue ,1.


Answer: 9.86975

29 Rework Exercise 28 using finite differenceswith a step length of


(a) * and (b) 1.

It may be assumedthat y will be symmetrical about the point x:|


Answer: (a) 9, (b) 9-37

7.6 Further reading


Burden, R.L. and Faires, J.D. (1985). Numerical Atulysis,3rd cdn, Prindle, Weber and Schmidt, Boston,
Mass.
Collata L. (1966). The Nwnerical Treatment of Diferential Equations,3rd edn, Springer-Verlag New York.
Conte, S.D. and de Boor, C. (1980). Eletnentary Numerical Atnlysis, McGraw-Hill, New York.
Crandall, S.H. (1956). Engineering Analysis, McGraw-Hill, New York.
Dahlquist, G. and Bjorck, A. (1974). Nutrwrical Methods, Prentice-Hall, Englewood Cliffs, New Jersey.
Gear, C.W. (1971). Numerbal Initial Value Problems in Ordinary Diferentia! Equations, Prentice-Hall,
Englewood Cliffs, New Jersey.
Gladwell, I. and Sayers, D. (eds) (1980). Computatioml Techniques for Ordinary Diferential Equations,
Academic Press, New York.
Grove, W.E. (1966).'Brief Numerical Methoils, Prentice-Hall, Englewood Cliffs, New Jersey.
Hall, G. and Watt, J. (eds) (19i6). Modern Numerical Methods for Ordinary Diferential Equations, Oxford
University Press.
Hamming, R.W. (1959). Stable predictor--corrector mcthods for ordinary differential equations, Journal of
the Associationof Computer Machinery, Vol. 6, No. l, pp. 3747.
S O L U T I O N SO F O R D I N A R YD I F F E R E N T I AELO U A T T O N S

Johnson, L.W. and Riess, R.D. (1982). Numerical Analysis,3rd edn, Addison-Wesley, Reading Mass.
Lapidus, L. and Seinfeld,J. (1971).Numerical Solution of Ordinary Diflerential Equations,Academic Press,
New York.
Ralston, A. (1965). A First Course in Numerical Analysis, McCraw-Hill, New York.
Shoup, T.E. (1979). A Practical Guide to Computer Methods for Engineers, Prentice-Hall, Englewood Cliffs,
New Jersey.
Strang, G. and Fix, G- (1973).An Analysisof the Finite Element Method, Prentice-Hall, Englewood Cliffs.

You might also like