Numercal Smulaton of Space Plasmas (I [AP-4036] Lecture 2 by Lng-Hsao Lyu March, 2018 Lecture 2: Numercal Methods for Dfferentatons and Integratons As we have dscussed n Lecture 1 that numercal smulaton s a set of carefully planed numercal schemes to solve an ntal value problem numercally. followng three types of dfferental equatons. dy(t dt dy(t dt y(x,t t Let us consder the f (t (2.1 f ( y,t (2.2 f (t, y, y 2 x, y x,..., 2 y,... (2.3 The numercal methods for tme ntegraton of these equatons wll be dscussed n the Lecture 3. Before we conduct the tme ntegraton, we need to determne the dfferentatons y x, 2 y,... and the ntegratons 2 x y,... on the rght-hand sde of the equaton (2.3 at each grd pont. In ths secton, we are gong to dscuss the followng three types of numercal methods, whch are commonly used n spatal dfferentatons and ntegratons. 1. Fnte Dfferences (based on Taylor seres expanson 2. FFT (Fast Fourer Transform 3. Cubc Splne 2.1. Fnte Dfferences For convenence, we shall use the followng notaton n the rest of ths lecture notes. For a gven tabulate functon f jk n f (x Δx, y jδy, z kδz, t nδt f (x, y j,z k,t n : 1 2... N x : x 1 x 2... x N f : f 1 f 2... f N the fnte-dfference expresson of the n-th order dervatves of the gven functon f can be obtaned from the Taylor seres expresson of f. Table 2.1 lsts examples of 2-1
Numercal Smulaton of Space Plasmas (I [AP-4036] Lecture 2 by Lng-Hsao Lyu March, 2018 fnte-dfference expresson of [d f / ] xx, [d 2 f / 2 ] xx, and [d 3 f / 3 ] xx, where the central-dfference expressons are of the frst-order accuracy, whle the forward-dfference and backword-dfference expressons are of the zeroth-order accuracy. Complete dervatons of the hgher-order fnte-dfference schemes are gven n the Appen B. Table 2.2 lsts examples of the fnte-dfference expressons of spatal ntegratons y(x x +Δx x f (x. Complete dervatons of the hgher-order fnte-dfference scheme are gven n the Appen C.1. Dervatves Table 2.1. The numercal dfferentatons based on fnte dfference method Central Dfference (Frst-order scheme Forward Dfference (Zeroth-order scheme Backward Dfference (Zeroth-order scheme d f δ f f f +1 1 xx 2Δx Δ f f f +1 Δx f f f 1 Δx d 2 f 2 xx δ 2 f f 2 f + f +1 1 Δ 2 f (Δx 2 f 2 f + f +2 +1 2 f (Δx 2 f 2 f + f 1 2 (Δx 2 d 3 f 3 xx δ 3 f f +2 2 f +1 + 2 f 1 f 2 2(Δx 3 Δ 3 f f +3 3 f +2 + 3 f +1 f (Δx 3 3 f f 3 f 1 + 3 f 2 f 3 (Δx 3 Exercse 2.1. (a Please prove that the central-dfference expressons shown n Table 2.1 are of frst-order accuracy. (b Please prove that the forward-dfference and backward-dfference expressons shown n Table 2.1 are of zeroth-order accuracy. Exercse 2.2. Determne d f /, and d 2 f / 2 of a gven analytcal functon f (x numercally based on the frst-order, the thrd-order, and the ffth-order fnte-dfference expressons lsted n the Appen B. The dfferences between the numercal solutons and the analytcal solutons are called the numercal errors. Determne the numercal errors of a gven grd sze Δx. Show (or plot that the numercal error s a functon of poston and also a functon of Δx. 2-2
Numercal Smulaton of Space Plasmas (I [AP-4036] Lecture 2 by Lng-Hsao Lyu March, 2018 Table 2.2. The spatal ntegratons based on fnte dfference method d y(x f (x, h Δx 1 st order ntegraton + h f + O(h 2 f (1 2 nd order ntegraton Trapezodal rule 4 th order ntegraton Smpson's rule 4 th order ntegraton Smpson's 3/ 8 rule + h f +1 + f 2 + O(h 3 f (2 + h( 1 6 f + 4 6 f +(1/2 + 1 6 f +1 + O(h5 f (4 + h 3 (3 8 f + 9 8 f + 9 + 1 8 f + 3 + 2 8 f +1 +O(h5 f (4 3 3 Exercse 2.3. Use the frst-order, the second-order, and the forth-order ntegraton expressons lsted n Table 2.2 to determne y(x π / 4 wth dy(x / cos(x and boundary condton y(x 0 0. Determne the numercal errors n your results. Compare the numercal errors obtaned from dfferent spatal ntegraton expressons. 2.2. FFT (Fast Fourer Transform A functon can be expanded by a complete set of sne and cosne functons. In the Fast Fourer Transform, the sne and cosne tables are calculated n advance to save the CPU tme of the smulaton. For a perodc functon f, one can use FFT to determne ts spatal dfferentatons and ntegratons,.e., d f FFT 1 {k[fft ( f ]} f FFT 1 { 1 [FFT ( f ]} for k > 0. k Exercse 2.4. Use an FFT subroutne to determne the frst dervatves of a perodc analytcal functon f. Determne the numercal errors n your results. 2-3
Numercal Smulaton of Space Plasmas (I [AP-4036] Lecture 2 by Lng-Hsao Lyu March, 2018 Exercse 2.5. Use an FFT subroutne to determne the frst dervatves of a non-perodc analytcal functon f. Determne the numercal errors n your results. 2.3. Cubc Splne A tabulate functon can be ftted by a set of pece-wse contnuous functons, n whch the frst and the second dervatves of the fttng functons are contnuous at each grd pont. One need to solve a tr-dagonal matrx to determne the pece-wse contnuous cubc splne functons. The nverson of the tr-dagonal matrx depends only on the poston of grd ponts. Thus, for smulatons wth fxed grd ponts, one can evaluate the nverson of the tr-dagonal matrx n advance to save the CPU tme of the smulaton. For a non-perodc functon f, t s good to use the cubc splne method to determne ts spatal dfferentatons and ntegratons at each grd pont. Results of the spatal dfferentatons obtaned from the cubc splne show the same order of accuracy as the results obtaned from the ffth order fnte dfferences scheme. form. The pece-wse contnuous functon n the cubc splne can be wrtten n the followng f x x k+1 f (x +1 +1 + f (x + [a k (x + b k ](x (x +1 2 The constants {a k, b k, for k 1 n 1} are chosen such that the matchng condtons for cubc splne can be fulflled,.e., df 1 x x k and df (x x x k k+1 d 2 f 1 x x k d 2 f x x k+1 2 2 One can obtan the followng two types of recurson formula f 1 + f [2 + 2( 1 ]+ f ( 1 3 f 0 1 + 3 f 0 ( 1 2-4
Numercal Smulaton of Space Plasmas (I [AP-4036] Lecture 2 by Lng-Hsao Lyu March, 2018 f 1 + f [2 + 2( ]+ f (x +1 ( 6 [ f k 1 1 h 0 f 0 1 ] k 1 where f 0 f (x f (x k+1 k and h x k+1 x k+1. k Detal dervatons of the Cubc Splne wth dfferent boundary condtons are gven n Appen A. Exercse 2.6. Use a Cubc Splne subroutne to determne the frst dervatves of an analytcal functon f. Determne the numercal errors n your results. References Hldebrand, F. B., Advanced Calculus for Applcatons, 2 nd edton, Prentce-Hall, Inc., Englewood, Clffs, New Jersey, 1976. Press, W. H., B. P. Flannery, S. A. Teukolsky, and W. T. Vetterlng, Numercal Recpes (n C or n FORTRAN and Pascal, Cambrdge Unversty Press, Cambrdge, 1988. System/360 Scentfc Subroutne Package Verson III, Programmer s Manual, 5 th edton, IBM, New York, 1970. 2-5