Student Support Forum: 'Iteration' topicStudent Support Forum > General > "Iteration"

 < Previous Comment | Next Comment > Help | Reply To Comment | Reply To Topic
 Author Comment/Response Forum Moderator email me 09/20/04 4:11pm Look again at the sections of the Mathematica Book that were recommended earlier in this thread. You will see that code provided by Henry Lamb is based on the ideas in these sections. Included in section 2.5.9 is an evaluation that looks at the information on the defined function and shows when functions are defined in as shown in that section and in Henry's code, that Mathematca "remembers" the values it has already calculated. This means that once f[n] is calculated, it will not be recalculated unless you do something to force that (more on that later.) For example using the preliminary definitions and assigments (with w = 200./10^9), you can evaluate p[5]. In[]:= p[5] Out[]= -8 1.61051 10 In order to get this result, Mathematica evaluates p for 0,1,2,3,4 and keeps the values (look at the code defining p and the suggested sections of the Mathematica book to see why that is the case.) You can see those values by evaluating ?p In[]:= ?p p 1 p[0] = --------- 100000000 -8 p[1] = 1.1 10 -8 p[2] = 1.21 10 -8 p[3] = 1.331 10 -8 p[4] = 1.4641 10 -8 p[5] = 1.61051 10 1 p[j_] := p[j] = - (q[j - 1] - 1) p[j - 1] T + p[j - 1] t The same is true for q, ?q results in: q[0] = 2 q[1] = 2. q[2] = 2. q[3] = 2. q[4] = 2. 1 q[j_] := q[j] = - (r - q[j - 1] - (r - 1) q[j - 1] p[j - 1]) T + q[j - 1] w If you change the value of some symbol or expression used in evaluating used in either evaluation, say w and evaluate p[6], Mathematica uses the existing values for p[5] ... p[0]. That is how the code is designed, i.e. it is designed so that already computed values do not have to be recomputed. Now evauluate p[1000] In[10]:= p[1000] Out[10]= 1. Change the value of w: In[11]:= 1000 w = ---- 9 10 Out[11]= 1 ------- 1000000 p[1000] does not change as it has already been calculated. In[12]:= p[1000] Out[12]= 1. p[220] is more instructive as this one of the values where the effect of changing w should be seen: In[13]:= p[220] Out[13]= 1.17189 In[14]:= 1000 w = ---- 9 10 Out[14]= 1 ------- 1000000 But since it has already been calculated, it's value is not recalculated, rather it is just "reported." In[15]:= p[220] Out[15]= 1.17189 Evaluations of p[1001] and beyond will use the new w but still will be based on the p[1000] calculated with the old value. You will need to clear the values already calculated to force p[1] through p[1000] to be recalculated. When you quit Mathematica all values including p and q are cleared. A less drastic method is to clear just p and q: In[16]:= Clear[p, q] Then evaluate all of definitions needed to calculated p and q: In[17]:= 1 p[j_] := p[j] = - (q[j - 1] - 1) p[j - 1] T + p[j - 1] t In[18]:= 1 q[j_] := q[j] = - (r - q[j - 1] - (r - 1) q[j - 1] p[j - 1]) T + q[j - 1] w In[19]:= -8 p[0] = 10 ; In[20]:= q[0] = 2; Now p[1000} recalculates with the "new" w (which wasn't cleared since we are still in the same kernel session). In[21]:= p[1000] Out[21]= 0.999722 and p[220] has it's new value. In[22]:= p[220] Out[22]= 3.48895 There may be other ways to reset p and q not covered here but the above should always work Tom Zeller URL: ,

 Subject (listing for 'Iteration') Author Date Posted Iteration Paul 09/03/04 1:07pm Re: Iteration Forum Modera... 09/08/04 12:58pm Re: Iteration Paul Stysley 09/09/04 1:59pm Re: Iteration Henry Lamb 09/13/04 02:40am Re: Iteration Paul Stysley 09/13/04 3:04pm Re: Iteration Henry Lamb 09/16/04 02:11am Re: Iteration Paul 09/20/04 2:11pm Re: Iteration paul 09/20/04 2:20pm Re: Iteration Forum Modera... 09/20/04 4:11pm Re: Iteration Paul 09/21/04 09:59am
 < Previous Comment | Next Comment > Help | Reply To Comment | Reply To Topic