(*^ ::[ Information = "This is a Mathematica Notebook file. It contains ASCII text, and can be transferred by email, ftp, or other text-file transfer utility. It should be read or edited using a copy of Mathematica or MathReader. If you received this as email, use your mail application or copy/paste to save everything from the line containing (*^ down to the line containing ^*) into a plain text file. On some systems you may have to give the file a name ending with ".ma" to allow Mathematica to recognize it as a Notebook. The line below identifies what version of Mathematica created this file, but it can be opened using any other version as well."; FrontEndVersion = "NeXT Mathematica Notebook Front End Version 2.2"; NeXTStandardFontEncoding; fontset = title, inactive, noPageBreakBelow, noPageBreakInGroup, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, L1, e8, 24, "Times"; ; fontset = subtitle, inactive, noPageBreakBelow, noPageBreakInGroup, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, L1, e6, 18, "Times"; ; fontset = subsubtitle, inactive, noPageBreakBelow, noPageBreakInGroup, nohscroll, preserveAspect, groupLikeTitle, center, M7, italic, L1, e6, 14, "Times"; ; fontset = section, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, grayBox, M22, bold, L1, a20, 18, "Times"; ; fontset = subsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, blackBox, M19, bold, L1, a15, 14, "Times"; ; fontset = subsubsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, whiteBox, M18, bold, L1, a12, 12, "Times"; ; fontset = text, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12; fontset = smalltext, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 10, "Times"; ; fontset = input, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeInput, M42, N23, bold, L1, 12, "Courier"; ; fontset = output, output, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L-5, 12, "Courier"; ; fontset = message, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L1, 12, "Courier"; ; fontset = print, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L1, 12, "Courier"; ; fontset = info, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L1, 12, "Courier"; ; fontset = postscript, PostScript, formatAsPostScript, output, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeGraphics, M7, l34, w282, h287, L1, 12, "Courier"; ; fontset = name, inactive, noPageBreakInGroup, nohscroll, preserveAspect, M7, italic, B65535, L1, 10, "Times"; ; fontset = header, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, italic, L1, 12, "Times"; ; fontset = leftheader, 12; fontset = footer, inactive, nohscroll, noKeepOnOnePage, preserveAspect, center, M7, italic, L1, 12, "Times"; ; fontset = leftfooter, 12; fontset = help, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12; fontset = clipboard, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12; fontset = completions, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12, "Courier"; ; fontset = special1, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12; fontset = special2, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12; fontset = special3, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12; fontset = special4, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12; fontset = special5, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12; paletteColors = 128; automaticGrouping; currentKernel; ] :[font = title; inactive; preserveAspect; startGroup] Solving Linear Equations Step by Step :[font = subtitle; inactive; preserveAspect] Two Equations in Two Unknowns :[font = subsubtitle; inactive; preserveAspect] by George Beck :[font = text; inactive; preserveAspect] This notebook discusses the solutions of systems of equations The built-in functions Solve and NSolve are the usual way to solve such systems. The defined function WalkSolve solves a system step by step. ;[s] 5:0,0;102,1;166,2;176,3;204,4;206,-1; 5:1,11,8,Times,0,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Courier,0,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Courier,0,12,0,0,0; :[font = subsection; inactive; preserveAspect; startGroup] Proper formatting of output :[font = subsubsection; inactive; preserveAspect] From the menu, select Info, Preferences, Action... to produce a big box titled Action Preferences. ;[s] 1:0,0;99,-1; 1:1,11,8,Times,0,12,0,0,0; :[font = subsubsection; inactive; preserveAspect] Look at the second box within Action Preferences titled Placing Print Output. ;[s] 1:0,0;77,-1; 1:1,11,8,Times,0,12,0,0,0; :[font = subsubsection; inactive; preserveAspect] Click on the first little button so that there is no check mark for ``Place Print output as it is generated''. ;[s] 1:0,0;111,-1; 1:1,11,8,Times,0,12,0,0,0; :[font = subsubsection; inactive; preserveAspect] Click on the second little button so that there is a check mark for ``Place each Print line in a separate cell''. ;[s] 1:0,0;114,-1; 1:1,11,8,Times,0,12,0,0,0; :[font = subsubsection; inactive; preserveAspect; endGroup] Click on the Apply button at the bottom. ;[s] 1:0,0;42,-1; 1:1,11,8,Times,0,12,0,0,0; :[font = section; inactive; preserveAspect; startGroup] Discussion :[font = text; inactive; preserveAspect] This defines four linear equations. that we will solve in pairs. The single equal sign is an assignment, the double equal signs represent equality. The variables l1, l2, l3, and l4 stand for the whole equation. :[font = input; initialization; preserveAspect] *) l1 = 2 x + 3 y == 4; (* :[font = input; initialization; preserveAspect] *) l2 = -4 x + 3 y == -1; l3 = 2 x + 3 y == 8; l4 = -8 x + 6 y == -2; (* :[font = subsection; inactive; preserveAspect; startGroup] The fast way :[font = subsubsection; inactive; preserveAspect; startGroup] Example 1 :[font = text; inactive; preserveAspect] This is the format for solving the two equations simultaneously. Solve gives the answers immediately. :[font = input; preserveAspect; startGroup] Solve[{l1, l2}, {x, y}] :[font = output; output; inactive; preserveAspect; endGroup] {{x -> 5/6, y -> 7/9}} ;[o] 5 7 {{x -> -, y -> -}} 6 9 :[font = text; inactive; preserveAspect] If you want to see decimals, use N, which stands for numeric. In Mathematica % means the previous result. ;[s] 2:0,0;65,1;131,-1; 2:1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0; :[font = input; preserveAspect; startGroup] N[%] :[font = output; output; inactive; preserveAspect; endGroup] {{x -> 0.833333333333333, y -> 0.7777777777777778}} ;[o] {{x -> 0.833333, y -> 0.777778}} :[font = text; inactive; preserveAspect] You can get decimals right away by using NSolve. :[font = input; preserveAspect; startGroup] NSolve[{l1, l2}, {x, y}] :[font = output; output; inactive; preserveAspect; endGroup; endGroup] {{x -> 0.833333333333333, y -> 0.7777777777777778}} ;[o] {{x -> 0.833333, y -> 0.777778}} :[font = subsubsection; inactive; preserveAspect; startGroup] Example 2 :[font = text; inactive; preserveAspect] There are no solutions to this next system, because the equations l1 and l3 are the same on the left hand side but different on the right. :[font = input; preserveAspect; startGroup] Solve[{l1, l3}, {x, y}] :[font = output; output; inactive; preserveAspect; endGroup; endGroup] {} ;[o] {} :[font = subsubsection; inactive; preserveAspect; startGroup] Example 3 :[font = text; inactive; preserveAspect] In this case there are infinitely many solutions, because one of the equations is a multiple of the other. :[font = input; preserveAspect; startGroup] Solve[{l2, l4}, {x, y}] :[font = output; output; inactive; preserveAspect; endGroup; endGroup; endGroup] {{x -> 1/4 + (3*y)/4}} ;[o] 1 3 y {{x -> - + ---}} 4 4 :[font = subsection; inactive; preserveAspect; startGroup] One step at a time :[font = text; inactive; preserveAspect] You can solve systems of equations one step at a time, instead of all at once like Solve or NSolve. With WalkSolve, each step is explained and the usual Mathematica output for Solve is shown as a check. With RunSolve no explanations are given. :[font = subsubsection; inactive; preserveAspect; startGroup] Example 4 :[font = input; preserveAspect; startGroup] WalkSolve[{l1, l2}, {x, y}] :[font = print; inactive; preserveAspect] 2 x + 3 y == 4 -4 x + 3 y == -1 :[font = print; inactive; preserveAspect] Subtract the second equation from the first. :[font = print; inactive; preserveAspect] 6 x == 5 -4 x + 3 y == -1 :[font = print; inactive; preserveAspect] Divide by 6 in the first equation. :[font = print; inactive; preserveAspect] 5 x == - 6 -4 x + 3 y == -1 :[font = print; inactive; preserveAspect] Subtract -4 times the first equation from the\ second. :[font = print; inactive; preserveAspect] 5 x == - 6 7 3 y == - 3 :[font = print; inactive; preserveAspect] Divide by 3 in the second equation. :[font = print; inactive; preserveAspect] 5 x == - 6 7 y == - 9 :[font = print; inactive; preserveAspect] This is the result from Solve in one step: :[font = output; output; inactive; preserveAspect; endGroup; endGroup] {{x -> 5/6, y -> 7/9}} ;[o] 5 7 {{x -> -, y -> -}} 6 9 :[font = subsubsection; inactive; preserveAspect; startGroup] Example 5 :[font = input; preserveAspect; startGroup] WalkSolve[{l1, l3}, {x, y}] :[font = print; inactive; preserveAspect] 2 x + 3 y == 4 2 x + 3 y == 8 :[font = print; inactive; preserveAspect] Subtract the first equation from the second. :[font = print; inactive; preserveAspect] 2 x + 3 y == 4 False :[font = print; inactive; preserveAspect] There are no solutions! :[font = print; inactive; preserveAspect] Using Solve: :[font = output; output; inactive; preserveAspect; endGroup; endGroup] {} ;[o] {} :[font = subsubsection; inactive; preserveAspect; startGroup] Example 6 :[font = input; preserveAspect; startGroup] RunSolve[{l3, l4}, {x, y}] :[font = print; inactive; preserveAspect] 2 x + 3 y == 8 -8 x + 6 y == -2 :[font = print; inactive; preserveAspect] ......................................... :[font = print; inactive; preserveAspect] 3 y x + --- == 4 2 -8 x + 6 y == -2 :[font = print; inactive; preserveAspect] ......................................... :[font = print; inactive; preserveAspect] 3 y x + --- == 4 2 18 y == 30 :[font = print; inactive; preserveAspect] ......................................... :[font = print; inactive; preserveAspect] 3 y x + --- == 4 2 5 y == - 3 :[font = print; inactive; preserveAspect] ......................................... :[font = print; inactive; preserveAspect; endGroup; endGroup; endGroup; endGroup] 3 x == - 2 5 y == - 3 :[font = section; inactive; preserveAspect; startGroup] Implementation :[font = input; initialization; preserveAspect] *) WalkSolve::usage = "WalkSolve[{a x + b y == u, c x + d y == v}, {x, y}] solves the pair of simultaneous system of equations for x and y one step at a time, explaining each step."; (* :[font = input; preserveAspect] RunSolve::usage = "RunSolve[{a x + b y == u, c x + d y == v}, {x, y}] solves the pair of simultaneous system of equations for x and y one step at a time with no explanations."; :[font = subsection; inactive; preserveAspect; startGroup] Converting between equations and a matrix :[font = input; initialization; preserveAspect] *) MatrixFromEquations[eqns_, {x_, y_}] := { { eqns[[1, 1]] /. {x -> 1, y -> 0}, eqns[[1, 1]] /. {x -> 0, y -> 1}, eqns[[1, 2]] }, { eqns[[2, 1]] /. {x -> 1, y -> 0}, eqns[[2, 1]] /. {x -> 0, y -> 1}, eqns[[2, 2]] } } (* :[font = input; initialization; preserveAspect; endGroup] *) EquationsFromMatrix[{{a_, b_, u_}, {c_, d_, v_}}, {x_, y_}] := {a x + b y ==u, c x +d y == v} (* :[font = subsection; inactive; preserveAspect; startGroup] WalkSolve :[font = subsubsection; inactive; preserveAspect; startGroup] many or no solutions :[font = input; initialization; preserveAspect] *) WalkSolve[eqns:{True, _}, {x_, y_}] := ( Print @ TableForm @ eqns; Print["There are infinitely many solutions!"]; Print["This is the result from Solve in one step: "]; Solve[eqns, {x, y}] ) (* :[font = input; initialization; preserveAspect] *) WalkSolve[eqns:{_, True}, {x_, y_}] := ( Print @ TableForm @ eqns; Print["There are infinitely many solutions!"]; Print["Using Solve: "]; Solve[eqns, {x, y}] ) (* :[font = input; initialization; preserveAspect] *) WalkSolve[eqns:{False, _}, {x_, y_}] := ( Print @ TableForm @ eqns; Print["There are no solutions!"]; Print["This is the result from Solve in one step: "]; Solve[eqns, {x, y}] ) (* :[font = input; initialization; preserveAspect; endGroup] *) WalkSolve[eqns:{_, False}, {x_, y_}] := ( Print @ TableForm @ eqns; Print["There are no solutions!"]; Print["This is the result from Solve in one step: "]; Solve[eqns, {x, y}] ) (* :[font = subsubsection; inactive; preserveAspect; startGroup] both equations solved :[font = input; initialization; preserveAspect; endGroup] *) WalkSolve[ eqns:{x_ == u_, y_ == v_}, {x_, y_}] := ( Print @ TableForm @ eqns; Print["This is the result from Solve in one step: "]; Solve[eqns, {x, y}] ) (* :[font = subsubsection; inactive; preserveAspect; startGroup] the loop :[font = input; initialization; preserveAspect; endGroup; endGroup] *) WalkSolve[eqns_, {x_, y_}] := Module[ {rm, neweqns}, Print @ TableForm @ eqns; rm = ReduceMatrix[MatrixFromEquations[eqns, {x, y}]]; Print[ rm[[1]] ]; neweqns = EquationsFromMatrix[rm[[2]], {x, y}]; WalkSolve[neweqns, {x, y}] ] (* :[font = subsection; inactive; preserveAspect; startGroup] RunSolve :[font = subsubsection; inactive; preserveAspect; startGroup] many or no solutions :[font = input; initialization; preserveAspect] *) WalkSolve[eqns:{True, _}, {x_, y_}] := Print @ TableForm @ eqns; (* :[font = input; initialization; preserveAspect] *) RunSolve[eqns:{_, True}, {x_, y_}] := Print @ TableForm @ eqns; (* :[font = input; initialization; preserveAspect] *) RunSolve[eqns:{False, _}, {x_, y_}] := Print @ TableForm @ eqns; (* :[font = input; initialization; preserveAspect; endGroup] *) RunSolve[eqns:{_, False}, {x_, y_}] := Print @ TableForm @ eqns; (* :[font = subsubsection; inactive; preserveAspect; startGroup] both equations solved :[font = input; initialization; preserveAspect; endGroup] *) RunSolve[ eqns:{x_ == u_, y_ == v_}, {x_, y_}] := Print @ TableForm @ eqns; (* :[font = subsubsection; inactive; preserveAspect; startGroup] the loop :[font = input; initialization; preserveAspect; endGroup; endGroup] *) RunSolve[eqns_, {x_, y_}] := Module[ {rm, neweqns}, Print @ TableForm @ eqns; rm = ReduceMatrix[MatrixFromEquations[eqns, {x, y}]]; Print[ "........................................." ]; neweqns = EquationsFromMatrix[rm[[2]], {x, y}]; RunSolve[neweqns, {x, y}] ] (* :[font = subsection; inactive; pageBreak; preserveAspect; startGroup] String statements :[font = input; initialization; preserveAspect] *) ToNiceString[z_] := If[ Head[z] === Rational, StringJoin[ ToString[Numerator[z]], "/", ToString[Denominator[z]] ], ToString[z] ] (* :[font = input; initialization; preserveAspect] *) SwitchFirstSecond[] := "Switch the equations." (* :[font = input; initialization; preserveAspect] *) SubtractFirstSecond[] := "Subtract the first equation from the second." (* :[font = input; initialization; preserveAspect] *) SubtractSecondFirst[] := "Subtract the second equation from the first." (* :[font = input; initialization; preserveAspect] *) DivideFirst[z_] := StringJoin[ "Divide by ", ToNiceString[z], " in the first equation." ] (* :[font = input; initialization; preserveAspect] *) DivideSecond[z_] := StringJoin[ "Divide by ", ToNiceString[z], " in the second equation." ] (* :[font = input; initialization; preserveAspect] *) SubtractMultipleFirstSecond[z_] := StringJoin[ "Subtract ", ToNiceString[z], " times the first equation from the second." ] (* :[font = input; initialization; preserveAspect; endGroup] *) SubtractMultipleSecondFirst[z_] := StringJoin[ "Subtract ", ToNiceString[z], " times the second equation from the first." ] (* :[font = subsection; inactive; preserveAspect; startGroup] ReduceMatrix :[font = subsubsection; inactive; preserveAspect; startGroup] one equation is solved :[font = input; initialization; preserveAspect] *) ReduceMatrix[{{1, 0, u_}, {c_, d_, v_}}] := { SubtractMultipleFirstSecond[c], {{1, 0, u}, {0, d, v - c u}} } /; c!= 0 (* :[font = input; initialization; preserveAspect; endGroup] *) ReduceMatrix[{{a_, b_, u_}, {0, 1, v_}}] := { SubtractMultipleSecondFirst[a], {{a, 0, u - b v}, {0, 1, v}} } /; b != 0 (* :[font = subsubsection; inactive; preserveAspect; startGroup] missing terms :[font = input; initialization; preserveAspect] *) ReduceMatrix[{{0, b_, u_}, {c_, d_, v_}}] := { SwitchFirstSecond[], {{c, d, v}, {0, b, u}} } (* :[font = input; initialization; preserveAspect] *) ReduceMatrix[{{a_, b_, u_}, {0, d_, v_}}] := { DivideSecond[d], {{a, b, u}, {0, 1, v/d}} } /; d != 1 (* :[font = input; initialization; preserveAspect] *) ReduceMatrix[{{a_, b_, u_}, {c_, 0, v_}}] := { SwitchFirstSecond[], {{c, 0, v}, {a, b, u}} } (* :[font = input; initialization; preserveAspect; endGroup] *) ReduceMatrix[{{a_, 0, u_}, {c_, d_, v_}}] := { DivideFirst[a], {{1, 0, u/a}, {c, d, v}} } /; a != 1 (* :[font = subsubsection; inactive; preserveAspect; startGroup] matching terms :[font = input; initialization; preserveAspect] *) ReduceMatrix[{{a_, b_, u_}, {a_, d_, v_}}] := { SubtractFirstSecond[], {{a, b, u}, {0, d - b, v - u}} } (* :[font = input; initialization; preserveAspect; endGroup] *) ReduceMatrix[{{a_, b_, u_}, {c_, b_, v_}}] := { SubtractSecondFirst[], {{a - c, 0, u - v}, {c, b, v}} } (* :[font = subsubsection; inactive; preserveAspect; startGroup] a coefficient is 1 :[font = input; initialization; preserveAspect] *) ReduceMatrix[{{1, b_, u_}, {c_, d_, v_}}] := { SubtractMultipleFirstSecond[c], {{1, b, u}, {0, d - b c, v - c u}} }/; c != 1 (* :[font = input; initialization; preserveAspect] *) ReduceMatrix[{{a_, b_, u_}, {1, d_, v_}}] := { SubtractMultipleSecondFirst[a], {{0, b - a d, u - a v}, {1, d, v}} } /; a != 1 (* :[font = input; initialization; preserveAspect] *) ReduceMatrix[{{a_, 1, u_}, {c_, d_, v_}}] := { SubtractMultipleFirstSecond[d], {{a, 1, u}, {c - a d, 0, v - d u}} }/; d != 1 (* :[font = input; initialization; preserveAspect; endGroup] *) ReduceMatrix[{{a_, b_, u_}, {c_, 1, v_}}] := { SubtractMultipleSecondFirst[b], {{a - b c, 0, u - b v}, {c, 1, v}} } /; b != 1 (* :[font = subsubsection; inactive; preserveAspect; startGroup] the general case :[font = input; initialization; preserveAspect; endGroup; endGroup; endGroup; endGroup] *) ReduceMatrix[{{a_, b_, u_}, {c_, d_, v_}}] := { DivideFirst[a], {{1, b/a, u/a}, {c, d, v}} } /; a != 1 (* ^*)