| Mathematica |
|
|
|
Recurrence relations and a series expansion for ArcSinQuestion:Can Mathematica generate a series expansion for the arcsin? Can it find a recurrence relation for the terms in the series? Answer:The Series function generates a power series expansion of a function. The general usage of Series is: Series[ f[x], {x, x0, n}]
This generates a series expansion of the function f about x = x0 of order n in (x - x0) [The Mathematica Book 5th Edition, pp 100-101]. The expansion of ArcSin[x] about x0 = 0 to order 13 is therefore: Series[ArcSin[x], {x, 0, 13}]
Examining these terms leads to the conclusion that each term is of the form: 2 (2n)! (2n+1) Test this hypothesis by using the Mathematica function SymbolicSum. The SymbolicSum function SymbolicSum[expression, {i, imin, imax}]
attempts to find a value for: imax SymbolicSum is not a built in part of Mathematica, but is part of the standard Algebra package, read it in: Needs["Algebra`SymbolicSum`"] Now use SymbolicSum to compute: 2 (2n)! SymbolicSum[2 (2n)!/((n!)^2 (2n+1)) (x/2)^(2n+1), {n, 0, Infinity}]
This shows that the series is a representation for the arcsin of x. A recursion relation expresses a relationship between successive members of a series. The general expression for the nth term in this series is known from the above. This can be used to express the nth term as a function of the (n -1)th term. Obtain an expression for the nth term: termn = 2 (2n)!/((n!)^2 (2n+1)) (x/2)^(2n+1) And one for the (n - 1)th term termnm1 = termn /. n->n - 1 Then divide the nth term by the (n - 1)th term to find the relationship between the two. ratio = termn / termnm1 Try to simplify it: Simplify[%] Mathematica does not seem to do very well with simplifying expressions involving factorials. Provide it with some help by explicitly supplying some substitutions. Simplify[% /. { n! -> n (n-1)!, (2n)! -> (2n)(2n-1) (2(n-1))! }]
Hence: 2 2 With this relationship, all the terms in the series can be generated once one is given. To make use of this, start by defining the first term: term[0, x_] := x Then define a function that, given the (n - 1)th term, returns the nth term. term[n_, x_] := term[n, x] = The use of term[n_, x_] := term[n, x] = ... causes the value of the function evaluated at a given n and x to be stored. Otherwise they would have to be recomputed every time they were used. This method makes it possible to compute the series expansion to arbitrary order, and produces a significant savings in time. Sum[term[n, x], {n, 0, 6}]
Compare the timings for the two different methods. First compute the series using the explicit term by term definition: Timing[Sum[termn, {n, 0, 600}];]
And then use the recurrence relation: Timing[Sum[term[n, x], {n, 0, 600}];]
Mathematica Memory ManagementQuestion:I keep running out of memory, what can I do? Answer:The rapidly increasing computational capabilities of personal computers are making them more attractive for solving larger and more complex problems. However, many components of these systems are not keeping pace with the advances in CPU speed. Specifically, many personal computers are lacking in memory or memory management capabilities. The techniques discussed below allow more and larger problems to be addressed with limited memory. Removing Unused VariablesThe ClearAll command removes all information associated with a variable. To clear an unused variable x, use: In[10]:= Wild card patterns can also be used with ClearAll. Most user defined variables and functions are in the Global` context. All of the variables and functions in the Global` context can be removed from memory with: In[6]:= Values for derivatives, i.e. f'[x] = -3Cos[3x], are cleared with: In[7]:= Clearing the command historyAll commands and their results are stored in the arrays In[n] and Out[n] respectively. This history can be cleared with the commands In[50]:= The % notation can not be used to refer to results cleared by this procedure. An exampleThe session below uses the MemoryInUse function to display the amount of memory Mathematica is using. The initial amount of memory in use could be slightly reduced by removing some of the packages from the Mathematica StartUp directory. A large array is allocated as an example of how memory might be consumed by Mathematica. Finally, the memory is cleared. On startup check how much memory, in bytes, we are using. This example was run on a 64 bit Unix system. In[1]:= Allocate a large array. This is indicative of the memory intensive operations that can be done in Mathematica. In[2]:= Attempting a second, similar, operation would exceed the available memory. First clear the variable BigArray. In[4]:= The memory has not been freed. This is because BigArray is still referenced by the Out[] array. Clearing Out[] removes the final reference to BigArray and allows Mathematica to reclaim the memory. In[6]:= The cleanslate packageThe Cleanslate package provides an automatic procedure for clearing large amounts of memory from Mathematica. There are 3 functions exported from the package: CleanSlate, CleanSlateExcept, and ClearInOut. ClearInOut[]
the share commandThe Share command causes Mathematica to conserve memory by sharing subexpressions. This command should be reissued periodically. the front endMathematica is divided into two parts. The front end handles interactions with the user such as displaying plots. The kernel carries out the mathematical computations. When you start Mathematica you actually start only the front end. The front end will in turn start the kernel only when a calculation is required. Except for a slight delay in the first calculation this should be transparent. The other occasion when the separation betwee n the front end and the kernel is likely to become evident is if your computer has enough memory to start the front end, but not enough to start the kernel. In this case you will receive a message that the kernel was unable to start. write the notebook to diskGraphics consume most of the front end's memory. Normally, all of the graphics produced during a session must be stored in memory. However, if the notebook containing the graphics has been saved since the graphics was produced, the front end will flush the graphics from memory, and recover it from disk when needed. This makes it highly desirable to save the notebook to disk after every graphics intensive calculation. Indeed, the Auto Save after Each Result option, located in the Action pulldown menu, causes the front end to automatically save the notebook after every calculation. clear the clipboardWhenever possible use Paste and Discard rather than Paste. supress the display of intermediate resultsAppending a semicolon to Mathematica input suppresses the display of the corresponding Out[n] expression. This saves more time and memory than one might expect because formatting calculations are not done for expressions that are not displayed. memory use and timing for a normal mathematica sessionThe session below shows the memory and time used when no effort is made to conserve memory. In[1]:= memory use and timing for a mathematica session with output supressedThis session shows how even a small effort can produce a significant savings in both time and memory.In[1]:= Supressing graphics outputGraphics are one of the most memory intensive aspects of Mathematica. To suppress the display of graphical output, use the DisplayFunction->Identity option to the command generating the graphics. MyGraph = Plot[Sin[x], {x, 0, 2Pi}, DisplayFunction->Identity]
The plot can then be manipulated through the variable MyGraph. The following command displays the plot.
Show[MyGraph, DisplayFunction -> $DisplayFunction] The a postscript version of the plot can also be written directly to disk. Display["MyGraph.ps", MyGraph]Such a PostScript file can be read back into Mathematica with the !! command: !!MyGraph.ps This loads the PostScript into a cell as text. To display the PostScript as a Mathematica graphic, first select the cell containing the PostScript, then choose the Graphics style from the Cell Style submenu of the Style pulldown menu.A postscript file written in this way can also be displayed directly by the front end, without the need to startup a kernel. Use the Open... option from the File pulldown menu to open the PostScript file. Then use the procedure described above to display the PostScript as a Mathematica graphic. mac specific hintsThe Mac front end keeps both a PostScript and PICT version of the graphics that it displays. Usually the PostScript version can be discarded. This is done by first select the graphics, then select Convert to PICT... from the graph menu. Clear the clipboard. From the Edit pulldown menu select Convert Clipboard... then click on Empty. This clears any text or graphics from the clipboard. This can be combined with a paste operation by selecting Paste and Discard. Some Problems with Series ExpansionsQuestion:Are there any known bugs in Mathematica's Series function? Answer:There are a number of minor problems with the Series function. Below is a discussion of all of them that I am aware of. This information was extracted from a series of discussions on the newsgroup comp.soft-sys.math.mathematica, primarily between David Withoff of Wolfram and Peter Arnold at the University of Washington. In[2]:= Series[ Log[x(1-x)],{x,0,1}]
The bug is that the Log of a series expansion around zero with exactly one linear coefficient returns an order term that is bigger than it should be. The bug is specific to this example. I don't know of any other manifestations. This bug has been fixed in the development version of Mathematica; contacting Wolfram technical support ( This e-mail address is being protected from spambots. You need JavaScript enabled to view it ) might produce a workaround for later versions. In[3]:= E^( (a - b x) (c - Log[d]) ) Series mistakenly thinks that anything with Log in a non-constant exponent has an essential singularity. The workaround is to replace these logs with something else. This has also been fixed in the upcomming version of Mathematica. The cases where it doesn't return the expansion to the order requested are also a potential source of trouble because one frequently wants to take the Normal of a series before processing it. If one isn't aware that Mathematica may not return the series to the order requested, mistakes can easily be made. WRI has responded, by the way, that the fact that Series doesn't always return the order you request is *not* an error in Series; it is instead an error in the documentation. In[2]:= Series[Gamma[x],{x,0,0}]
and In[3]:= Series[Gamma[x],{x,0,2}]
The current algorithm used by Series is unable (at least as a practical matter) to predict the order that will appear in the result. A nearly identical problem comes up in numerical computations, where cancellations and numerical instability can give low-precision output even for high precision inputs. There are several ways to solve this problem (both in series and in numerical computations), but it has not been solved in Version 2.2 of Mathematica. Since the documentation says that "Series[f, {x, x0, n}] generates a power series expansion ... to order (x - x0)^n", this behavior does not agree with the documentation. Whether this is an error in the code or in the documentation is matter of semantics. The next example is something that doesn't work and is documented as such. It should, though, fail more gracefully. In[1]:= Series[Exp[x]/x^4, {x,0,-4}]
Wolfram has promised that this will be fixed in upcoming versions of Mathematica. The problems people seem to encounter with Series are special cases of the examples listed above.
Naming Conventions in MathematicaQuestion:What do things like Global` or Graphics` mean? Answer:The full name of an object, usually a variable or a function, in Mathematica consists of two parts, a context and a short name. The full name of an object is then context`short name, where the ` is a backquote. The context is frequently a Mathematica package. This prevents local variables in one package from interfearing with local variables in another package. When an object is referred to in Mathematica, but the context is not specified, Mathematica searches the current context then the contexts listed in $ContextPath for object. The current context is available from the variable $Context. This shows that the default context is Global` In[1]:= Most user defined variables and functions are in the Global` context. The $ContextPath by default contains: In[2]:= When additional packages are loaded, their contexts are automatically added to $ContextPath. In[3]:= Functions, such as TrigReduce, that are part of the Algebra`Trigonometry` package, are automatically found when called because their context is included in $ContextPath. In most cases this process is transparent to the user. However, explicit knowledge of this mechanism is useful when writing Mathematica packages, loading packages with name conflicts, or clearing unused packages and variables from Mathematica's memory. For additional details, see Mathematica: A system for Doing Mathematics by Computer, PP332-338. |



