Re: How to Enable Automatic Recalculation
- To: mathgroup at smc.vnet.net
- Subject: [mg109899] Re: How to Enable Automatic Recalculation
- From: Victor Roberts <xxx at lighting-research.com>
- Date: Sat, 22 May 2010 00:40:28 -0400 (EDT)
- References: <ht5oau$179$1@smc.vnet.net>
Murray, You are correct. The example I gave is somewhat simpler than the problem I am working on. The problem is a definite integral of a piecewise function, where that piecewise function depends upon the value of one variable. Based on prior experience with a different math program, I had expected that after the integral was calculated, I could go back and change the value of the variable and have the value of the integral change automatically. As I have said elsewhere, this was all based on a misunderstanding of the design of the Mathematica interface, and I now see that I can just change the value of the variable, and then reevaluate the integral. Thanks for your help. -- Vic Roberts Replace xxx with vdr in e-mail address. On Fri, 21 May 2010 10:46:54 +0000 (UTC), Murray Eisenberg <murray at math.umass.edu> wrote: >Method 1's final result as I showed it is indeed dead wrong, due to an >unfortunate copy-and-paste; thanks for catching that. > >But I see nothing "wrong" in method 2. I just checked the results >against evaluation of the input lines shown (one per input cell) in a >fresh Mathematica 7.0.1 notebook. But I don't deny there are potential >pitfalls. > >I suspected that the O.P. was interested in something more complicated >than the simple arithmetic example he used in his query. >So I wanted to suggest the possibility that the sort of thing he was >looking for could be treated by Dynamic, and not just SetDelayed. > >On 5/20/2010 6:39 AM, dh wrote: >> Hi, >> there seems to be two pitfalls in method 2 and 3. >> Dynamic is thought to update screen outputs, not to recalculate >> variables. If nothing is printed, Dynamic is not even evaluated. >> Let's look at method2: >> (* method 2: use Dynamic *) >> Clear[a,b] >> Dynamic[a] >> a >> a = 3; (* preceding output changes to 3 *) >> b = 3 + a >> 6 >> a = Pi; >> b >> 3 + Pi >> The last answer is WRONG. b is definitly set to 6 and is NEVER >> recalculated when a is changed. This is so because we use a un-delayed >> assign. >> >> Method 3 is even more dangerous: >> (* method 3: another way to use Dynamic *) >> Clear[a,b] >> a = 3; >> Dynamic[b = 3 + a] >> 6 >> a = Pi; (* preceding ouptput changes to 3 + Pi *) >> b >> 3 + Pi >> >> The last answer is again wrong in a devious way. The correct value is 6. >> However, if you reevaluate b again, it is suddenly 3+Pi. The reason is, >> that the Dynamic statement is evaluated AFTER b has been printed. >> Therefore, b obtains its new value too late. >> >> What can we learn from this is that Dynamic should be used for display >> jobs and not anything else. The right thing to do here is to use an >> delayed assign as in method 1. >> cheers, Daniel >> >> Am 20.05.2010 02:15, schrieb Murray Eisenberg: >>> There IS a "simple" answer. Actually, two answers. it used to be that >>> the only way to do this was to use SetDelayed ( := ) for the symbol you >>> wanted to be recalculated whenever you changed another variable whose >>> values was specified by Set ( = ). Since Mathematica 6, you also have >>> the more sophisticated method of using Dynamic quantities. >>> >>> (* method 1: use SetDelayed instead of Set for b *) >>> a = 5; >>> b := 3 + a >>> b >>> 8 >>> a = Pi; >>> b >>> 3 + Pi >>> >>> >>> (* method 2: use Dynamic *) >>> Clear[a,b] >>> Dynamic[a] >>> a >>> a = 3; (* preceding output changes to 3 *) >>> b = 3 + a >>> 6 >>> a = Pi; >>> b >>> 3 + Pi >>> >>> (* method 3: another way to use Dynamic *) >>> Clear[a,b] >>> a = 3; >>> Dynamic[b = 3 + a] >>> 6 >>> a = Pi; (* preceding ouptput changes to 3 + Pi *) >>> b >>> 3 + Pi >>> >>> >>> >>> On 5/19/2010 7:01 AM, Victor Roberts wrote: >>>> I'm new to Mathematica, so perhaps there is a simple answer >>>> to this question. >>>> >>>> How to I set Mathematica so that when I change the value >>>> assigned to a variable it will automatically change all >>>> later calculations that use that variable. >>>> >>>> For example, if I set >>>> >>>> a = 5 >>>> >>>> and >>>> >>>> b = 3 + a >>>> >>>> I would like the value of b updated each time I change the >>>> value of a. >>>> >>>> Right how, I need to recalculate each and every expression >>>> that uses the variable a if I change its value. There must >>>> be a better way. >>>> >>>> >>>> -- >>>> Vic Roberts >>>> Replace xxx with vdr in e-mail address. >>>> >>> >> >>