| 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 Seriesfunction generates a power series expansion of a function. The general usage of Seriesis: 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->Identityoption 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. |



