Computer Derivations o Numerical Dierentiation Formulae By Jon H. Matews Department o Matematics Caliornia State University Fullerton USA Int. J. o Mat. Education in Sci. and Tec. V No (Marc-April ) pp.8-87. Traditional "pencil and paper" derivations o te numerical dierentiation ormulas or '[x and ''[x ave been done independently as i tere was no connection among te two derivations. Tis new approac gives a parallel development o te ormulas. It requires te solution o a "linear system" tat includes symbolic quantities as coeicients and constants. It is sown ow te power o a computer algebra system suc as Matematica can be used to elegantly solve tis linear system or '[x and ''[x. Te extension to derivations o iger order numerical dierentiation ormulas or te central orward or backward dierences are also presented.. Te tree point central dierence ormulas. Using tree points [x - [x and [x + we give a parallel development o te numerical dierentiation ormulas or '[x and ''[x. Start wit te Taylor series or [x + expanded in powers o o suiciently ig degree so tat a remainder suitable or te derivation is included. Te degree n = will suice. n=; Series[[x +{n} () [x + '[x + ''[x + [x + [x + O[ n+ 5 5 Te Matematica notation or te remainder term is O[ = O[. Te term O[ is removed rom te series wit te command Normal and a Taylor polynomial o degree n= is ormed. s[_=normal[series[[x +{n} () [x + '[x + ''[x + [x + [x 5 Construct two "equations" by setting te series equal to te unction at x = x ±. eqns={s[-==[x - s[==[x +} () { [x - '[x + ''[x - [x + [x == [- + x () [x + '[x + ''[x + [x + [x == [ + x } We ave two equations in te two unknowns '[x and ''[x and all te oter quantities [x [x [x + [x () and [x are considered as constants. Now use te Matematica command "Solve" to solve or '[x and ''[x.
solset=solve[eqns{'[x ''[x } {{ '[x ''[x [- + x - [ + x + [x () [x - [- + x - [ + x + [x - }} Te result is not too easy to read so we can use te commands ExpandAll and Collect to manipulate te ormula. In te print statements we add "+..." to remind us tat we are using truncated ininite series. Te ollowing commands will create a niceer looking printout or te above solution. sol = Collect[ExpandAll[solset [[ ; sol [[ = Togeter[sol [[ ; sol = Collect[ExpandAll[solset [[ ; Print[" '[x = "sol" +..."; Print["''[x = "sol" +..."; - [- + x + [ + x '[x = - [x +... - [x + [- + x + [ + x () ''[x = - [x +... Tus we ave derived te numerical dierentiation ormula or '[x and ''[x and te irst term in te series expansion or te remainder wic involves [x () or [x respectively. Since te "numerical dierentiation ormulae" are "truncated" ininite series we know tat te ellipses "..." means tat tere are ininitely many more term wic are not sown. Wen we do not include te "..." we must evaluate te lowest order derivative in te series or te remainder at te value x = c instead o x = x ten we can "cop o" te ininite series at te term involving [c or () [c respectively. Te Matematica command ReplaceAll is used to accomplis tis task. sol = ReplaceAll[Take[sol (k_) [x -> (k) [c; sol = ReplaceAll[Take[sol (k_) [x -> (k) [c; Print[" '[x = "sol; Print["''[x = "sol; - [- + x + [ + x '[x = - - [x + [- + x + [ + x ''[x = - [c () [c Tereore we ave establised te numerical dierentiation ormulas [x + - [x - '[x = - [c [x - - [x + [x + ''[x = - () [c
And te corresponding ormulas wit te big "O" notation O[ are [x + - [x - '[x = + O[ [x - - [x + [x + ''[x = + O[. Comparison wit te traditional derivations. Te above derivation diers only sligtly rom te traditional derivations wic also start wit te two equations () () [x - = [x - '[x + ''[x - [x + [x +L () [x + = [x + '[x + ''[x + [x + [x +L Wen deriving te numerical dierentiation ormula or '[x te equations () are subtracted and te terms involving ''[x cancel and manipulations are used to solve or '[x and its truncation error term. Te traditional derivation o te numerical dierentiation ormula or ''[x starts wit te same equations (). But tis time te equations are added and te terms involving '[x cancel and manipulations are used to solve or ''[x and its truncation error term. Tis is no "big deal" made about te act tat te starting place is te same. We sall see or te iger order ormulas tat using te same starting place will be te key to successul computer derivations o numerical dierentiation ormulas.. Te ive point central dierence ormulas. Using ive points [x - [x - [x [x + and [x + we can give a parallel development o te numerical dierentiation ormulas or '[x ''[x [x () and [x. Start wit te Taylor series or [x + expanded in powers o o suiciently ig degree so a remainder suitable or te n+ 7 derivation is included. Te degree n = will suice. Since te remainder term is O[ = O[ we will use Matematica's Normal command to remove it rom te series and orm te Taylor polynomial o degree n =. k=; n=k+; s[_ = Normal[Series[[x +{n} () 5 (5) () [x + '[x + ''[x + [x + [x + [x + [x 7
Use ive points and set up te "equations" by setting s[x +k == [x +k or k = -- wic is automated by using Matematica's Table command. Altoug te value k= will produce [x +k==[x wic is te boolean value True it will do no arm wen solving te set o equations and we will see tat te generations o iger order ormulas wic require more complicated sets o equations is more easily automated wit te use o a table. eqns=table[s[j == [x + j {j-kk} { () 5 (5) () [x - '[x + ''[x - [x + [x - [x + [x == [- + x 5 5 () 5 (5) () [x - '[x + ''[x - [x + [x - [x + [x == [- + x 7 True () 5 (5) () [x + '[x + ''[x + [x + [x + [x + [x == [ + x 7 () 5 (5) () [x + '[x + ' '[x + [x + [x + [x + [x == [ + x } 5 5 We can consider tat tese are our equations in te our unknowns '[x ''[x [x (5) and all te oter quantities [x [x [x [x + [x + [x constants. Now use Matematica to solve or '[x ''[x [x () and [x () and [x are () and [x. Since tis requires typing o our derivatives to be solved we will automate tis process too by using a table to construct te "variables." vars = Table[ (k) [x {kk} {'[x ''[x [x () [x } Amazingly te algebra involved in solving te our equations will result in te cancellation o in te odd order derivatives and te term will cancel in te even order derivatives. Tis can be accomplised using Matematica's Solve procedure. solset=solve[eqnsvars {{ '[x ''[x 5 (5) - 5[- + x + [- + x - [ + x + 5 [ + x - [x - 5 [x + 5[- + x - [- + x- [ + x + 5 [ + x - - 8 5 (5) [- + x - [- + x + [ + x - [ + x + [x [x - () [x
() [x }} - - [x - [- + x + [- + x + [ + x - [ + x + ( )[x Te output is not too clear to read so we sall use te ormula manipulation commands ExpandAll and Collect to group te numerical dierentiation portion and ten use te ReplaceAll command to (5) () cange [x and [x to (5) [c and () [c ten a inal use o Togeter will clean up te numerical dierentiation ormula part. All tis will be accomplised in te ollowing tree Matematica commands. I te reader is curious about exactly wat is appening in eac step ten te semi-colons can be deleted and te results o eac operation will be sown. solset [[All = Collect[ExpandAll[solset [[All ; solset [[All = ReplaceAll[solset [[All (i_)[x ->(i)[c; solset [[All = Togeter[solset [[All ; solset [- + x - 8[- + x + 8 [ + x - [ + x (5) {{ '[x + [c - [x - [- + x + [- + x + [ + x - [ + x () ''[x + [c 9 - [- + x + [- + x - [ + x + [ + x (5) [x - [c () [x + [- + x - [- + x - [ + x + [ + x () [x - [c }} Notice tat te ormula or approximating '[x and ''[x ave truncation error terms involving so tey () are numerical dierentiation ormulas o order O[. But te ormula or approximating [x and [x ave truncation error terms involving one o te many surprises in te teory o numerical analysis. so tey are numerical dierentiation ormulas o order O[. Tis is Tereore we ave establised te numerical dierentiation ormulas [x - -8 [x - + 8[ + x - [x + (5) '[x = + [c - [x - + [x - - [x + [x + - [x + ''[x = + 9 - [x - + [x - - [x + + [x + (5) [x = - [c () [x - - [x - + [x - [x + + [x + () [x = - [c. () [c
Tese ormulas can be written wit te big "O" notation O[ and O[ i desired. [x - -8 [x - + 8 [ + x - [x + '[x = + O[ - [x - + [x - - [x + [x + - [x + ''[x = + O[ - [x - + [x - - [x + + [x + [x = + O[ () [x - - [x - + [x - [x + + [x + [x = + O[.. Appendix. Subroutines or generating numerical dierentiation ormulae. Te central dierence ormulae use an odd number o points m = k+ and an even number o n = k equations. Since we want to inclued te remainder terms we need to use series expansions o order n = k+. Te remainder term in tese ormulas all involve even powers o and even and odd derivatives depending on te situation. Also te subroutine requires a replacement o te point were te remainder term is evaluated to be x=c instead o x=x. CentralDi[k_ := Module[{k=k} Clear[jnsx; s[_=normal[series[[x +{k}; eqns=table[s[j == [x + j {j-kk}; vars = Table[ (k) [x {kk}; solset=solve[eqnsvars; solset=mapall[togetersolset; Print[TableForm[solset [[ ;; Now te ormulas or te ive point central dierence ormulae can be generated automatically by typing te ollowing Matematica command: CentralDi[ [- + x - 8[- + x + 8 [ + x - [ + x '[x + - [x - [- + x + [- + x + [ + x ''[x - [- + x + [- + x - [ + x + [ + x [x [x + [- + x - [- + x - [ + x + [ [x - [ (5) + x + () + - x [c (5) - [c 9 () [c () [c
Tese ormulas are in agreement wit our previous derivation. Using te subroutine will automate te more diicult derivations. Te reader is encouraged to use te subroutine and type te command () CentralDi[. Te result will be six equations or '[x up to [x. Once we ave te idea or making te subroutine or te central dierence ormulas all we need to do is modiy te set o equations wit orward step sizes or backward step sizes and we can make subroutines or generating te orward and backward dierence ormulaes. ForwardDi[k_ := Module[{k=k} Clear[ijnsx; s[_=normal[series[[x +{k+}; eqns=table[s[j ==[x + j {jk}; vars = Table[(k)[x {kk}; solset=solve[eqnsvars; solset [[All =Collect[ExpandAll[solset [[All ; solset [[All =ReplaceAll[solset [[All (i_) [x -> (i) [c; solset [[All =Togeter[solset [[All ; Print[TableForm[solset [[ ;; BackwardDi[k_ := Module[{k=k} Clear[jnsx; s[_=normal[series[[x +{k+}; eqns=table[s[-j ==[x -j {jk}; vars = Table[(k)[x {kk}; solset=solve[eqnsvars; solset [[All =Collect[ExpandAll[solset [[All ; solset [[All =ReplaceAll[solset [[All (i_) [ x -> (i) [c; solset [[All =Togeter[solset [[All ; Print[TableForm[solset [[ ; ; Dut to te lengty nature o te ormulas wic are output rom te subroutines we leave it to te reader to experiment wit te subroutines ForwardDi and BackwardDi.
Numerical Metods Using Matlab t Edition Jon H. Matews and Kurtis K. Fink ISBN: --58- Prentice-Hall Inc. Upper Saddle River New Jersey USA ttp://vig.prenall.com/