College o Egeerg ad Computer Scece Mecacal Egeerg Departmet ME 9 Numercal Aalyss Marc 4, 4 Istructor: Larry Caretto Numercal Deretato Itroducto Tese otes provde a basc troducto to umercal deretato usg te- derece grds. Tey cosder te terplay betwee trucato error ad roudo error. Fte-derece grds I a te-derece grd, a rego s subdvded to a set o dscrete pots. Te spacg betwee te pots may be uorm or o-uorm. For example, a grd te x drecto, xm x xmax may be wrtte as ollows. Frst we place a seres o N+ odes umbered rom zero to N ts rego. Te coordate o te rst ode, x equals xm. Te al grd ode, xn = xmax. Te spacg betwee ay two grd odes, x ad x-, as te symbol Δx. Tese relatos are summarzed as equato []. x = xm xn = xmax x x- = Δx [] A o-uorm grd, wt deret spacg betwee deret odes, s llustrated below. --------- ------------ ------------------ ---~ ~------- ---------- ------ x x x x xn- xn- xn For a uorm grd, all values o Δx are te same. I ts case, te uorm grd spacg, a oedmesoal problem s usually gve te symbol. I.e., = x x- or all values o. I ME 9 we wll lmt our cosderato to oe-dmesoal te-derece problems. However, advaced courses cosder multple space dmesos dscussed below I two space dmesos a grd s requred or bot te x ad y, drectos, wc results te ollowg grd ad geometry detos, assumg tat tere are M+ grd odes te y drecto. x = xm xn = xmax x x- = Δx y = ymj ym = ymay yj yj- = Δyj [] For a tree-dmesoal traset problem tere would be our depedet varables: te tree space dmesos, x, y ad z, ad tme. Eac o tese varables would be deed at dscrete pots,.e. x = xm xn = xmax x x- = Δx y = ymj ym = ymay yj yj- = Δyj [] z = zmk zk = zmaz zk zk- = Δzk t = tm tl = tmay t t- = Δt Ay depedet varable suc as u(x,y,z,t) a cotuous represetato would be deed oly at dscrete grd pots a te-derece represetato. Te ollowg otato s used or a oe dmesoal problem. Jacarada Hall 4 Mal Code Poe:N/A Emal: lcaretto@csu.edu 848 Fax: 88.77.7
Numercal metods ME 9, L. S. Caretto, Marc 4, 4 Page x ) [4] k Ts otato ca be exteded to problems more ta oe dmeso cludg traset problems. I te most complex case te otato u x, y, z, t ) ( k u s used to deote te jk ( j k value o te depedet at a partcular pot te rego, (x, yj, zk, t) were te varable s deed. Fte-derece Expressos Derved rom Taylor Seres Te Taylor seres provdes a smple tool or dervg te-derece approxmatos. It also gves a dcato o te error caused by te te derece expresso. Recall tat te Taylor seres or a ucto o oe varable, (x), expaded about some pot x = a, s gve by te te seres, ( x) ( a) d xa ( x a)! d xa! d xa... [5] Te x = a subscrpt o te dervatves reorces te act tat tese dervatves are evaluated at te expaso pot, x = a. We ca wrte te te seres usg a summato otato as ollows: d ( x) []! xa I te equato above, we use te detos o! =! = ad te deto o te zerot dervatve as te ucto tsel. I.e., d / x=a = (a). I te seres s trucated ater some te umber o terms, say m terms, te omtted terms are called te trucato error. Tese omtted terms are also a te seres. Ts s llustrated below. ( x) m d d x a ( - )! m x a! xa Terms used Trucato error [7] I ts equato te secod sum represets te trucato error, εm, rom trucatg te seres ater m terms. d m [8]! m Te teorem o te mea ca be used to sow tat te te-seres trucato error ca be expressed terms o te rst term te trucato error, tat s xa m ( m m d m m )! x [9]
Numercal metods ME 9, L. S. Caretto, Marc 4, 4 Page Here te subscrpt, x = ξ, o te dervatve dcates tat ts dervatve s o loger evaluated at te kow pot x = a, but s to be evaluated at x = ξ, a ukow pot betwee x ad a. Tus, te prce we pay or reducg te te seres or te trucato error to a sgle term s tat we lose te certaty about te pot were te dervatve s evaluated. I prcple, ts would allow us to compute a boud o te error by dg te value o ξ, betwee x ad a, tat made te error computed by equato [9] a maxmum. I practce, we do ot usually kow te exact uctoal orm, (x), let aloe ts (m+) t dervatve. I usg Taylor seres to derve te basc te-derece expressos, we start wt uorm oedmesoal grd spacg. Te derece, Δx, betwee ay two grd pots s te same ad s gve te symbol,. Ts uorm grd ca be expressed as ollows. Δx = x x- = or x = x + or all =,,N [] Varous cremets x at ay pot alog te grd ca be wrtte as ollows: x+ x- = x+ x = x- x = x x+ = x- x+ = x x+ = [] Usg te cremets x deed above ad te otato = (x) te ollowg Taylor seres ca be wrtte usg expaso about te pot x = x to express te values o at some specc grd pots, x+, x-, x+ ad x-. Te covetoal Taylor seres expresso or (x) equato [5] ca be adapted or use te dereces by wrtg a expaso equato about a partcular grd pot, x = x, to determe te value o (x) at aoter grd pot, x+k. From equato [], we see tat x+k = x + k so tat (x+k) = (x + k). Te derece, x, te depedet varable, x, betwee te evaluato pot, x + k, ad te expaso pot, x, s equal to k. Usg a = x as te expaso pot ad k as x allows us to rewrte equato [5] as sow below. ( x k) ( x ) d k d! ( k) d! ( k)... [] xx xx xx Te ext step s to use te otato tat (x + k) = +k, ad te ollowg otato or te t dervatve, evaluated at x = x. d d... xx xx d xx [] Wt tese otatoal cages, te Taylor seres equato [] ca be wrtte as ollows. ( k)! ( k)! k k... [4] Fte-derece expressos or varous dervatves ca be obtaed by wrtg te Taylor seres sow above or deret values o k, combg te results, ad solvg or te dervatve. Te smplest example o ts s to use oly te seres or k =.... [5] We ca rearrage ts equato to solve or te rst dervatve, ; recall tat ts s te rst dervatve at te pot x = x.
Numercal metods ME 9, L. S. Caretto, Marc 4, 4 Page 4... O( ) [] Te rst term to te rgt o te equal sg gves us a smple expresso or te rst dervatve; t s smply te derece te ucto at two pots, (x+) (x), dvded by, wc s te derece x betwee tose two pots. Te remag terms te rst orm o te equato are a te seres. Tat te seres gves us a equato or te error tat we would ave we used te smple te derece expresso to evaluate te rst dervatve. As oted above, we ca replace te te seres or te trucato error by te leadg term tat seres. Remember tat we pay a prce or ts replacemet; we o loger kow te pot at wc te leadg term s to be evaluated. Because o ts we ote wrte te trucato error as sow te secod equato. Here we use a captal o ollowed by te grd sze pareteses. I geeral te grd sze s rased to some power. (Here we ave te rst power o te grd sze, =.) I geeral we would ave te otato, O( ). Ts otato tells us ow te trucato error depeds o te step sze. Ts s a mportat cocept. I te error s proportoal to, cuttg al would cut te error al. I te error s proportoal to, te cuttg te step sze al would reduce te error by ¼. We te trucato error s wrtte wt ts O( ) otato, we call te order o te error. I two calculatos, wt step szes ad, we expect te ollowg relato betwee te trucato errors, ε ad ε or te calculatos. [7] We use te approxmato sg ( ) rater ta te equalty sg ts equato because te error term also cludes a ukow actor o some ger order dervatve, evaluated at some ukow pot te rego. Te approxmato sow equato [7] would be a equalty ts oter actor were te same or bot step szes. Aoter mportat dea about te order o te error s tat a t order te-derece expresso wll gve a exact value or te dervatve o a t order polyomal. Because a Taylor seres s a polyomal seres, t ca represet a polyomal exactly a sucet umber o terms are used. Ts s llustrated urter below. Te expresso or te rst dervatve tat we derved equato [] s sad to ave a rst order error. We ca obta a smlar te derece approxmato by wrtg te geeral seres equato [4] or k = -. Ts gves te ollowg result.... [8] We ca rearrage ts equato to solve or te rst dervatve, ; recall tat ts s te rst dervatve at te pot x = x.... O( ) [9] Here aga, as equato [], we ave a smple te-derece expresso or te rst dervatve tat as a rst-order error. Te expresso equato [] s called a orward derece. It gves a approxmato to te dervatve at pot terms o values at tat pot
Numercal metods ME 9, L. S. Caretto, Marc 4, 4 Page 5 ad pots orward ( te +x drec to) o tat pot. Te expresso equato [9] s called a backwards derece or smlar reasos. A expresso or te rst dervatve tat as a secod-order error ca be oud by subtractg equato [8] rom equato [5]. We ts s doe, terms wt eve powers o cacel gvg. 5... [] Solvg ts equato or te rst dervatve gves te ollowg result. 4... O( ) [] Te te-derece expresso or te rst dervatve equato [] s called a cetral derece. Te pot at wc te dervatve s evaluated, x, s cetral to te two pots (x+ ad x-) at wc te ucto s evaluated. Te cetral derece expresso provdes a ger order (more accurate) expresso or te rst dervatve as compared to te orward or backward dervatves. Tere s oly a small amout o extra work (a dvso by ) gettg ts more accurate result. Because o ter ger accuracy, cetral dereces are usually preerred te derece expressos. Cetral derece expressos are ot possble at te start o ed o a boudary. It s possble to get ger order te derece expressos or suc pots by usg more complex expressos. For example, at te start o a rego, x = x, we ca wrte te Taylor seres equato [4] or te rst two pots rom te boudary, x ad x, expadg aroud te boudary pot, x.... [] () () ()... [] Tese equatos ca be combed to elmate te terms. To start, we multply equato [] by 4 ad subtract t rom equato []. 4 () () Ts equato ca be smpled as ollows ()... 4 ( ) ( ) ( ).. ( ) 4 () 4... [4] We ts equato s solved or te rst dervatve at te start o te rego a secod order accurate expresso s obtaed. 4 4... O( ) [5]
Numercal metods ME 9, L. S. Caretto, Marc 4, 4 Page A smlar equato ca be oud at te ed o te rego, x = xn, by obtag te Taylor seres expasos about te pot x = xn, or te values o (x) at x = xn- ad x = xn-. Ts dervato parallels te dervato used to obta equato [5]. Te result s sow below. N N 4 N N N 4 N N N... O( ) [] Equatos [5] ad [] gve secod-order accurate expressos or te rst dervatve. Te expresso equato [5] s a orward derece; te oe equato [] s a backwards derece. Te evaluato o tree expressos or te rst dervatve s sow Table -. Tese are () te secod-order, cetral-derece expresso rom equato [], () te rst-order, orwardderece rom equato [], ad () te secod-order, orward-derece rom equato [5]. Te rst dervatve s evaluated or (x) = e x. For ts ucto, te rst dervatve, d/ = e x. Sce we kow te exact value o te rst dervatve, we ca calculate te error te te derece results. I Table, te results are computed or tree deret step szes: =.4, =. ad =.. Te table also sows te rato o te error as te step sze s caged. Te ext-to-last colum sows te rato o te error or =.4 to te error or =.. Te al colum sows te rato o te error or =. to te error or =.. Table Tests o Fte-Derece Formulae to Compute te Frst Dervatve (x) = exp(x) x x x x x x x Results usg secod-order cetral dereces..8.8.7.8.8.7.4.8.55.55.44.49.9.7 4..9.459.459.47.4.47.4 4...78.78.794.7.74.8.78.45 4. 4...4.4.4..9.5 4.....4..57.55 4...9.9.754..4 4.55 4.55 Results usg rst-order orward dereces..8.8.44.48.7.95.9.94.5.7.7.8.8.47.4.9.55.79.4.5.7.8.55.55.74.59.47.8.4.5.5.7.9.459.459.4.54.78..588.7.5.7..78.78.4.4.9.99.8588.4.5.7..4.4.57.5.595.55.7....754.55.498.77.7..9.9.859.897.4 4.55 4.55
Numercal metods ME 9, L. S. Caretto, Marc 4, 4 Page 7 Table Tests o Fte-Derece Formulae to Compute te Frst Dervatve (x) = exp(x) x (x) =.4 =. =. Error Ratos Exact (x) (=.4)/ (=.)/ (x) Error (x) Error (x) Error (=.) (=.) Results usg secod-order orward dereces..8.8.895.7.798.8.85. 4.9 4..7.8.8.985..5.7 4..8.55.55.9.4.75.8 4..9.459.459.44.8.458.88 4...78.78.7.4.785.98 4...4.4.994.8....8.9..9.9.4 4.55 4.55 For te secod-order ormulae, te error ratos te last two colums o Table - are about 4, sowg tat te secod-order error creases by a actor o 4 as te step sze s doubled. For te rst order expresso, tese ratos are about. Ts sows tat te error creases by te same actor as te step sze or te rst order expressos. Te expected values o te error ratos are oly obtaed te lmt o very small step szes. We see tat te values te last colum o ts table (were te actual values o are smaller ta tey are te ext-to-last colum) are closer to te deal error rato. Trucato errors are ot te oly kd o error tat we ecouter te derece expressos. As te step szes get very small te terms te umerator o te te derece expressos become very close to eac oter. We lose sgcat gure we we do te subtracto. For example, cosder te prevous problem o dg te umercal dervatve o (x) = e x. Pck x = as te pot were we wat to evaluate te dervatve. Wt =. we ave te ollowg data or calculatg te dervatve by te cetral-derece ormula equato []. ( x) ( x ) ( x ).4.785.785 (.) Sce te rst dervatve o e x s e x, te correct value o te dervatve at x = s e =.788; so te error ts value o te rst dervatve s 4.5x -. For =., te umercal value o te rst dervatve s oud as ollows. ( x) ( x ) ( x ).78557.789.788899 (.) Here, te error s 4.5x -9. Ts looks lke our secod-order error. We cut te step sze by a actor o, ad our error decreased by a actor o,,, as we would expect or a secod order error. We are startg to see potetal problems te subtracto o te two umbers te umerator. Because te rst our dgts are te same, we ave lost our sgcat gures dog ts subtracto. Wat appes we decrease by a actor o, aga? Here s te result or = -7. ( x) ( x ) ( x ).788874.7885588.78857 (.)
Error Numercal metods ME 9, L. S. Caretto, Marc 4, 4 Page 8 Our trucato aalyss leads us to expect aoter actor o oe mllo te error reducto as we decrease te step sze by,. Ts sould gve us a error o 4.5x -5. However, we d tat te actual error s 5.9x -9. We see te reaso or ts te umerator o te te derece expresso. As te derece betwee (x+) ad (x-) srks, we are takg te derece o early equal umbers. Ts kd o error s called roudo error because t results rom te ecessty o a computer to roud o real umbers to some te sze. (Tese calculatos were doe wt a excel spreadseet wc as about 5 sgcat gures.) Fgure - sows te eect o step sze o error or a large rage o step szes. For te large step szes to te rgt o Fgure -, te plot o error versus step sze appears to be a stragt le o ts log-log plot. Ts s cosstet wt equato [7]. I we take logs o bot sdes o tat equato ad solve or, we get te ollowg result. log log log( ) log( ) log( ) log( ) [7] Equato [7] sows tat te order o te error s just te slope o a log(error) versus log() plot. I we take te slope o te stragt-le rego o te rgt o Fgure -, we get a value o approxmately two or te slope, cormg te secod order error or te cetral derece expresso tat we are usg ere. However, we also see tat as te step sze reaces about -5, te error starts to level o ad te crease. At very small step szes te umerator o te te-derece expresso becomes zero o a computer ad te error s just te exact value o te dervatve. Fgure. Eect o Step Sze o Error.E+.E+.E-.E-.E-.E-4.E-5.E-.E-7.E-8.E-9.E-.E-.E-8.E-.E-4.E-.E-.E-8.E-.E-4.E-.E+ Step Sze
Numercal metods ME 9, L. S. Caretto, Marc 4, 4 Page 9 Fal Observatos o Fte-Derece Expressos rom Taylor Seres Te otes above ave ocused o te geeral approac to te dervato o te-derece expressos usg Taylor seres. Suc dervatos lead to a expresso or te trucato error. Tat error s due to omttg te ger order terms te Taylor seres. We ave caracterzed tat trucato error by te power or order o te step sze te rst term tat s trucated. Te trucato error s a mportat actor te accuracy o te results. However, we also saw tat very small step szes lead to roudo errors tat ca be eve larger ta trucato errors. Te use o Taylor seres to derve te derece expressos ca be exteded to ger order dervatves ad expressos tat are more complex, but ave a ger order trucato error. Te cetral-derece expresso or te secod dervatve ca be oud by addg equatos [5] ad [8]. 4... 4 [8] We ca solve ts equato to obta a te-derece expresso or te secod dervatve.... O( ) [9] Altoug we ave bee dervg expressos ere or ordary dervatves, we wll apply te same expressos to partal dervatves. For example, te expresso equato [9] or te secod dervatve could represet d / or / x. Te Taylor seres we ave bee usg ere ave cosdered x as te depedet varable. However, tese expressos ca be appled to ay coordate drecto or tme. Altoug we ave used Taylor seres to derve te te-derece expressos, tey could also be derved rom terpolatg polyomals. I ts approac, oe uses umercal metods or developg polyomal approxmatos to uctos, te takes te dervatves o te approxmatg polyomals to approxmate te dervatves o te uctos. A te-derece expresso wt a t order error tat gves te value o ay quatty sould be able to represet te gve quatty exactly or a t order polyomal. * Te expressos tat we ave cosdered are or costat step sze. It s also possble to wrte te Taylor seres or varable step sze ad derve te derece expressos wt varable step szes. Suc expressos ave lower-order trucato error terms or te same amout o work computg te te derece expresso. Altoug accuracy tells us tat we sould ormally preer cetral-derece expressos or dervatves, some specal applcatos use oe-sded dereces. Te ma example o ts s * I a secod order polyomal s wrtte as y = a + bx + cx ; ts rst dervatve at a pot x = x s gve by te ollowg equato: [dy/]x=x = b + cx. I we use te secod-order cetral-derece expresso equato [] to evaluate te rst dervatve, we get te same result as sow below: dy y( x ) y( x ) a b( x ) c( x ) [ a b( x ) c( x ) ] x x b c( x x ) c( x x ) b 4cx b cx
Numercal metods ME 9, L. S. Caretto, Marc 4, 4 Page suc as covecto terms computatoal lud dyamcs were oe-sded dereces, called upwd dereces, are used. I solvg deretal equatos by te-derece metods, te deretal equato s replaced by ts te derece equvalet at eac ode. Ts gves a set o smultaeous algebrac equatos tat are solved or te values o te depedet varable at eac grd pot. Fte derece expressos ca be derved rom Taylor seres. Ts approac leads to a expresso or te trucato error tat provdes us wt kowledge o ow ts error depeds o te step sze. Ts s called te order o te error. I te-derece approaces, we eed to be cocered about bot trucato errors ad roudo errors. Roudo errors were more o a cocer earler computer applcatos were lmtatos o avalable computer tme ad memory restrcted te sze o real words, or practcal applcatos, to bts. Ts correspods to te sgle precso type Fortra or te Sgle type VBA. Wt moder computers, t s possble to do route calculatos usg 4-bt real words. Ts correspods to te double precso type Fortra * or te double type VBA. Te -bt real word allows about 7 sgcat gures; te 4-bt real word allows almost sgcat gures. * Also kow as real(8) or real(kind=8) Fortra 9 ad later versos; sgle precso s typed as real, real(4) or real(kind=4) tese versos o Fortra.