(*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 3.0, MathReader 3.0, or any compatible application. The data for the notebook starts with the line of stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. ***********************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 9865, 378]*) (*NotebookOutlinePosition[ 10502, 401]*) (* CellTagsIndexPosition[ 10458, 397]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Symbolic Computation", "Section"], Cell[TextData[{ "This notebook is designed to accompany Chapter 6 of Introduction to \ Scientific Programming: Computational Problem Solving Using ", StyleBox["Mathematica", FontSlant->"Italic"], " and C by Joseph L. Zachary. In it we will explore the idea of symblic \ computing further." }], "Text"], Cell[CellGroupData[{ Cell["Introduction", "Subsection"], Cell[TextData[{ " Chapter 6 only hints at the support for symbolic mathematics provided by \ ", StyleBox["Mathematica", FontSlant->"Italic"], ". In this notebook we will explore this support in more depth. The \ material that we will be covering here requires varying levels of \ mathematical expertise. Feel free to skip over examples if you don't have \ the necessary background." }], "Text"] }, Closed]], Cell[CellGroupData[{ Cell["Algebraic simplification", "Subsection"], Cell[TextData[{ StyleBox["Mathematica", FontSlant->"Italic"], "'s \"Simplify\" function expects an expression as a parameter, which it \ simplifies. For example, notice the difference between" }], "Text"], Cell[BoxData[ \(Sin[x]^2\ + \ Cos[x]^2\)], "Input"], Cell["and", "Text"], Cell[BoxData[ \(Simplify[Sin[x]^2\ + \ Cos[x]^2]\)], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["Equation solving", "Subsection"], Cell[TextData[{ StyleBox["Mathematica", FontSlant->"Italic"], " provides two general-purpose functions for solving equations, Solve and \ FindRoot. Solve works symbolically much as you do when solving equations; \ FindRoot works numerically by repeatedly guessing at a solution until it \ finds one that works. Solve is exact, FindRoot is approximate. For \ complicated equations, Solve can be much slower than FindRoot. In fact, \ there are plenty of equations where Solve fails but FindRoot succeeds." }], "Text"], Cell["\<\ Solve knows all kinds of tricks for solving equations. For \ example, it knows the quadratic formula\ \>", "Text"], Cell[BoxData[ \(Solve[3*x^2\ - \ 10*x\ + \ 6\ == \ 0, \ x]\)], "Input"], Cell["\<\ With floating-point coefficients, we get floating-point \ solutions.\ \>", "Text"], Cell[BoxData[ \(Solve[3.*x^2\ - \ 10.*x\ + \ 6.\ == \ 0, \ x]\)], "Input"], Cell["Complex roots are also possible", "Text"], Cell[BoxData[ \(Solve[3*x^2\ - \ 4*x\ + \ 6\ == \ 0, \ x]\)], "Input"], Cell["The coefficients can be entirely symbolic.", "Text"], Cell[BoxData[ \(Solve[a*x^2\ + \ b*x\ + \ c\ == \ 0, \ x]\)], "Input"], Cell[TextData[{ StyleBox["Mathematica", FontSlant->"Italic"], " can also solve simultaneous linear equations. Notice how list notation \ is used here." }], "Text"], Cell[BoxData[ \(Solve[{3*x\ + \ 4*y\ == \ 7, \ 5*x\ + \ 3*y\ == \ 11}, \ {x, y}]\)], "Input"], Cell["The equations don't have to be linear.", "Text"], Cell[BoxData[ \(Solve[{3*x^2\ + \ y\ == \ 7, \ 5*x\ + \ 3*y\ == \ 11}, \ {x, y}]\)], "Input"], Cell["\<\ It is not difficult to pose an equation that solve cannot deal \ with.\ \>", "Text"], Cell[BoxData[ \(Solve[3*Cos[x] == \ x, \ x]\)], "Input"], Cell["\<\ If you don't need an exact or a symbolic solution, FindRoot can \ solve more equations and is generally faster.\ \>", "Text"], Cell[BoxData[ \(FindRoot[3*Cos[x]\ == \ x, \ {x, 0}]\)], "Input"], Cell["Of course, if you forget and pose a symbolic equation", "Text"], Cell[BoxData[ \(FindRoot[a*cos[x]\ == \ x, \ {x, 0}]\)], "Input"], Cell["you'll get an error message.", "Text"] }, Closed]], Cell[CellGroupData[{ Cell["Calculus", "Subsection"], Cell["\<\ Mathematica is also handy for doing calculus. The D function does \ symbolic differentiation\ \>", "Text"], Cell[BoxData[ \(D[Cos[Sin[x]], \ x]\)], "Input"], Cell["whereas the Integrate function does symbolic integration", "Text"], Cell[BoxData[ \(Integrate[\(-Sin[Sin[x]]\)*Cos[x], \ x]\)], "Input"], Cell["You can also do definite integration by providing bounds", "Text"], Cell[BoxData[ \(Integrate[\(-Sin[Sin[x]]\)*Cos[x], \ {x, 0, Pi/2.}]\)], "Input"], Cell["It is not difficult to stump Mathematica.", "Text"], Cell[BoxData[ \(Integrate[Cos[Sin[x]], \ x]\)], "Input"], Cell["\<\ and this is how it tells you that it has failed. In situations \ like this, trying to do a definite integral as above may be futile as well.\ \ \>", "Text"], Cell[BoxData[ \(Integrate[Cos[Sin[x]], \ {x, 0, Pi/2.}]\)], "Input"], Cell[TextData[{ "In this case, if you \"NIntegrate\" instead, ", StyleBox["Mathematica", FontSlant->"Italic"], " will do a numerical integration. This means that instead of first \ integration symbolically and then plugging in the bounds, it will find an \ approximation to the integral without first computing the definite integral." }], "Text"], Cell[BoxData[ \(NIntegrate[Cos[Sin[x]], \ {x, 0, Pi/2.}]\)], "Input"], Cell["\<\ It is possible to supply infinity and -infinity as bounds. \ Thus\ \>", "Text"], Cell[BoxData[ \(Integrate[1/x, {x, 0, Infinity}]\)], "Input"], Cell["diverges, whereas", "Text"], Cell[BoxData[ \(Integrate[1/Exp[x], \ {x, 0, Infinity}]\)], "Input"], Cell["gives us a finite answer.", "Text"] }, Closed]], Cell[CellGroupData[{ Cell["Replacements", "Subsection"], Cell[TextData[{ "Another handy feature of ", StyleBox["Mathematica", FontSlant->"Italic"], " is the ability to perform substitutions. Let's suppose that we solve an \ equation numerically" }], "Text"], Cell[BoxData[ \(soln\ = \ FindRoot[Cos[x] == x, \ {x, 0}]\)], "Input"], Cell["\<\ We can substitute the value of soln for x in the left-hand-side of \ the equation by doing\ \>", "Text"], Cell[BoxData[ \(ReplaceAll[Cos[x], \ soln]\)], "Input"], Cell["\<\ This works for any kind of expression. For example, suppose we \ differentiate\ \>", "Text"], Cell[BoxData[ \(res\ = \ D[Exp[Cos[x]], \ x]\)], "Input"], Cell["\<\ and we'd like to find out the value of res when x is 1.2. We can \ do that with\ \>", "Text"], Cell[BoxData[ \(ReplaceAll[res, \ {x -> 1.2}]\)], "Input"], Cell["\<\ When an equation has multiple solutions, it is possible to pick \ them out individually. Thus\ \>", "Text"], Cell[BoxData[ \(solns\ = \ Solve[3.15*x^2\ - \ 24.2*x\ + \ 15\ == \ 0, \ x]\)], "Input"], Cell["We can access the first of the two solutions as", "Text"], Cell[BoxData[ \(solns[\([1]\)]\)], "Input"], Cell["and the second of the two solutions as", "Text"], Cell[BoxData[ \(solns[\([2]\)]\)], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["Vectors and matrices", "Subsection"], Cell[TextData[{ "Vectors and matrices are used extensively in linear algebra. ", StyleBox["Mathematica", FontSlant->"Italic"], " will do all of the standard operations on them." }], "Text"], Cell["Now we can define a vector with", "Text"], Cell[BoxData[ \(v\ = \ {1, 2, 3}\)], "Input"], Cell[TextData[{ "We can ask ", StyleBox["Mathematica", FontSlant->"Italic"], " to display the vector using more standard notation by using the function \ \"MatrixForm\"." }], "Text"], Cell[BoxData[ \(MatrixForm[v]\)], "Input"], Cell["We can define a 3x3 matrix with", "Text"], Cell[BoxData[ \(m\ = \ {{1, 2, 3}, {4, 5, 4}, {3, 2, 1}}\)], "Input"], Cell["and display it with", "Text"], Cell[BoxData[ \(MatrixForm[m]\)], "Input"], Cell["\<\ Let's do some operations on vectors and matrices. We can do \ addition,\ \>", "Text"], Cell[BoxData[{ \(MatrixForm[v + v]\), \(MatrixForm[m + m]\)}], "Input"], Cell["matrix multiplication,", "Text"], Cell[BoxData[{ \(MatrixForm[m.m]\), \(MatrixForm[m.v]\)}], "Input"], Cell["and dot product", "Text"], Cell[BoxData[ \(MatrixForm[v\ .\ v]\)], "Input"], Cell["We can transpose", "Text"], Cell[BoxData[ \(MatrixForm[Transpose[m]]\)], "Input"], Cell["or invert", "Text"], Cell[BoxData[{ \(minverse\ = \ Inverse[m]\), \(MatrixForm[m\ .\ minverse]\)}], "Input"], Cell["\<\ Much more is possible. Consult the on-line documentation for \ pointers.\ \>", "Text"] }, Closed]] }, Open ]] }, FrontEndVersion->"X 3.0", ScreenRectangle->{{0, 1280}, {0, 1024}}, WindowSize->{520, 600}, WindowMargins->{{Automatic, 258}, {Automatic, 32}} ] (*********************************************************************** Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. ***********************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[1731, 51, 39, 0, 54, "Section"], Cell[1773, 53, 312, 7, 68, "Text"], Cell[CellGroupData[{ Cell[2110, 64, 34, 0, 45, "Subsection"], Cell[2147, 66, 407, 9, 86, "Text"] }, Closed]], Cell[CellGroupData[{ Cell[2591, 80, 46, 0, 29, "Subsection"], Cell[2640, 82, 213, 5, 50, "Text"], Cell[2856, 89, 56, 1, 27, "Input"], Cell[2915, 92, 19, 0, 32, "Text"], Cell[2937, 94, 66, 1, 27, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[3040, 100, 38, 0, 29, "Subsection"], Cell[3081, 102, 527, 9, 104, "Text"], Cell[3611, 113, 125, 3, 50, "Text"], Cell[3739, 118, 78, 1, 27, "Input"], Cell[3820, 121, 92, 3, 32, "Text"], Cell[3915, 126, 81, 1, 27, "Input"], Cell[3999, 129, 47, 0, 32, "Text"], Cell[4049, 131, 77, 1, 27, "Input"], Cell[4129, 134, 58, 0, 32, "Text"], Cell[4190, 136, 77, 1, 27, "Input"], Cell[4270, 139, 172, 5, 50, "Text"], Cell[4445, 146, 104, 2, 27, "Input"], Cell[4552, 150, 54, 0, 32, "Text"], Cell[4609, 152, 104, 2, 27, "Input"], Cell[4716, 156, 94, 3, 32, "Text"], Cell[4813, 161, 60, 1, 27, "Input"], Cell[4876, 164, 135, 3, 50, "Text"], Cell[5014, 169, 70, 1, 27, "Input"], Cell[5087, 172, 69, 0, 32, "Text"], Cell[5159, 174, 70, 1, 27, "Input"], Cell[5232, 177, 44, 0, 32, "Text"] }, Closed]], Cell[CellGroupData[{ Cell[5313, 182, 30, 0, 29, "Subsection"], Cell[5346, 184, 117, 3, 32, "Text"], Cell[5466, 189, 52, 1, 27, "Input"], Cell[5521, 192, 72, 0, 32, "Text"], Cell[5596, 194, 72, 1, 27, "Input"], Cell[5671, 197, 72, 0, 32, "Text"], Cell[5746, 199, 84, 1, 27, "Input"], Cell[5833, 202, 57, 0, 32, "Text"], Cell[5893, 204, 60, 1, 27, "Input"], Cell[5956, 207, 167, 4, 50, "Text"], Cell[6126, 213, 72, 1, 27, "Input"], Cell[6201, 216, 357, 7, 68, "Text"], Cell[6561, 225, 73, 1, 27, "Input"], Cell[6637, 228, 90, 3, 32, "Text"], Cell[6730, 233, 65, 1, 27, "Input"], Cell[6798, 236, 33, 0, 32, "Text"], Cell[6834, 238, 72, 1, 27, "Input"], Cell[6909, 241, 41, 0, 32, "Text"] }, Closed]], Cell[CellGroupData[{ Cell[6987, 246, 34, 0, 29, "Subsection"], Cell[7024, 248, 211, 6, 50, "Text"], Cell[7238, 256, 75, 1, 27, "Input"], Cell[7316, 259, 114, 3, 32, "Text"], Cell[7433, 264, 59, 1, 27, "Input"], Cell[7495, 267, 103, 3, 32, "Text"], Cell[7601, 272, 62, 1, 27, "Input"], Cell[7666, 275, 104, 3, 32, "Text"], Cell[7773, 280, 62, 1, 27, "Input"], Cell[7838, 283, 118, 3, 32, "Text"], Cell[7959, 288, 99, 2, 27, "Input"], Cell[8061, 292, 63, 0, 32, "Text"], Cell[8127, 294, 47, 1, 27, "Input"], Cell[8177, 297, 54, 0, 32, "Text"], Cell[8234, 299, 47, 1, 27, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[8318, 305, 42, 0, 29, "Subsection"], Cell[8363, 307, 200, 5, 50, "Text"], Cell[8566, 314, 47, 0, 32, "Text"], Cell[8616, 316, 50, 1, 27, "Input"], Cell[8669, 319, 192, 6, 50, "Text"], Cell[8864, 327, 46, 1, 27, "Input"], Cell[8913, 330, 47, 0, 32, "Text"], Cell[8963, 332, 74, 1, 27, "Input"], Cell[9040, 335, 35, 0, 32, "Text"], Cell[9078, 337, 46, 1, 27, "Input"], Cell[9127, 340, 96, 3, 32, "Text"], Cell[9226, 345, 80, 2, 43, "Input"], Cell[9309, 349, 38, 0, 32, "Text"], Cell[9350, 351, 76, 2, 43, "Input"], Cell[9429, 355, 31, 0, 32, "Text"], Cell[9463, 357, 52, 1, 27, "Input"], Cell[9518, 360, 32, 0, 32, "Text"], Cell[9553, 362, 57, 1, 27, "Input"], Cell[9613, 365, 25, 0, 32, "Text"], Cell[9641, 367, 97, 2, 43, "Input"], Cell[9741, 371, 96, 3, 32, "Text"] }, Closed]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)