(*********************************************************************** 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[ 5562, 132]*) (*NotebookOutlinePosition[ 6458, 165]*) (* CellTagsIndexPosition[ 6356, 158]*) (*WindowFrame->Normal*) Notebook[{ Cell[TextData[{ "This module handles automatic step control. As arguments, it accepts a \ method, a proposed step size, the current solution, the method order, and the \ local tolerance criteria. The basic idea is to compute the solution using \ both one step of size h and two steps of size h/2 and to use Richardson \ extrapolation to make an estimate of the error. Suppose the method has error \ of order p. Then, if ", Cell[BoxData[ \(TraditionalForm\`A\_h\)]], "refers to the approximation with step size h, and S the actual solution, \ we have asymptotically\n", Cell[BoxData[ \(TraditionalForm\`\(\(A\_h = S\ + \ c\ h\^\(p + 1\)\)\(\ \)\)\)]], " and ", Cell[BoxData[ \(TraditionalForm\`A\_\(h/2\) = S\ + 2\ c\ \((h/2)\)\^\(p + 1\)\)]], "\n(p + 1 because this is the local error: combining steps gives one order \ less, namely p. Solving these equations for c, the error estimate is\n2 ", Cell[BoxData[ \(TraditionalForm\`\(c(h/2)\)\^p \[TildeFullEqual] \ \((A\_h - A\_\(h/2\))\)/\((2\^p - 1)\)\)]], ".\nIn a similar way, an \"optimal\" next step size h can also be computed. \ In practice we multiply this by a safety factor because it is relatively \ expensive to reject a step." }], "Text"], Cell["\<\ The scaled norm here is fairly typical for DE solvers. The balance of \ absolute (AccuracyGoal) and relative (PrecisionGoal) tolerance is the same as \ for the built in NDSolve command.\ \>", "Text"], Cell[BoxData[ \(\(DoubleStep[method_, htry_, u_, ord_, {atol_, rtol_}] := Block[{h = 2\ htry, half, one, two, errest = Infinity, count = 0, fac = 1}, \[IndentingNewLine]half = \(method[h/2]\)[ u]; \[IndentingNewLine]While[ errest > 1, \[IndentingNewLine]\(count++\); \[IndentingNewLine]h /= 2; \[IndentingNewLine]one = half; \[IndentingNewLine]half = \(method[h/2]\)[ u]; \[IndentingNewLine]two = \(method[h/2]\)[ half]; \[IndentingNewLine]w = atol + rtol* Map[Max, Transpose[{Abs[u], Abs[two]}]]; \[IndentingNewLine]diff = Max[Abs[\((two - one)\)/ w]]; \[IndentingNewLine]errest = \((2^ord - 1)\)\ diff;\[IndentingNewLine]]; \[IndentingNewLine] \ (*\[IndentingNewLine]Success : increase\ h\ if\ we\ didn' t\ have\ to\ \ reduce\[IndentingNewLine]*) \[IndentingNewLine]If[ count < 2, \[IndentingNewLine]If[ Developer`ZeroQ[errest], \[IndentingNewLine]fac = 2, \[IndentingNewLine]fac = Min[2, \((95/ 100)\)*\((1/ errest)\)^\((1/\((ord + 1)\))\)]]\[IndentingNewLine]]; \ \[IndentingNewLine]{h*fac, h, two}];\)\)], "Input", CellTags->"DoubleStep"], Cell[TextData[{ "Handler for Automatic values for the PrecisionGoal and AccuracyGoal \ options. This is essentially identical to how built-in ", StyleBox["Mathematica", FontSlant->"Italic"], " functions handle these values. " }], "Text"], Cell[BoxData[ \(\(AutomaticAGPG[wprec_, apgin_, pora_] := Module[{apg = apgin}, \[IndentingNewLine]If[ apg === Automatic, \[IndentingNewLine]apg = If[wprec \[LessEqual] $MachinePrecision, 6, wprec - 10], If[Not[NumberQ[apg] && Positive[apg]], \[IndentingNewLine]If[ pora === Precision, \[IndentingNewLine]Message[ SplitStepSolve::precg, apg], \[IndentingNewLine]Message[ SplitStepSolve::accg, apg]]; \[IndentingNewLine]Return[$Failed];]]; \ \[IndentingNewLine]SetPrecision[10. ^\((\(-apg\))\), wprec]];\)\)], "Input", CellTags->"AutoAGPG"] }, FrontEndVersion->"4.0 for Microsoft Windows", ScreenRectangle->{{0, 1024}, {0, 695}}, WindowSize->{496, 599}, WindowMargins->{{73, Automatic}, {Automatic, 0}} ] (*********************************************************************** 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->{ "DoubleStep"->{ Cell[3196, 81, 1442, 28, 490, "Input", CellTags->"DoubleStep"]}, "AutoAGPG"->{ Cell[4893, 119, 665, 11, 210, "Input", CellTags->"AutoAGPG"]} } *) (*CellTagsIndex CellTagsIndex->{ {"DoubleStep", 6167, 148}, {"AutoAGPG", 6258, 151} } *) (*NotebookFileOutline Notebook[{ Cell[1717, 49, 1263, 24, 242, "Text"], Cell[2983, 75, 210, 4, 71, "Text"], Cell[3196, 81, 1442, 28, 490, "Input", CellTags->"DoubleStep"], Cell[4641, 111, 249, 6, 52, "Text"], Cell[4893, 119, 665, 11, 210, "Input", CellTags->"AutoAGPG"] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)