class23 - PHYS 5900 Class 23 (10/21/2009 11am-12:15pm)...

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

View Full Document Right Arrow Icon
PHYS 5900 Class 23 (10/21/2009 11am-12:15pm) Zi-Wei Lin Recursive Functions and Dynamic Programming We can define a recursive function (i.e., defined in terms of the function itself). Example : the Hermite polynomial can be defined as H n H z L = 2 z H n - 1 H z L - 2 H n - 1 L H n - 2 H z L with H 0 H z L = 1 and H 1 H z L = 2 z . Typically, we can define this recursive relation as In[1]:= ClearAll @ hermite D In[2]:= hermite @ n_Integer ± ; n > 1, z_ D : = Expand ± H 2 * z * hermite @ n - 1, z D - 2 * H n - 1 L * hermite @ n - 2, z DL In[3]:= hermite @ 0, z_ D : = 1; hermite @ 1, z_ D : = 2 * z; In[5]:= Timing @ hermite @ 28, z DD Out[5]= 9 14.609, 3 497 296 636 753 920 000 - 97 924 305 829 109 760 000 z 2 + 424 338 658 592 808 960 000 z 4 - 678 941 853 748 494 336 000 z 6 + 533 454 313 659 531 264 000 z 8 - 237 090 806 070 902 784 000 z 10 + 64 661 128 928 428 032 000 z 12 - 11 368 989 701 701 632 000 z 14 + 1 326 382 131 865 190 400 z 16 - 104 029 971 126 681 600 z 18 + 5 475 261 638 246 400 z 20 - 189 619 450 675 200 z 22 + 4 122 161 971 200 z 24 - 50 734 301 184 z 26 + 268 435 456 z 28 = In[6]:= ? hermite Global`hermite hermite @ n_Integer ± ; n > 1, z_ D : = Expand @ 2 z hermite @ n - 1, z D - 2 H n - 1 L hermite @ n - 2, z DD hermite @ 0, z_ D : = 1 hermite @ 1, z_ D : = 2 z In[7]:= ClearAll @ hermite D
Background image of page 1

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

View Full DocumentRight Arrow Icon
A better definition using Dynamic Programming: To make the computation more efficient, we can ask Mathematica to remember the values of hermite[n,z] as they are computed and use those to compute for higher n-values. Dynamic Programming means creating and storing new rules for a function during its evaluation, the syntax is func[arg1_, arg2_, . ..] : = func[arg1, arg2, . ..] = rhs The Hermite polynomial can then be defined as In[8]:= hermite @ n_Integer ± ; n > 1, z_ D : = hermite @ n, z D = Expand ± H 2 * z * hermite @ n - 1, z D - 2 * H n - 1 L * hermite @ n - 2, z DL In[9]:= hermite @ 0, z_ D : = 1 hermite @ 1, z_ D : = 2 * z In[11]:= Timing @ hermite @ 28, z DD Out[11]= 9 5.96745 * 10 - 16 , 3 497 296 636 753 920 000 - 97 924 305 829 109 760 000 z 2 + 424 338 658 592 808 960 000 z 4 - 678 941 853 748 494 336 000 z 6 + 533 454 313 659 531 264 000 z 8 - 237 090 806 070 902 784 000 z 10 + 64 661 128 928 428 032 000 z 12 - 11 368 989 701 701 632 000 z 14 + 1 326 382 131 865 190 400 z 16 - 104 029 971 126 681 600 z 18 + 5 475 261 638 246 400 z 20 - 189 619 450 675 200 z 22 + 4 122 161 971 200 z 24 - 50 734 301 184 z 26 + 268 435 456 z
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/25/2010 for the course PHYS 5900 taught by Professor Lin during the Fall '09 term at East Carolina University .

Page1 / 9

class23 - PHYS 5900 Class 23 (10/21/2009 11am-12:15pm)...

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

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