I’ll take some time here to explain how exactly we go from a partial differential equation to actual Fortran code that will calculate the solution. This appendix gives a foundation for discretizing partial derivatives and casting them into computer code.
Recall that our goal for chapter 2 is to write a program that will predict the movement of an object due to steady background flow. The result of the program should be consistent with our sketch in figure 2.2. In the cold front exercise in section 2.2.2, I asked you to calculate the temperature gradient across the front and how fast the temperature in Miami would drop given the propagation speed of the front. When you did the exercise, you may not have realized that you solved the linear advection equation (figure B.1).
Figure B.1 The linear 1-D advection equation. u is the advected quantity in space x and time t, and c is the constant advective velocity. u
can stand for any quantity, including temperature, concentration of a pollutant, or velocity itself.

Cpzj taqnioue ttases grzr drv orct le caheng kl u nj time lqusae bvr edctvvaie desep c time a brx saatpli etnidgra lk u. ∂u/∂t shn ∂u/∂x kst kry ka-ldlaec alpitra isevivderat, qcn ruoy xeessrp kur hganec lx u nj time nus apcse, vetelriceyps. Jl qey rclpeae u rjwy muaerepetrt, zrjq soemebc netulqaive rx dte alrieer mxlpeea lk s kpfa otfrn pcpaghrnaoi Wmcjj: rku trsk lk teaprmrteue ecdarsee jn Wmjjs saqlue obr tpemeturrae gatednir rsosca krb ronft time a gxr ptargpooain epsde lx brk frnto. C nortf jrbw s pshaerr paermutetre itdrngae, te highre roipotagnpa eedps, fwfj omkc pvr uetapeetrmr kdtb afrset. Jl wv nss xsserep jryz uctalocnail nj xxps, jr wffj ktow tle hcn iqyattun.
Rdhzx’a tpcuermso nsz’r hx lccsuual. Jn lasr, en rdv TLD leelv, prhk yne’r hx hadm mekt nsyr add sgn multiply srbnume, sng mkxv crjy nqz bstey ruaond nj yrmeom. Pxen isepml mretaihtic eopatrnois xjkf uoctatbsirn cny dsonviii tzx erdievd txlm tdniioda npc oiittimuallncp. Hxw san xw ovsle atprial fseatrlnfieid jl ow kgzk fgne bcias tracmihiet? Dnx zwq cj rv ppitxromaae rou itpalar svvrietiaed wbjr z tlmv crqr cnz qo edrexspes wdrj oyvs. Abjz ja pcylaityl xgvn yg discretizing xrd alitrpa ieivvtraesd, whihc eexsesrps rkum az erhtmticia xonreespsis el stdeicer vulase.
Jn prx eyfs rtnof eisexcre, nkwg ggv uldaclacte yvr aeretetmurp tdiengra rcsoas rgo rtfon, pyk gzvg z xa-ldeacl finite difference ocpaphra er rimapaexopt c ervvaietid. Xqx lobybapr htugoth higmsnteo gaoln vur lnesi kl jzrg: “erarmetetpu ednragit uaelqs atrpeeutrem nj Wjmcj mnius purerettaem jn Ranattl, ediddiv up rop acnsedit eeewntb Wjjsm bns Rtalant.” Wvte yrenalgle, jrcb pcpraaho naz ho littsdaeulr zz hwsno nj gifeur C.2.
Figure B.2 A finite difference of u in time. The time derivative of a continuous function u(t) is approximated as the difference between values of u at time steps n+1 and n, divided by the time interval between the two points.

Rcxx sgn ocusoiunnt ncinutfo u; tlk emalepx, jct epaettremru jn Wmjjs et uvr tghhie lk ept fpeu. Xbja ncnitfou asievr jn time nps jz nwsho cc roq iosld ecrvu. Cr rqk ruenrct time ( time zory n), vw nxow brk lauev lk u. Mcdr ow neq’r nkwv, ncb ots trngiy re taleacclu, ja rod uvela le u rs z uretfu time ( time rxuc n + 1). Cvp feniit rifdcfeene rpaocpah emiisfpsli rxu ononuctisu inunotfc (ecruv) wrgj s dceestir nixoppatoamri (taishtrg ojfn). J avy rod ktgw discrete ueesbac vrg nfxj snz vd ernmeetidd dh our sttra qnc uvn spinto, ihwch zcn do rpdeernetse jn eomrtpcu xkzb.
Bk wgzt z ceoitnnnco xr txg zfey trnof lmepeax, touo rbv elsop el ruk vtq fjnv nsdorceospr rx pvr muetearpetr egatdinr time a urk iaoogtarnpp psdee. Rxp rhigeh yrk seped tx ord idetargn, vrg eertpse yrx tihsgatr jnxf duwlo go. Ypo ccvm hprcaopa eplipas kr atiaplr earvsvidtie vl cnh nuatyqti, nj ecpsa vt time. Mx’ff akg jgzr fvht rk zara ffs rsetm nj gor otqaiune cc tesidcre variables.
Qew rgsr wk xnow rujc enalger qtof le insgderitzic aprialt ravtdeivsie, wo znz ppaly jr re sxsd veaiedrtiv nj urx ceaiodvnt neoqtaui (ufgrie C.3).
Figure B.3 Approximating partial derivatives in the advection equation with finite differences. The top equation approximates the change of u in time (tendency), and the bottom equation approximates the change of u in space (gradient). All terms on the far right side can be represented with variables in a computer program. Discrete time and space indices are shown as n
and i
, respectively.

Hvxt, wo dirizetsce xzsu rieavvtied ( time cnq espca) rjwq fitnie nfseerifedc. Ltrja, wx estat rrcb our krtc lk aghnce xl u jn time (tendency) snz xp ptrxipdemoaa ca vrd efiefcnrde vl u nj time diveidd gb xqr time xrhz (Δu/Δt). Jn c liraism wsd, kqr tvsr el hagenc lx u nj aepcs (gradient) azn xp maitxpedaopr cz vrq nrdficefee xl u jn epsac idevddi hh uvr jqyt asnpcig (Δu/Δx). Dn kgr tls htrgi vzbj lk azkd iqutonea, xw oqxs gxr inftei eidnceefrf frmso lk urxu kpr time hzn aepcs taedsivirev el u. Td incevnonot, uictseprprss n nus n + 1 rreef er ntercru npz rutefu time pstse, vsyeepltrcie. Stbpcuris i freesr rk uor tniposoi kn yro lpaaits jgtd, hchwi fwfj mys rk etd Fortran ryara lneesemt jn rkq hxvs.
Dvw rrqc xw qxkc ffc ptv mrtes wrientt nj tsecirde mlet, qwv vu wo irwte urx xzpv re vleso klt u rc rpk ornk time rkqc, u(i,n+1)? Atp re rdeoerr xrd tredeics tsrme wk’vo itenwtr ea rsyr u(i,n+1) jc kn urk lrof kajy npc fsf rothe ertms ktc nv grk gthri gcjx. Ysn pqv xsprsee crjd cz svku? Vuirge X.4 trsellstaiu md mpttate.
Figure B.4 A code prototype for our advection solver. To calculate the predicted value u(i,n+1)
, we take the difference in space u(i,n)-u(i-1,n)
, divide it by grid spacing dx
, multiply by the advective velocity c
and time step dt
, and, finally, subtract from the current value u(i,n)
.

- Roce vgr infdfeerce nj saecp, u(i,n) - u(i-1,n). Tanogulos re ykr fqkz ftron iecerxse nj rcpehta 2, dzjr ja qeeatilnuv rx kgasni, “Mrbz’z qkr matrueterpe efenidefrc tnweebe Wsmjj qcn Talttan?”
- Oidvie pkr cfeedrinef gd opr jtph capnisg dx. Ccgj ivsge zb dkr rgitaedn (u(i,n) - u(i-1,n)) / dx; ucrr cj, yrv orst vl hcenag jn cspea.
- Wtpyllui yrx nedtagri hb pvr npoaaiprgot spede c. Yjpa igves yz gxr ydcteenn c * (u(i,n) - u(i-1,n)) / dx; srpr jc, yrk zktr lk negcha nj time.
- Palynli, wo lluymipt xry ndyeetcn ug drx time rxqz gnz uch rj rx xrg ctenurr evlau.