(* Content-type: application/mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 6.0' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 145, 7] NotebookDataLength[ 31607, 949] NotebookOptionsPosition[ 27568, 815] NotebookOutlinePosition[ 27927, 831] CellTagsIndexPosition[ 27884, 828] WindowFrame->Normal ContainsDynamic->False*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell["Examples using functions", "Subsubtitle", CellChangeTimes->{{3.416577557006275*^9, 3.416577591693158*^9}, 3.416654642129732*^9}, Background->RGBColor[0, 1, 0]], Cell[TextData[StyleBox["Joe Sloan April 7, 2008", FontSize->10]], "Subsubtitle", CellChangeTimes->{{3.416577567351078*^9, 3.416577576877472*^9}}], Cell[CellGroupData[{ Cell["Simple functions", "Subsubsection", CellChangeTimes->{{3.416577662847951*^9, 3.416577665766445*^9}, 3.416654713307067*^9}], Cell[BoxData[ RowBox[{ RowBox[{"P", "[", RowBox[{"p0_", ",", " ", "r_", ",", " ", "t_"}], "]"}], " ", ":=", " ", RowBox[{"p0", " ", "*", " ", RowBox[{"Exp", "[", RowBox[{"r", " ", "*", " ", "t"}], "]"}]}]}]], "Input", CellChangeTimes->{{3.4165771051479607`*^9, 3.416577120388797*^9}, { 3.4165771574736423`*^9, 3.416577209610443*^9}}], Cell["Functions ease use when doing repetitive calculations", "Text", CellChangeTimes->{{3.416654718690641*^9, 3.416654732691285*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"P", "[", RowBox[{"1000", ",", " ", ".1", ",", " ", "2"}], "]"}]], "Input", CellChangeTimes->{{3.416577214961321*^9, 3.4165772286419773`*^9}}], Cell[BoxData["1221.40275816017`"], "Output", CellChangeTimes->{3.416577229960679*^9, 3.416655246569488*^9, 3.416656525707217*^9}] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"P", "[", RowBox[{"1000", ",", " ", ".1", ",", " ", "4"}], "]"}]], "Input", CellChangeTimes->{{3.416577214961321*^9, 3.416577247300802*^9}}], Cell[BoxData["1491.8246976412704`"], "Output", CellChangeTimes->{3.416577248246811*^9, 3.416655246612979*^9, 3.416656525752129*^9}] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"P", "[", RowBox[{"1000", ",", " ", ".1", ",", " ", "6"}], "]"}]], "Input", CellChangeTimes->{{3.416577214961321*^9, 3.41657725314075*^9}}], Cell[BoxData["1822.118800390509`"], "Output", CellChangeTimes->{3.4165772537818727`*^9, 3.41665524665285*^9, 3.416656525787698*^9}] }, Open ]], Cell["Here is an example using a loop.", "Text", CellChangeTimes->{{3.416654917981347*^9, 3.416654930627777*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"Do", "[", RowBox[{ RowBox[{"Print", "[", RowBox[{"P", "[", RowBox[{"1000", ",", " ", ".1", ",", " ", "t"}], "]"}], "]"}], ",", " ", RowBox[{"{", RowBox[{"t", ",", " ", "0", ",", " ", "2", ",", " ", ".2"}], "}"}]}], "]"}]], "Input", CellChangeTimes->{{3.416577263442134*^9, 3.41657733847262*^9}, 3.416656492118594*^9}], Cell[CellGroupData[{ Cell[BoxData["1000.`"], "Print", CellChangeTimes->{3.416655246693139*^9, 3.4166564947007017`*^9, 3.416656525815775*^9}], Cell[BoxData["1020.2013400267558`"], "Print", CellChangeTimes->{3.416655246693139*^9, 3.4166564947007017`*^9, 3.416656525818027*^9}], Cell[BoxData["1040.8107741923882`"], "Print", CellChangeTimes->{3.416655246693139*^9, 3.4166564947007017`*^9, 3.4166565258211317`*^9}], Cell[BoxData["1061.8365465453596`"], "Print", CellChangeTimes->{3.416655246693139*^9, 3.4166564947007017`*^9, 3.416656525824244*^9}], Cell[BoxData["1083.2870676749587`"], "Print", CellChangeTimes->{3.416655246693139*^9, 3.4166564947007017`*^9, 3.4166565258273277`*^9}], Cell[BoxData["1105.1709180756477`"], "Print", CellChangeTimes->{3.416655246693139*^9, 3.4166564947007017`*^9, 3.416656525852015*^9}], Cell[BoxData["1127.4968515793757`"], "Print", CellChangeTimes->{3.416655246693139*^9, 3.4166564947007017`*^9, 3.416656525908523*^9}], Cell[BoxData["1150.2737988572273`"], "Print", CellChangeTimes->{3.416655246693139*^9, 3.4166564947007017`*^9, 3.41665652593548*^9}], Cell[BoxData["1173.5108709918102`"], "Print", CellChangeTimes->{3.416655246693139*^9, 3.4166564947007017`*^9, 3.4166565259623613`*^9}], Cell[BoxData["1197.2173631218102`"], "Print", CellChangeTimes->{3.416655246693139*^9, 3.4166564947007017`*^9, 3.4166565259904957`*^9}], Cell[BoxData["1221.40275816017`"], "Print", CellChangeTimes->{3.416655246693139*^9, 3.4166564947007017`*^9, 3.4166565260148582`*^9}] }, Open ]] }, Open ]], Cell["\<\ You can repackage a function to set specific variables. Here we know the \ initial population and rate and only want to calculate the population at \ different times.\ \>", "Text", CellChangeTimes->{{3.416654757691312*^9, 3.416654806447981*^9}, { 3.416654864266086*^9, 3.41665488576309*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"f1", "[", "t_", "]"}], " ", ":=", " ", RowBox[{"P", "[", RowBox[{"1000", ",", " ", ".1", ",", " ", "t"}], "]"}]}]], "Input", CellChangeTimes->{{3.416577386803783*^9, 3.416577433268199*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"f1", "[", "1", "]"}]], "Input", CellChangeTimes->{{3.4165774418039513`*^9, 3.41657744581999*^9}}], Cell[BoxData["1105.1709180756477`"], "Output", CellChangeTimes->{3.416577446529358*^9, 3.4166552469742393`*^9, 3.416656526107581*^9}] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"f1", "[", "2", "]"}]], "Input", CellChangeTimes->{{3.4165774473507433`*^9, 3.4165774497559853`*^9}}], Cell[BoxData["1221.40275816017`"], "Output", CellChangeTimes->{3.416577450222321*^9, 3.416655247001696*^9, 3.416656526136327*^9}] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"f1", "[", "3", "]"}]], "Input", CellChangeTimes->{{3.416577450856583*^9, 3.416577454364056*^9}}], Cell[BoxData["1349.858807576003`"], "Output", CellChangeTimes->{3.416577454862514*^9, 3.416655247041757*^9, 3.4166565261763067`*^9}] }, Open ]], Cell["Some more functions.", "Text", CellChangeTimes->{{3.41665494251818*^9, 3.416654948396295*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"f2", "[", "t_", "]"}], " ", ":=", " ", RowBox[{"P", "[", RowBox[{"1000", ",", " ", ".2", ",", " ", "t"}], "]"}]}]], "Input", CellChangeTimes->{{3.416577386803783*^9, 3.416577433268199*^9}, { 3.4165774683748293`*^9, 3.416577471052786*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"dP", "[", "P_", "]"}], " ", ":=", " ", RowBox[{ RowBox[{"-", ".0004"}], " ", "*", " ", "P"}]}]], "Input", CellChangeTimes->{{3.4165775030182447`*^9, 3.416577530580942*^9}}], Cell["\<\ Although we interpret the following as a derivative, Mathematica just sees it \ as another function.\ \>", "Text", CellChangeTimes->{{3.416654972739423*^9, 3.416655017688265*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"dP", "[", RowBox[{"P_", ",", " ", "M_", ",", " ", "r_"}], "]"}], " ", ":=", " ", RowBox[{"r", " ", "*", "P", "*", RowBox[{"(", RowBox[{"1", " ", "-", " ", RowBox[{"P", "/", "M"}]}], ")"}]}]}]], "Input", CellChangeTimes->{{3.416577682673902*^9, 3.416577711640107*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"dP", "[", RowBox[{"100", ",", " ", "1000", ",", " ", ".1"}], "]"}]], "Input", CellChangeTimes->{{3.416577749474307*^9, 3.41657776247318*^9}}], Cell[BoxData["9.`"], "Output", CellChangeTimes->{3.416577763572886*^9, 3.416655247151794*^9, 3.416656526248423*^9}] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"dP", "[", RowBox[{"1005", ",", " ", "1000", ",", " ", ".1"}], "]"}]], "Input", CellChangeTimes->{{3.416577767581653*^9, 3.41657777740945*^9}}], Cell[BoxData[ RowBox[{"-", "0.5025000000000001`"}]], "Output", CellChangeTimes->{3.416577780402608*^9, 3.4166552472054873`*^9, 3.416656526283802*^9}] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"dP", "[", RowBox[{"1000", ",", "1000", ",", " ", ".5"}], "]"}]], "Input", CellChangeTimes->{{3.416577783381459*^9, 3.416577789489357*^9}}], Cell[BoxData["0"], "Output", CellChangeTimes->{3.416577791986763*^9, 3.416655247259696*^9, 3.416656526323749*^9}] }, Open ]], Cell["\<\ We can pass function as arguments. Here is a function for the Euler method \ that uses take a function as the second argument. The function is applied in \ the body of the do loop. Note the use of paren' s and semicolons to sequence \ operations. Note the use of NumberForm and spaces and tabs with in the Print \ command to control formatting.\ \>", "Text", CellChangeTimes->{{3.416655036802576*^9, 3.4166551713966427`*^9}}], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"Euler1", "[", RowBox[{"p0_", ",", " ", "dP_", ",", " ", "dt_", ",", " ", "tend_"}], "]"}], " ", ":=", "\[IndentingNewLine]", RowBox[{"(", RowBox[{ RowBox[{"p", " ", "=", " ", "p0"}], ";", "\[IndentingNewLine]", RowBox[{"Do", "[", RowBox[{ RowBox[{ RowBox[{"p", " ", "=", " ", RowBox[{"p", " ", "+", " ", RowBox[{ RowBox[{"dP", "[", "p", "]"}], "*", "dt"}]}]}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{ "\"\