TMME, vol4, o.1, p.84 Numercal Methods wth MS Excel M. El-Gebely & B. Yushau 1 Departmet of Mathematcal Sceces Kg Fahd Uversty of Petroleum & Merals. Dhahra, Saud Araba. Abstract: I ths ote we show how MS Excel ca be used to to perform umercal Itegrato, specfcally Trapezodal Rule ad Smso s rule. Futhermore, we llustrate how to geerate Lagrages Iterpolato polyomal. 1. Itroducto MS Excel s the most commoly used spreadsheet, ad has ow grow to powerful software that ca be used vrtually by all braches of scece ad egeerg. The avalablty of the program almost all PCs makes ts usage appears to be at the crease. The program has bee use teachg ad solvg may mathematcal problems may dfferet ways. Ad ths usage rages from lower level to advaced level courses. The ature of the program makes umercal methods much easer to be mplemeted. However, sce Excel the users assume more resposblty desgg the applcato, ad are full cotrol of the mplemetato, the program requres certa degree of creatvty. I ths paper, we ted to llustrate how teachers ad studets ca use Excel to mplemet three well-kow umercal methods: Smpso s Rule, Trapezodal Rules ad Lagrage s terpolato. It should be oted that usg Excel addressg the frst two tegrato techques s ot ew (see refereces), however, our approach s much smpler, more drect, ad do ot requre ay macros for executo. I addto, our worksheet eeds o modfcatos oes s developed, except for eterg the terval of tegrato ad the umber of dvsos (<= 100). Ths gves us motvato to share our experece wth larger mathematcs educato commuty. 1 P. O. Box 1154 KFUPM, Dhahra, 31261, Saud Araba byushau@kfupm.edu.sa Fax: 966-03-860 2979 Tel # 966-03-860-1081/5023 The Motaa Mathematcs Ethusast, ISSN 1551-3440, Vol. 4, o.1, pp. 84-92 2007 The Motaa Coucl of Teachers of Mathematcs
TMME, vol4, o.1, p.85 The motvato for Lagrage s terpolato stems from a questo asked to Dr. Math MathForum.org (http://mathforum.org/lbrary/drmath/vew/63984.html). The questoer wated to kow amog other thgs how to use Excel to work out Lagrage s terpolato. Although the two other questos were aswered suffcetly, the Excel mplemetato was skpped by Dr. Math. So ths ote, we pla to aswer that questo as well as share our experece wth larger mathematcs commuty. 2. Numercal Itegrato wth Excel I ths secto we llustrate how Excel worksheets ca be used to mplemet the trapezodal rule ad the Smpso s rule for umercal tegrato. The problem s to fd a umercal approxmato for the tegral b I = f ( xdx ). a 2.1 The trapezodal rule The trapezodal rule works by approxmatg the fucto f ( x ) by a pecewse lear fucto ad evaluate the tegral of each pece. If the terval [ a, b ] s dvded up to equal b a subterval, each of wdth h =, the the approxmate tegral s I h 2 = 1 f ( x ) f ( ) 1 + x, where, x = a+ h, ad = 0, 1,, We lluastrate wth the followg example 1 6 (14x + 7) dx. 0 The method s explaed as follows. The edpots (tal ad termal) of the terval, ad the umber of dvsos are etered the cells A2, B2, C2, respectvely. The value of h s calculated the cell D2 by eterg the formula = (B2-A2)/C2. To geerate the x s we take the followg steps: 1. I cell E2 eter the formula =A2. Ths copes the value of a = x0 to E2. The followg fgure shows the upper part of the Excel worksheet mplemetato of the method.
TMME, vol4, o.1, p.86 2. The ext values are geerated wth the formula = IF(E2>=$B$2,$B$2,E2+$D$2) E3. Ths formula adds h to the prevous value utl we reach the value of b. Afterwards t keeps eterg the value of b. Ths mechasm s used to eable chagg the value of to get more cotrol o the accuracy of the soluto as explaed Step 5. 3. Copy ths formula to the ext 100 cells or so below E4. The beow fgure shows a part of the sheet further dow, where you ca see the value of b beg repeated. 4. The fucto f s etered the colum labeled f ( x ) by eterg the formula =7+14*E2^6 the cell F2 ad copyg t alog the correspodg cells for the x s. 5. We the form the elemets of the summato the trapezodal rule by eterg the formula = (E3-E2)/2*(F2+F3) cell G3 ad copyg t alog the correspodg cells for the x s. Observe that, stead of usg the value of h geerated cell D2, we used the equvalet dfferece E3-E2. Ths has two advatages: a) The formula produces zeros whe we go past the rght edpot b. I ths way, the fal sum of these umbers s ot affected by the repetto of b. b) It allows the use of the trapezodal rule wth o-uform dvsos of the terval [, ] a b. 6. The last step s to add the terms colum G to get the approxmato of the tegral. Select the rage of cells that cotas the summato terms ad the clck the sum butto ( ) o the toolbar. The result s show below.
TMME, vol4, o.1, p.87 Note: (a) If you ow chage the umber of dvsos to 100, the ew, more accurate approxmato wll appear the same cell (G103). (b) To chage the terval of tegrato all you eed to do s to chage the values a, b cells A2, B2. (c) To chage the tegrated fucto eter the ew formula cell F2 ad copy t to cell F103. 2.2 Smpso s rule Smpso s rule fds a approxmato of the value of the tegral I by approxmatg the tegrad wth a pecewse polyomal of degree 2 ad the evaluate the tegral over each pece. Smpso s formula s I h 3 1 = 1 [ f ( x + 4 f ( x ) f ( x ))], 1 + + 1 b a Where the terval [ a, b ] s dvded to tervals, each of legth h =. The Excel mplemetato of Smpso s rule s very much smlar to that of the trapezodal rule, except for some detals. The followg fgure shows the upper part of the worksheet mplemetato.
TMME, vol4, o.1, p.88 As you ca see from ths fgure the table s exactly the same as that for the trapezodal rule except for the last colum. The last colum s geerated as follows. I the cell G3 eter the formula =(E4-E2)/6*(F2+4*F3+F4). Sce Smpso s formula spas two subtervals for each etry the summato, the copyg of the formula s doe as follows. Select the rage of two cells G2, G3 (ote that G2 s actually empty). Usg the formula copyg techque, drag the two cells dow to cell G103. The result s that the formula s coped to every other cell. Oe more thg to otce here s that, the above formula, the value of h s replaced by the dfferece over 3 cells dvded by 2. Ths way the same skppg s acheved ad o problem arses as a result of repeatg the values of b. Whe you select the rage G2:G103 ad clck the sum butto, you wll see the result 9.000003644, whch s more accurate tha the result of the trapezodal rule as the theory predcts. 3. Lagrage Iterpolato The dea of terpolato as succctly elaborated the ste (http://mathforum.org/lbrary/drmath/vew/63984.html) s to fd a fucto of a specfed form whch passes through a gve lst of pots. Lagrage terpolato uses polyomals. For stace, t s well kow that gve two pots ( x1, y 1) ad ( x 2, y 2 ), there s exactly oe le (degree oe polyomal) that ca be geerated from these two pots. It s also true that gve three dstct pots, there are two possbltes, ether the three pots le a le, or else there s exactly oe quadratc polyomal that the three pots pass through. If you have four pots, there wll be a cubc (at most) polyomal through them; f you have 10 pots, there wll be a polyomal of degree at most 9 through them. I geeral, gve a lst of dstct pots{ ( x, y ), ( x, y ), ( x, y ),..., ( x, y )}, there wll be a polyomal of degree at most 1 1 2 2 3 3
TMME, vol4, o.1, p.89 ( 1) passg through the gve pots. I ths secto, we llustrate how oe ca use Excel to geerate ths polyomal. The steps are as follows: 1. Mark a rage (say R(2,3):R(2,+2)) as x 1, x 2, x 3,..., x (ths s a raw of cells startg at C2). 2. Mark a colum (say A4:A+3 (R(4,1):R(+3,1))) as x1, x2, x3,..., x. 3. I the rage R(3,3):R(3,+2) eter the values of the terpolato pots x k from the terval [ a, b ]. 4. I the colum B4:B+3, that s (R(4,2):R(+3,2)) eter the values of the terpolato pots x k from the terval [ a, b ]. 5. I the cell C4 (.e., R(4,3)) eter the formula = IF(C$3-$B4<>0;C$3-$B4;1). Notce that raw 3 ad colum B are fxed ths formlua. 6. Copy the formula through the rage R(4,3):R(+3,+2). Ths should produce the dffereces ( x x j) for all, j but replacg the dfferece x j x j by oe stead of ts true value 0. The et effect s to cacel the calculato of x j x j. products (x-xj) -> Iterpolato x1 x2 x3 x4 pots 1 2 3 4 x1 1 1 1 2 3 x2 2-1 1 1 2 x3 3-2 -1 1 1 x4 4-3 -2-1 1 Prod(xj-x) -> -6 2-2 6 1 0-1 -2-3 0 1.2 0.2-0.8-1.8-2.8-0.8064 1.4 0.4-0.6-1.6-2.6-0.9984 1.6 0.6-0.4-1.4-2.4-0.8064 1.8 0.8-0.2-1.2-2.2-0.4224 2 1 0-1 -2 0 2.2 1.2 0.2-0.8-1.8 0.3456 2.4 1.4 0.4-0.6-1.6 0.5376 2.6 1.6 0.6-0.4-1.4 0.5376 2.8 1.8 0.8-0.2-1.2 0.3456 3 2 1 0-1 0 3.2 2.2 1.2 0.2-0.8-0.4224 3.4 2.4 1.4 0.4-0.6-0.8064 3.6 2.6 1.6 0.6-0.4-0.9984 3.8 2.8 1.8 0.8-0.2-0.8064 4 3 2 1 0 0 x values
TMME, vol4, o.1, p.90 7. I the cell R(+4,3) eter the formula = Product(R(4,3):R(+3,3)). The selecto of the argumet of the product sheet fucto ca be doe by draggg wth the mouse. Copy the formula R(+4,3) through the rage R(+4,3): R(+4,+2) by draggg wth the mouse. Ths gves the products that go the deomators of the Lagrage bass fuctos. 8. Geerate the set of x values that you wat to compute at the rage R(+5,2):R(+m+5,2) from the terval [a, b] by ay method of sequece geerato. 9. I the cell R(+5,3) eter the formula =R(+5,$2)-R($3,3). Drag ths formula through the rage R(+5,3):R(+m+5,+2). ths geerates a table of the values of ( x x ), 1. 10. I the cell R(+5,+3) eter the formula = Product(R(+5,3):R(+5,+2). Drag ths formula through the rage R(+5,+3):R(+m+5,+3). 11. Now we are ready to geerate the values of the Lagrage bass fuctos at the pots x chose step 7. Label the cells (should be blak) R(+4,+4):R(+4,2+3) as L1, L2,..., L. L1 L2 L3 L4 f(x) P(x) s(x) 1 0 0 0 8.41471 8.41471 8.41471 0.672 0.504-0.224 0.048 9.092974 9.55817 9.320391 0.416 0.832-0.312 0.064 1.4112 10.14123 9.854497 0.224 1.008-0.288 0.056-7.56802 10.22038 9.995736 0.088 1.056-0.176 0.032 9.852127 9.738476 0 1 0 0 9.092974 9.092974-0.048 0.864 0.216-0.032 7.99942 8.084964-0.064 0.672 0.448-0.056 6.627964 6.754632-0.056 0.448 0.672-0.064 5.035109 5.155014-0.032 0.216 0.864-0.048 3.277354 3.349882 0 0 1 0 1.4112 1.4112 0.032-0.176 1.056 0.088-0.50685-0.58374 0.056-0.288 1.008 0.224-2.4203-2.55541 0.064-0.312 0.832 0.416-4.27265-4.4252 0.048-0.224 0.504 0.672-6.00739-6.11858 0 0 0 1-7.56802-7.56802
TMME, vol4, o.1, p.91 1.2 0.8 0.4 0-0.4 1 2 3 4 L1 L2 L3 L4 12. I the cell R(+5,+4) eter the formula If(R(+5,3)=0;1;R(+5,$+3)/R(+5,3)/R($+4,3)). Drag ths formula through the rage cell R(+5,+4):R(+m+5,2+m+4). Ths geerates the values of the Lagrage bass fuctos at the m chose pots x. 13. The fal stage s to geerate the Lagrage polyomal. Label the cells R(+4,2+4) ad R(+4,2+5) as f ( x ) ad P( x) respectvely. 14. I the rage R(+5,2+4):R(2+4:2+4) eter the values of f ( x ), = 1,2,...,. 15. Select the rage of cells (empty by ow) R(+5,2+5):R(+m+5,2+5) ad eter the formula =MMULT(R(+5,+4):R(+m+5,2+m+4); R(+5,2+4):R(2+4:2+4)). Ths produces the values of P(x) at the m pots x. 16. The values of the orgal fucto ca be produced ext ad the f ( x ) ad P( x ) ca be plotted agast x o the same graph.
TMME, vol4, o.1, p.92 11 7 3-1 1 2 3 4 P(x) s(x) -5-9 Refereces http://oregostate.edu/struct/ch490/lessos/lesso11.htm http://oregostate.edu/~haggertr/487/tegrate.htm http://www.math.vllaova.edu/coectos/excel/ http://mathforum.org/lbrary/drmath/vew/63984.html