class25 - PHYS 5900 Class 25 (10/26/2009 11am-12:15pm)...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
PHYS 5900 Class 25 (10/26/2009 11am-12:15pm) Zi-Wei Lin Loops ± Changing Values of Variables in loops we often need to change/update the value of variables. Commands include x++ ++x x-- --x x+=dx x-=dx x*=c x/=c {x,y}={y,x} PrependTo[s,element] AppendTo[s,element] In[1]:= ? PrependTo PrependTo @ s , elem D prepends elem to the value of s , and resets s to the result. ± In[2]:= ? Prepend Prepend @ expr , elem D gives expr with elem prepended. ± In[3]:= x = 3; x *= 4 * b Out[3]= 12 b In[4]:= s = 8 1, 2, 3 < ; AppendTo @ s, r D ; s Out[4]= 8 1, 2, 3, r < Compare AppendTo with Append : In[5]:= s = 8 1, 2, 3 < ; Append @ s, r D ; s Out[5]= 8 1, 2, 3 <
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
So AppendTo[s, r] is equivalent to s = Append[s, r] Increment (++) and decrement (--) operators: In[6]:= x = 3; y1 = ++ x; 8 x, y1 < Out[6]= 8 4, 4 < In the above, ++x uses the prefix form of the increment operator (++): x is incremented first, then x is returned (as y1). In[7]:= x = 3; y2 = x ++ ; 8 x, y2 < Out[7]= 8 4, 3 < In the above, x++ uses the postfix form of the increment operator (++): x is returned first (as y2), then x is incremented. In[8]:= ClearAll @ x, y1, y2, s D Do , While , For can evaluate expressions/procedures repeatedly. Do: In[9]:= ? Do Do @ expr , 8 i max <D evaluates expr i max times. Do @ expr , 8 i , i max <D evaluates expr with the variable i successively taking on the values 1 through i max H in steps of 1 L . Do @ expr , 8 i , i min , i max <D starts with i = i min . Do @ expr , 8 i , i min , i max , di <D uses steps di . Do @ expr , 8 i , 8 i 1 , i 2 , <<D uses the successive values i 1 , i 2 , . Do @ expr , 8 i , i min , i max < , 8 j , j min , j max < , D evaluates expr looping over different values of j , etc. for each i . ± In[10]:= ClearAll @ f D In[11]:= f @ x_ D : = x^2; In[12]:= Do @ f @ i D , 8 i, 3 <D Do, While, For return Null . We can use Print to display the results: In[13]:= Do @ Print @ f @ i DD , 8 i, 3 <D 2 class25.nb
Background image of page 2
1 4 9 In[14]:= Do @ Print @ "k = ", k, " ", Sum @ 1 ± H n^k L , 8 n, 1, Infinity <DD , 8 k, 2, 8, 2 <D k = 2 Π 2 6 k = 4 Π 4 90 k = 6 Π 6 945 k = 8 Π 8 9450 ± Find the value of x after 120 iterations of x n + 1 = f H x n L , f(x)= Μ x(1-x) with x 0 =0.5, Μ =3.64 : In[15]:= x = 0.5; Do @ x = mu * x * H 1 - x L ± . mu 3.64, 8 120 <D ; x Out[15]= 0.371566 Nest can do the same: In[16]:= Nest @HH mu * ± * H 1 - ± LL ± . mu 3.64 L D Out[16]= 0.371566 In[17]:= ClearAll @ f, x D While: In[18]:= ? While While @ test , body D evaluates test , then body , repetitively, until
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 10

class25 - PHYS 5900 Class 25 (10/26/2009 11am-12:15pm)...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online