demo8.nb 1 DEMO #8 - GAUSSIAN ELIMINATION USING MATHEMATICA Obectves: - defne matrces n Mathematca - format the output of matrces - appl lnear algebra to solve a real problem - Use Mathematca to perform Gauss-Jordan elmnaton on a matrx Bran Knowlton ChE 50B, Chemcal Engneerng Department, UC Santa Barbara created: 1/7/98 revsed: 1/7/98 revsed b G. Fredrckson for Eng 5A: 10/27/98 1. Matrces n Mathematca In Mathematca, matrces are called Lsts. A Lst s ust a collecton of numbers, separated b commas, and surrounded b curl braces { }. For example, the followng defnes a 1x matrx. u = 8 1, 2, < 81, 2, < You can also use varables n matrces. v = 8x,, < 8x,, < The elements of a Lst don't have to be numbers or varables; the can also be other lsts. For example, a 2x5 matrx can be thought as a 2x1 Lst, where both elements are 1x5 Lsts. Therefore, to specf a 2x5 matrx n Mathematca, specf a two element Lst, where each element s a fve element Lst. Ths tpe of Lst s also referred to as a nested Lst, snce the fve element Lsts resde are 'nested' n another Lst. A = 8 8a11, a12, a1, a14, b1<, 8a21, a22, a2, a24, b2< < 88a11, a12, a1, a14, b1<, 8a21, a22, a2, a24, b2<< The matrx A mght be an augmented matrx derved from a par of lnear equatons n 4 unknowns, a 11 x 1 + a 12 x 2 + a 1 x + a 14 x 4 = b 1 a 21 x 1 + a 22 x 2 + a 2 x + a 24 x 4 = b 2
demo8.nb 2 where a are coeffcents multplng the unknowns x. The matrces u and v are called one-dmensonal matrces, where A s a two-dmensonal matrx. You can also specf three or hgher dmensonal matrces n Mathematca b further nestng of Lsts, and although we wll not use these n Eng 5A, the are useful mathematcal structures n varous branches of phscs and engneerng. 2. Eas-To-Read Matrx Output The format that Mathematca uses for matrx output s a lttle dffcult to read. Mathematca provdes a functon, MatrxForm, that produces more readable output. MatrxForm@AD a11 a12 a1 a14 b1 J a21 a22 a2 a24 b2 N. Applng Lnear Algebra to a Real Problem In Fgure 1 s a crcut dagram, representng a collecton of tpcal household applances, connected (perhaps unsafel) to a sngle wall outlet. The power that each applance uses s lsted n the fgure, along wth the voltage provded b the outlet. The problem s to predct the amount of current that s requred to run all the applances at once. Ths nformaton can be mportant, snce f too much current s drawn from the outlet, a fuse wll fal and ou wll lose to power to all applances. Fgure 1: Crcut Dagram of Common Household Applances In order to fnd the current passng through the outlet, we need to fnd the current runnng through each applance and the voltage at varous places n the crcut. The frst step n analng ths crcut s to fnd the resstance of each applance. Usng the nformaton suppled b the manufacturer, the resstance for each applance can be determned. Fgure 2 shows the resstance of each applance, and also the remanng unknown currents and voltages.
demo8.nb Fgure 2: Applance Resstances and Labelng Notaton of Unknowns To fnd the unknowns, we use two phscal laws to wrte down lnear equatons descrbng the behavor of ths crcut. Frst, at each pont where the crcut branches or ons (ponts A or C), we emplo a "balancng equaton" of currents. Because current s ust the movement of electrons, the total current enterng an gven pont must equal the total current flowng out. Usng the notaton n Fgure 2, we can wrte the followng two (redundant) equatons. I 1 = I 2 + I I 2 + I = I 1 Equatons can be wrtten usng Ohm's Law, whch descrbes how the electrc potental drops when current flows through a resstor. Ohm's Law sas that the voltage drop across a resstor s proportonal to the current flowng through the resstor, where the constant of proportonalt s the resstance. As an equaton, Ohm's Law can be wrtten as, V = IR Applng ths law to each of the applances n Fgure 2, we can wrte fve more equatons, 120 - V 1 = 12 I 1 V 1 - V 2 = 9 I V 2 - V = 60 I V 1 - V = (2/) I 2 V - 0 = (2/) I 1 In total, we have seven equatons n sx unknowns. Wrtten n a more standard form, the equatons look lke the followng. I 1 - I 2 - I = 0 - I 1 + I 2 + I = 0 12 I 1 + V 1 = 120-9 I + V 1 - V 2 = 0-60 I + V 2 - V = 0 - (2/) I 2 + V 1 - V = 0
demo8.nb 4 - (2/) I 1 + V = 0 NOTE: We have chosen here to nclude the redundant equaton. In man applcatons, the fact that two equatons are redundant wll not be so obvous. Also, ou wll see that ncludng a redundant equaton wll not affect the soluton to the problem (see below). 4. Usng Mathematca To Perform Gauss-Jordan Elmnaton Usng Mathematca, we can wrte the equatons nto an augmented matrx: A = 8 8 1, 1, 1, 0, 0, 0, 0<, 8 1, 1, 1, 0, 0, 0, 0<, 812, 0, 0, 1, 0, 0, 120<, 8 0, 0, 9, 1, 1, 0, 0<, 8 0, 0, 60, 0, 1, 1, 0<, 8 0, 2, 0, 1, 0, 1, 0<, 8 2, 0, 0, 0, 0, 1, 0< <; MatrxForm@AD k 1 1 1 0 0 0 0 1 1 1 0 0 0 0 12 0 0 1 0 0 120 0 0 9 1 1 0 0 0 0 60 0 1 1 0 0 2 0 1 0 1 0 2 0 0 0 0 1 0 { To perform Gauss-Jordan Elmnaton on ths augmented matrx, we can call the alread prepared Mathematca functon RowReduce, whch performs Gauss-Jordan elmnaton, producng a matrx n reduced row-echelon form. B = RowReduce@AD; MatrxForm@BD 1 0 0 0 0 0 102510 0 1 0 0 0 0 9960 2880 0 0 1 0 0 0 2156160 0 0 0 1 0 0 210240 0 0 0 0 1 0 0 0 0 0 0 1 109440 k 0 0 0 0 0 0 0 {
demo8.nb 5 N@ MatrxForm@BDD k The fnal answer s: 1. 0 0 0 0 0.6266 0 1. 0 0 0 0.506 0 0 1. 0 0 0 0.102059 0 0 0 1. 0 0 76.4081 0 0 0 0 1. 0 75.4896 0 0 0 0 0 1. 8.7484 0 0 0 0 0 0 0 I 1 =.6; I 2 =.51; I = 0.102; V 1 = 76.408; V 2 = 75.490; V = 8.748; The total amperage requred to run all the applances at once s I 1 or.6 Amperes. { NOTE: The matrx B, the reduced matrx, has a row of eros as the last row. Ths row s the result of the redundant equaton that we ncluded n our sstem of equatons. An equaton that s not ndependant of the others wll show up as a row of eros, and can be neglected. Appendx 1: An Even Better Wa of Formattng Matrces The use of the MatrxForm functon s a rather cumbersome wa to format matrx output, snce ou must call the functon ever tme ou output a matrx. However, there s a wa ou can tell Mathematca to alwas prnt Lsts n matrx form. The followng command, when evaluated, wll cause Mathematca, b default, to use the MatrxForm functon for output of Lsts. $Post := If@MatrxQ@#1D, MatrxForm@#1D, #1D& Tpcall, ths command would come at the begnnng of the notebook. In order to restore the orgnal formattng, smpl clear the varable $Post. See the followng example for a demonstraton.
demo8.nb 6 Clear@$PostD A $Post := If@MatrxQ@#1D, MatrxForm@#1D, #1D& A 981, 1, 1, 0, 0, 0, 0<, 8 1, 1, 1, 0, 0, 0, 0<, 812, 0, 0, 1, 0, 0, 120<, 80, 0, 9, 1, 1, 0, 0<, 80, 0, 60, 0, 1, 1, 0<, 90, 2, 0, 1, 0, 1, 0=, 9 2, 0, 0, 0, 0, 1, 0== k 1 1 1 0 0 0 0 1 1 1 0 0 0 0 12 0 0 1 0 0 120 0 0 9 1 1 0 0 0 0 60 0 1 1 0 0 2 0 1 0 1 0 2 0 0 0 0 1 0 {