(* Content-type: application/mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 7.0' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 145, 7] NotebookDataLength[ 4850, 167] NotebookOptionsPosition[ 3868, 132] NotebookOutlinePosition[ 4256, 149] CellTagsIndexPosition[ 4213, 146] WindowFrame->Normal*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell["Calculate the Area of a Circle", "Subtitle", TextAlignment->Center, TextJustification->0], Cell["\<\ First a simple function definition. Note the []'s and the capitalized Pi. \ What does := do? (That means \"delayed evaluation.\" More later!) Why does \ radius on the left side have a \"_\" after it? (It means that radius on the \ right side is a dummy variable. The \"_\" defines a \"pattern.\" More \ later!)\ \>", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"area", "[", "radius_", "]"}], " ", ":=", " ", RowBox[{"Pi", " ", RowBox[{"radius", "^", "2"}]}]}]], "Input"], Cell["Test it out for a case where we know the answer.", "Text"], Cell[BoxData[ RowBox[{"area", "[", "1", "]"}]], "Input"], Cell["\<\ But suppose we want the answer to be numerical (i.e., with some number of \ digits). Here's one way:\ \>", "Text", CellChangeTimes->{{3.525767054963703*^9, 3.525767056145114*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"Narea", "[", "radius_", "]"}], " ", ":=", " ", RowBox[{"N", "[", RowBox[{"Pi", " ", RowBox[{"radius", "^", "2"}]}], "]"}]}]], "Input"], Cell["\<\ But what if we want \"n\" digits precision? Here's a new version:\ \>", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"Narea", "[", RowBox[{"radius_", ",", "n_"}], "]"}], " ", ":=", " ", RowBox[{"N", "[", RowBox[{ RowBox[{"Pi", " ", RowBox[{"radius", "^", "2"}]}], ",", "n"}], "]"}]}]], "Input"], Cell["\<\ Always check your definition with a \"?\" in front of the name:\ \>", "Text"], Cell[BoxData[ RowBox[{"?", "Narea"}]], "Input"], Cell[TextData[{ "We see that BOTH definitions are active. How does ", StyleBox["Mathematica", FontSlant->"Italic"], " know which one to use? Answer: By the number of arguments given to the \ function! Try it:" }], "Text"], Cell[BoxData[ RowBox[{"Narea", "[", "1", "]"}]], "Input"], Cell[BoxData[ RowBox[{"Narea", "[", RowBox[{"1", ",", "10"}], "]"}]], "Input"], Cell["\<\ Finally, let's write a \"program\" that asks the user for the radius. First \ the definition. The \"Module\" ensures that \"radius\" and \"area\" are \ local (as opposed to global) variables. Note the use of semicolons. (More \ later!)\ \>", "Text", CellChangeTimes->{{3.525767725999339*^9, 3.5257677304709663`*^9}, { 3.525767765133114*^9, 3.525767773020267*^9}}], Cell[BoxData[ RowBox[{"myarea", " ", ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"radius", ",", "area"}], "}"}], ",", " ", "\[IndentingNewLine]", RowBox[{ RowBox[{"radius", " ", "=", " ", RowBox[{"Input", "[", "\"\\"", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"area", " ", "=", " ", RowBox[{"N", "[", RowBox[{"Pi", " ", "*", " ", RowBox[{"radius", "^", "2"}]}], "]"}]}], ";", " ", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", " ", "radius", ",", " ", "\"\< is \>\"", ",", " ", "area"}], "]"}]}]}], "]"}]}]], "Input", CellChangeTimes->{{3.525767555615404*^9, 3.5257676959138927`*^9}, { 3.5257677437931223`*^9, 3.5257677584902067`*^9}}], Cell["Now run it.", "Text"], Cell[BoxData["myarea"], "Input"], Cell["That's all for now!", "Text"] }, Open ]] }, WindowToolbars->"EditBar", WindowSize->{924, 800}, WindowMargins->{{Automatic, 160}, {Automatic, 0}}, ShowSelection->True, FrontEndVersion->"7.0 for Mac OS X x86 (32-bit) (February 18, 2009)", StyleDefinitions->"Default.nb" ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[567, 22, 97, 2, 46, "Subtitle"], Cell[667, 26, 342, 6, 41, "Text"], Cell[1012, 34, 150, 4, 27, "Input"], Cell[1165, 40, 64, 0, 26, "Text"], Cell[1232, 42, 57, 1, 27, "Input"], Cell[1292, 45, 191, 4, 26, "Text"], Cell[1486, 51, 181, 5, 27, "Input"], Cell[1670, 58, 90, 2, 26, "Text"], Cell[1763, 62, 232, 7, 27, "Input"], Cell[1998, 71, 87, 2, 26, "Text"], Cell[2088, 75, 48, 1, 27, "Input"], Cell[2139, 78, 229, 6, 26, "Text"], Cell[2371, 86, 58, 1, 27, "Input"], Cell[2432, 89, 82, 2, 27, "Input"], Cell[2517, 93, 380, 7, 41, "Text"], Cell[2900, 102, 849, 21, 73, "Input"], Cell[3752, 125, 27, 0, 26, "Text"], Cell[3782, 127, 32, 0, 27, "Input"], Cell[3817, 129, 35, 0, 26, "Text"] }, Open ]] } ] *) (* End of internal cache information *)