(***********************************************************************
Mathematica-Compatible Notebook
This notebook can be used on any computer system with Mathematica 4.0,
MathReader 4.0, or any compatible application. The data for the notebook
starts with the line containing 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[ 13993, 533]*)
(*NotebookOutlinePosition[ 14655, 557]*)
(* CellTagsIndexPosition[ 14611, 553]*)
(*WindowFrame->Normal*)
Notebook[{
Cell["Introduction to Mathematica", "Subsubtitle",
FontSize->24],
Cell["\<\
A first draft for Physics 221A, Fall 2004.
Written by Daniel Larson (05-Sep-2004)\
\>", "Subsection",
FontSize->16],
Cell[CellGroupData[{
Cell["General advice", "Subsubsection",
FontSize->14],
Cell[TextData[{
"The purpose of this notebook is to let you quickly see some examples of \
how one can do calculations using ",
StyleBox["Mathematica",
FontSlant->"Italic"],
". As with any computer program, there are quirks of syntax that take some \
getting used to, and the easiest way to learn the ins and outs is by trying \
to use the program yourself. The fastest way to start doing that is by \
looking at and starting to modify some examples."
}], "Text",
FontSize->14],
Cell["\<\
These examples are by no means comprehensive. If you would like a \
more detailed introduction, open up the \"Help Browser\" in the \"Help\" menu \
at the upper right of the window; then choose the \"Getting Started\" \
subtopic.\
\>", "Text",
FontSize->14],
Cell[TextData[{
StyleBox["Mathematica",
FontSlant->"Italic"],
" is very powerful, but it can also make calculations a bit opaque. I \
recommend keeping your notebooks as neat as possible and using descriptive \
variable names. If the result of your computation looks mysterious, you \
really had better double check to make sure you know what ",
StyleBox["Mathematica",
FontSlant->"Italic"],
" is doing. I strongly recommend doing every step in multiple ways so that \
you are certain that ",
StyleBox["Mathematica",
FontSlant->"Italic"],
" is doing exactly what you want it to."
}], "Text",
FontSize->14],
Cell["\<\
To collapse these ramblings, and to view the examples below, simply \
double click in the right-most blue bracket at the right of this \
screen.\
\>", "Text",
FontSize->14]
}, Open ]],
Cell[CellGroupData[{
Cell["Basic Calculations and Variables", "Subsubsection",
FontSize->14],
Cell["\<\
Simple manipulations in Mathematica are relatively intuitive. You \
provide input by typing in a \"cell\", indicated by the brackets on the right \
side of the window. To evaluate the cell, you type \"Shift + Enter\". (For \
these examples, I've provided the input... all you have to do is click in the \
cell next to the numbers and then evaluate with \"Shift+Enter\".)\
\>", "Text",\
FontSize->14],
Cell[BoxData[
\(1 + 1\)], "Input"],
Cell[BoxData[
\(8*5/\((\(3!\))\) - 17\)], "Input"],
Cell[TextData[{
"Mathematica likes to give exact expressions if it can. If you want \
numerical values, you use the function \"N\", which can be done in several \
ways. Functions in ",
StyleBox["Mathematica",
FontSlant->"Italic"],
" always start with a capital letter and take arguments in square brackets \
\"[...]\". Simple functions can come after an expression using \"//\" which \
acts kind of like a pipe \"|\" in unix."
}], "Text",
FontSize->14],
Cell[BoxData[
\(N[8*5/\((\(3!\))\) - 17]\)], "Input"],
Cell[BoxData[
\(8*5/\((\(3!\))\) - 17 // N\)], "Input"],
Cell["\<\
If you want to learn more about a function, just open up the help \
browser (upper right menu) where you will find tons of information and \
documentation.\
\>", "Text",
FontSize->14],
Cell["\<\
Variables are any strings starting with a letter and that do not \
have any spaces. They can be assigned values using a single \"=\".\
\>", \
"Text",
FontSize->14],
Cell[BoxData[
\(a\)], "Input"],
Cell[BoxData[
\(a = 3\)], "Input"],
Cell[BoxData[
\(a\)], "Input"],
Cell[BoxData[{
\(myNewVariable1 = \ 10^2\), "\[IndentingNewLine]",
\(myNewVariable2 = 3*5\)}], "Input"],
Cell[BoxData[
\(myNewVariable1/myNewVariable2\)], "Input"],
Cell["\<\
Spaces between variable names represents multiplication, so be \
careful!\
\>", "Text"],
Cell[BoxData[
\(b = 8\)], "Input"],
Cell[BoxData[{
\(a\), "\[IndentingNewLine]",
\(b\), "\[IndentingNewLine]",
\(ab\), "\[IndentingNewLine]",
\(a\ b\)}], "Input"],
Cell[BoxData[
\(a\ b\ \[Equal] \ a*b\)], "Input"],
Cell["\<\
A couple of variables are already defined, such as \"I\", \"E\", \
and \"Pi\".\
\>", "Text"],
Cell[BoxData[{
\(I\), "\[IndentingNewLine]",
\(E\), "\[IndentingNewLine]",
\(Pi\)}], "Input"],
Cell[BoxData[{
\(N[E]\), "\[IndentingNewLine]",
\(Pi // N\)}], "Input"],
Cell["You can't reassign these variables:", "Text"],
Cell[BoxData[
\(Pi = 7\)], "Input"],
Cell[BoxData[
\(Pi // N\)], "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell["Plotting and Defining Functions", "Subsubsection",
FontSize->14],
Cell["\<\
Here are some examples to show you the names of some Mathematica \
functions and how to use them. There are intentionally some commands that \
produce errors... hopefully you can see why and how I fixed them.\
\>", "Text",\
FontSize->14],
Cell[BoxData[
\(Sin[Pi/3]\)], "Input"],
Cell[BoxData[
\(Plot[Sin[x], {x, 0, 4 Pi}]\)], "Input"],
Cell[BoxData[
\(taylor1 = Series[Sin[x], {x, 0, 3}]\)], "Input"],
Cell[BoxData[
\(Plot[taylor1, {x, 0, 4 Pi}]\)], "Input"],
Cell[BoxData[
\(taylor2 = Normal[taylor1]\)], "Input"],
Cell[BoxData[
\(Plot[taylor2, {x, 0, 4 Pi}, Frame \[Rule] True]\)], "Input"],
Cell[BoxData[
\(taylor3 = Normal[Series[Sin[x], {x, 0, 10}]]\)], "Input"],
Cell[BoxData[
\(Plot[{Sin[x], taylor2, taylor3}, {x, 0, 2 Pi}, Axes \[Rule] False, \
PlotStyle \[Rule] {RGBColor[1, 0, 0], RGBColor[0, 1, 0],
RGBColor[0, 0, 1]}]\)], "Input"],
Cell["\<\
You can also define functions. Here is an example to show you the \
syntax.\
\>", "Text",
FontSize->14],
Cell[BoxData[
\(myFunction[x_, y_] = Sin[x*y]\)], "Input"],
Cell[BoxData[
\(myFunction[Pi, 1]\)], "Input"],
Cell[BoxData[{
\(myFunction[Pi, 1/2]\), "\[IndentingNewLine]",
\(myFunction[a, c]\)}], "Input"],
Cell["(Remember, we defined \"a\" earlier!)", "Text",
FontSize->14],
Cell[BoxData[
\(Plot3D[myFunction[x, y], {x, 0, 3 Pi/2}, {y, 0, 3 Pi/2}]\)], "Input"],
Cell[BoxData[
\(ContourPlot[
myFunction[x, y], {x, 0, 3 Pi/2}, {y, 0, 3 Pi/2}]\)], "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell["Derivatives and Integrals", "Subsubsection",
FontSize->14],
Cell["Let's clear the variable \"a\":", "Text"],
Cell[BoxData[
\(a\)], "Input"],
Cell[BoxData[
\(a =. \)], "Input"],
Cell[BoxData[
\(a\)], "Input"],
Cell[BoxData[
\(f[z_] = z^5 Exp[\(-a\)\ z]\)], "Input"],
Cell[BoxData[
\(f[w]\)], "Input"],
Cell[BoxData[
\(D[f[w], w]\)], "Input"],
Cell[BoxData[
\(Integrate[f[r], {r, 0, Infinity}]\)], "Input"],
Cell["\<\
Mathematica tries to be as exact as possible, not necessarily \
assuming that \"a\" is real.\
\>", "Text",
FontSize->14],
Cell[BoxData[
\(Integrate[f[r], {r, 0, Infinity},
Assumptions \[Rule] {a > 0}]\)], "Input"],
Cell[BoxData[
\(Integrate[f[r], {r, 0, 5}, Assumptions \[Rule] {a > 0}]\)], "Input"],
Cell[BoxData[
\(Expand[%]\)], "Input"],
Cell["\<\
\"%\" means the last output... be very careful, because \"%\" \
changes every time, so if you go back later and re-evaluate the cell you'll \
likely get a different result.\
\>", "Text",
FontSize->14],
Cell[BoxData[
\(Integrate[Exp[\(-a\)\ x^2/2], {x, 0, \[Infinity]},
Assumptions \[Rule] {a > 0}]\)], "Input"],
Cell[BoxData[
\(Simplify[%]\)], "Input"],
Cell[BoxData[
\(FullSimplify[%]\)], "Input"],
Cell[BoxData[
\(PowerExpand[%] // FullSimplify\)], "Input"],
Cell["\<\
The above expression is a notorious example of an expression that \
Mathematica doesn't simplify well... the usual tricks \"Simplify\", \
\"FullSimplify\", and \"PowerExpand\" don't work and I haven't yet found \
something that works.\
\>", "Text",
FontSize->14],
Cell[BoxData[
\(value =
Integrate[Exp[\(-a\)\ x^2/2], {x, 0, 10},
Assumptions \[Rule] {a > 0}]\)], "Input"],
Cell["\<\
Don't worry about the error message... Mathematica variables are \
case sensitive. \"Value\" has some built-in meaning, but \"value\" is \
distinct.\
\>", "Text",
FontSize->14],
Cell[BoxData[
\(N[value]\)], "Input"],
Cell["\<\
The \"/.\" allows you take an expression and substitute values for \
the variables without having to actually assign the variable a value. The \
right arrow is formed by a \"-\" and \">\".\
\>", "Text",
FontSize->14],
Cell[BoxData[
\(value /. a \[Rule] 3\)], "Input"],
Cell[BoxData[
\(N[value /. a \[Rule] 3]\)], "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell["Equation solving", "Subsubsection",
FontSize->14],
Cell[BoxData[
\(soln = Solve[x^2 \[Equal] 5, x]\)], "Input"],
Cell["\<\
Mathematica likes to store things in lists of lists. You can \
extract an element using double brackets, \"[[...]]\".\
\>", "Text",
FontSize->14],
Cell[BoxData[{
\(soln\), "\[IndentingNewLine]",
\(soln[\([1]\)]\), "\[IndentingNewLine]",
\(soln[\([2]\)]\), "\[IndentingNewLine]",
\(soln[\([2, 1]\)]\)}], "Input"],
Cell[BoxData[
\(x^2 /. soln[\([2]\)]\)], "Input"],
Cell["\<\
You can also solve differential equations, with or without initial \
conditions. Mathematica understands \"primes\" as derivatives. The \"C[1]\" \
in the output is a constant of integration.\
\>", "Text",
FontSize->14],
Cell[BoxData[
\(DSolve[\(x''\)[t] \[Equal] \(-k\)*x[t], x[t], t]\)], "Input"],
Cell[BoxData[
\(DSolve[{\(x''\)[t] \[Equal] \(-k\)*x[t], \(x'\)[0] \[Equal] 0,
x[0] \[Equal] 3}, x[t], t]\)], "Input"],
Cell["\<\
You can also solve equations numerically. The result is an \
\"interpolating function\" that you can evaluate at a specific point or plot.\
\
\>", "Text",
FontSize->14],
Cell[BoxData[
\(result =
NDSolve[{\(x''\)[t] == \(-3\)\ x[t] + y[t], \(y''\)[
t] == \(-y[t]\), \(x'\)[0] \[Equal] 1,
x[0] == 0, \(y'\)[0] \[Equal] 0, y[0] \[Equal] 1}, {x[t],
y[t]}, {t, 0, 10}]\)], "Input"],
Cell[BoxData[{
\(\(x[t] /. result\) /. t \[Rule] 7\), "\[IndentingNewLine]",
\(\(x[t] /. result[\([1]\)]\) /. t \[Rule] 7\), "\[IndentingNewLine]",
\(\(y[t] /. result[\([1]\)]\) /. t \[Rule] 7\)}], "Input"],
Cell[BoxData[
\(Plot[{x[t], y[t]}, {t, 0, 10}]\)], "Input"],
Cell[BoxData[
\(Plot[{x[t] /. result, y[t] /. result}, {t, 0, 10}]\)], "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell["Matrices and vectors", "Subsubsection",
FontSize->14],
Cell["A matrix can be represented by a list of lists.", "Text",
FontSize->14],
Cell[BoxData[
\(myMatrix = {{a, b}, {c, d}}\)], "Input"],
Cell["Oops... maybe \"b\" is still assigned a value.", "Text",
FontSize->14],
Cell[BoxData[{
\(b =. \), "\[IndentingNewLine]",
\(b\), "\[IndentingNewLine]",
\(myMatrix\)}], "Input"],
Cell["\<\
Hmmm... \"b\" is redefined, but we haven't redefined \
\"myMatrix\".\
\>", "Text",
FontSize->14],
Cell[BoxData[
\(myMatrix = {{a, b}, {c, d}}\)], "Input"],
Cell["That's better. ", "Text",
FontSize->14],
Cell[BoxData[
\(myMatrix // MatrixForm\)], "Input"],
Cell[BoxData[
\(vec1 = {x, y}\)], "Input"],
Cell["\<\
Note the difference between the two following types of \
multiplication!\
\>", "Text",
FontSize->14],
Cell[BoxData[{
\(myMatrix*vec1\), "\[IndentingNewLine]",
\(% // MatrixForm\)}], "Input"],
Cell[BoxData[{
\(myMatrix . vec1\), "\[IndentingNewLine]",
\(% // MatrixForm\)}], "Input"],
Cell[BoxData[
\(Transpose[myMatrix] // MatrixForm\)], "Input"],
Cell[BoxData[
\(evals = Eigenvalues[myMatrix]\)], "Input"],
Cell[BoxData[
\(Eigenvectors[myMatrix]\)], "Input"],
Cell[BoxData[
\(together = Eigensystem[myMatrix]\)], "Input"],
Cell[BoxData[
\(\(?Together\)\)], "Input"],
Cell["\<\
(Nice to know! Maybe that \"possible spelling error\" isn't as \
annoying as I thought... it helps you learn about new commands!)\
\>", "Text",\
FontSize->14],
Cell[BoxData[{
\(together[\([1]\)]\), "\[IndentingNewLine]",
\(together[\([2]\)]\), "\[IndentingNewLine]",
\(together[\([2, 1]\)] // MatrixForm\)}], "Input"],
Cell[BoxData[
\(myMatrix . together[\([2, 1]\)] -
evals[\([1]\)]*together[\([2, 1]\)]\)], "Input"],
Cell[BoxData[
\(Simplify[%]\)], "Input"]
}, Closed]]
},
FrontEndVersion->"4.0 for X",
ScreenRectangle->{{0, 1280}, {0, 854}},
CellGrouping->Manual,
WindowSize->{520, 600},
WindowMargins->{{256, Automatic}, {Automatic, 68}}
]
(***********************************************************************
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[1717, 49, 66, 1, 66, "Subsubtitle"],
Cell[1786, 52, 127, 4, 68, "Subsection"],
Cell[CellGroupData[{
Cell[1938, 60, 55, 1, 43, "Subsubsection"],
Cell[1996, 63, 489, 10, 109, "Text"],
Cell[2488, 75, 269, 6, 71, "Text"],
Cell[2760, 83, 631, 15, 128, "Text"],
Cell[3394, 100, 184, 5, 52, "Text"]
}, Open ]],
Cell[CellGroupData[{
Cell[3615, 110, 73, 1, 43, "Subsubsection"],
Cell[3691, 113, 412, 8, 140, "Text"],
Cell[4106, 123, 38, 1, 27, "Input"],
Cell[4147, 126, 54, 1, 27, "Input"],
Cell[4204, 129, 464, 10, 140, "Text"],
Cell[4671, 141, 57, 1, 27, "Input"],
Cell[4731, 144, 59, 1, 27, "Input"],
Cell[4793, 147, 195, 5, 77, "Text"],
Cell[4991, 154, 175, 5, 56, "Text"],
Cell[5169, 161, 34, 1, 27, "Input"],
Cell[5206, 164, 38, 1, 27, "Input"],
Cell[5247, 167, 34, 1, 27, "Input"],
Cell[5284, 170, 112, 2, 43, "Input"],
Cell[5399, 174, 62, 1, 27, "Input"],
Cell[5464, 177, 97, 3, 34, "Text"],
Cell[5564, 182, 38, 1, 27, "Input"],
Cell[5605, 185, 145, 4, 75, "Input"],
Cell[5753, 191, 54, 1, 27, "Input"],
Cell[5810, 194, 102, 3, 34, "Text"],
Cell[5915, 199, 107, 3, 59, "Input"],
Cell[6025, 204, 80, 2, 43, "Input"],
Cell[6108, 208, 51, 0, 34, "Text"],
Cell[6162, 210, 39, 1, 27, "Input"],
Cell[6204, 213, 40, 1, 27, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[6281, 219, 72, 1, 27, "Subsubsection"],
Cell[6356, 222, 250, 6, 98, "Text"],
Cell[6609, 230, 42, 1, 27, "Input"],
Cell[6654, 233, 60, 1, 27, "Input"],
Cell[6717, 236, 68, 1, 27, "Input"],
Cell[6788, 239, 61, 1, 27, "Input"],
Cell[6852, 242, 58, 1, 27, "Input"],
Cell[6913, 245, 81, 1, 27, "Input"],
Cell[6997, 248, 77, 1, 27, "Input"],
Cell[7077, 251, 197, 3, 75, "Input"],
Cell[7277, 256, 115, 4, 56, "Text"],
Cell[7395, 262, 62, 1, 27, "Input"],
Cell[7460, 265, 50, 1, 27, "Input"],
Cell[7513, 268, 104, 2, 43, "Input"],
Cell[7620, 272, 69, 1, 35, "Text"],
Cell[7692, 275, 91, 1, 27, "Input"],
Cell[7786, 278, 103, 2, 43, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[7926, 285, 66, 1, 27, "Subsubsection"],
Cell[7995, 288, 47, 0, 34, "Text"],
Cell[8045, 290, 34, 1, 27, "Input"],
Cell[8082, 293, 38, 1, 27, "Input"],
Cell[8123, 296, 34, 1, 27, "Input"],
Cell[8160, 299, 60, 1, 27, "Input"],
Cell[8223, 302, 37, 1, 27, "Input"],
Cell[8263, 305, 43, 1, 27, "Input"],
Cell[8309, 308, 66, 1, 27, "Input"],
Cell[8378, 311, 132, 4, 56, "Text"],
Cell[8513, 317, 102, 2, 27, "Input"],
Cell[8618, 321, 88, 1, 27, "Input"],
Cell[8709, 324, 42, 1, 27, "Input"],
Cell[8754, 327, 212, 5, 77, "Text"],
Cell[8969, 334, 119, 2, 27, "Input"],
Cell[9091, 338, 44, 1, 27, "Input"],
Cell[9138, 341, 48, 1, 27, "Input"],
Cell[9189, 344, 63, 1, 27, "Input"],
Cell[9255, 347, 275, 6, 98, "Text"],
Cell[9533, 355, 127, 3, 43, "Input"],
Cell[9663, 360, 188, 5, 56, "Text"],
Cell[9854, 367, 41, 1, 27, "Input"],
Cell[9898, 370, 228, 5, 77, "Text"],
Cell[10129, 377, 53, 1, 27, "Input"],
Cell[10185, 380, 56, 1, 27, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[10278, 386, 57, 1, 27, "Subsubsection"],
Cell[10338, 389, 64, 1, 27, "Input"],
Cell[10405, 392, 157, 4, 56, "Text"],
Cell[10565, 398, 183, 4, 75, "Input"],
Cell[10751, 404, 53, 1, 27, "Input"],
Cell[10807, 407, 230, 5, 77, "Text"],
Cell[11040, 414, 81, 1, 27, "Input"],
Cell[11124, 417, 131, 2, 43, "Input"],
Cell[11258, 421, 180, 5, 56, "Text"],
Cell[11441, 428, 253, 5, 75, "Input"],
Cell[11697, 435, 220, 3, 59, "Input"],
Cell[11920, 440, 63, 1, 27, "Input"],
Cell[11986, 443, 83, 1, 27, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[12106, 449, 61, 1, 27, "Subsubsection"],
Cell[12170, 452, 79, 1, 35, "Text"],
Cell[12252, 455, 60, 1, 27, "Input"],
Cell[12315, 458, 78, 1, 35, "Text"],
Cell[12396, 461, 117, 3, 59, "Input"],
Cell[12516, 466, 108, 4, 35, "Text"],
Cell[12627, 472, 60, 1, 27, "Input"],
Cell[12690, 475, 47, 1, 35, "Text"],
Cell[12740, 478, 55, 1, 27, "Input"],
Cell[12798, 481, 46, 1, 27, "Input"],
Cell[12847, 484, 112, 4, 35, "Text"],
Cell[12962, 490, 97, 2, 43, "Input"],
Cell[13062, 494, 99, 2, 43, "Input"],
Cell[13164, 498, 66, 1, 27, "Input"],
Cell[13233, 501, 62, 1, 27, "Input"],
Cell[13298, 504, 55, 1, 27, "Input"],
Cell[13356, 507, 65, 1, 27, "Input"],
Cell[13424, 510, 46, 1, 27, "Input"],
Cell[13473, 513, 171, 5, 56, "Text"],
Cell[13647, 520, 171, 3, 59, "Input"],
Cell[13821, 525, 109, 2, 27, "Input"],
Cell[13933, 529, 44, 1, 27, "Input"]
}, Closed]]
}
]
*)
(***********************************************************************
End of Mathematica Notebook file.
***********************************************************************)