Re: Recognize multiple outputs
- To: mathgroup at smc.vnet.net
- Subject: [mg118087] Re: Recognize multiple outputs
- From: David Bailey <dave at removedbailey.co.uk>
- Date: Tue, 12 Apr 2011 05:55:56 -0400 (EDT)
- References: <inpesa$8t8$1@smc.vnet.net> <inqkv4$e8r$1@smc.vnet.net> <inunc1$2d4$1@smc.vnet.net>
On 11/04/2011 12:06, mailcwc at gmail.com wrote: > On 4=E6=9C=8810=E6=97=A5, =E4=B8=8A=E5=8D=886=E6=99=8201=E5=88=86, David Ba= > iley<d... at removedbailey.co.uk> wrote: >> >> However, I would strongly recommend that you think again about the way >> you work. Input cells containing multiple calculations, are probably >> best split into separate cells. You can do this very easily by putting >> the cursor at the right place, and selecting the menu item Call/Divide Cell. >> >> Very large expressions are often the result of a number of previous >> steps, and it may be better to re-execute those steps to re-derive the >> large expressions unless this is too time consuming. It is horribly easy >> for a mistake to lurk in a large expression - particularly if it has >> been entered by hand! >> >> Another general tip, is to keep a calculation symbolic as long as >> possible, so that it is easy to read and understand what is going on. >> Numbers can usually be injected at the last possible step. >> >> David Baileyhttp://www.dbaileyconsultancy.co.uk > > Hi, David, thanks for your tip. > I have thought about breaking the cell into smaller ones. > In my workflow, the cell will be recalculated with some variables > modified. > If the whole calculation is not in one single cell, > I have to press Shift+Enter for each of the smaller cells. > And I bet I will miss some cell sometimes and get the wrong answer in > the end. > > > > > A good alternative is to create a function. This can contain many steps, and may, or may not return a result. Say you started with: x1=expr1 x2=expr2 x3=expr3 You could write something like: doMyCalculation[]:=Module[{}, x1=expr1; Print["x1=",expr1]; x2=expr2; Print["x2=",expr2]; x3=expr3; Print["x3=",expr3]; ]; ***Note where the semicolons and commas go in the above *** Evaluating that cell just defines the function - nothing is calculated, but to get a calculation you execute: doMyCalculation[] Note that the Module doesn't really do much here, but once you start using functions, you will probably want to use local variables, which you supply as a list as the first argument to Module. I also usually organise things so that I can execute the entire notebook from the menu - this avoids the need to execute individual cells. Mathematica is an extremely flexible system in which to compute, unfortunately it is horribly easy to find very clumsy ways to use it! David Bailey http://www.dbaileyconsultancy.co.uk David Bailey http://www.dbaileyconsultancy.co.uk