Re: How to Enable Automatic Recalculation
- To: mathgroup at smc.vnet.net
- Subject: [mg109867] Re: How to Enable Automatic Recalculation
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Thu, 20 May 2010 06:39:24 -0400 (EDT)
On 5/19/10 at 7:01 AM, xxx at lighting-research.com (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.
It is a little unclear as to exactly what you want when you say
you want "the value of to be updated each time I change the
value of a". One way to interpret this would be that you want
computations made before a was changed to update when a is
changed. If so, you will need to have a, b and the other
expressions that depend on a in a dynamic module.
Another way to interpret what you want is as saying you want
computations made after a is changed to reflect the new value.
This is much simpler to implement. All you need do is define
those operations using SetDelayed rather than Set. For example:
In[1]:= a = 5;
b := a + 3
{a, b}
Out[3]= {5,8}
In[4]:= {a = 4, b}
Out[4]= {4,7}
SetDelayed causes the evaluation of b to be delayed until it is
used. So, any use of b when defined with set delayed will
reflect the value of a at the time b is used. Contrast that with
c defined below with Set
In[5]:= c = a + 5;
{a, c}
Out[6]= {4,9}
In[7]:= {a = 5, c}
Out[7]= {5,9}
Here, c is unchanged even though a is changed.