{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# class20 - PHYS 5900 Class 20 Zi-Wei Lin Example on Patterns...

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

PHYS 5900 Class 20 (10/14/2009) Zi-Wei Lin Example on Patterns: plot the function V[x]: V[x]= 0 for |x|>a = - V 0 for |x|<=a. We can choose a as the unit of x and V 0 as the unit of V, the function then becomes V[x]= 0 for |x|>1 = -1 for |x|<=1. In[1]:= v @ x_ ; Abs @ x D > 1 D : = 0; v @ x_ ; Abs @ x D <= 1 D : = - 1 In[3]:= Plot @ v @ x D , 8 x, - 2, 2 <D Out[3]= - 2 - 1 1 2 - 1.0 - 0.8 - 0.6 - 0.4 - 0.2 In[4]:= Plot @ v @ x D , 8 x, - 2, 2 < , AxesLabel -> 8 " x H a L ", " V H V 0 L " < , PlotStyle -> Thickness @ 0.01 DD Out[4]= - 2 - 1 1 2 x H a L - 1.0 - 0.8 - 0.6 - 0.4 - 0.2 V H V 0 L

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

View Full Document
In[5]:= Clear @ v D We cannot define the following function using an undefined a : In[6]:= v @ x_ ; Abs @ x D > a D : = 0; v @ x_ ; Abs @ x D <= a D : = - 1 In[8]:= v @ 0.5 D Out[8]= v @ 0.5 D In[9]:= Clear @ v D We can also impose conditions on transformation rules using lhs :> rhs /; condition In[10]:= 8 f @ 2 D , f @ a D , g @ 6 D , f @ 5 D< . f @ x_ D : > x ; x > 3 Out[10]= 8 f @ 2 D , f @ a D , g @ 6 D , 5 < We need to use :> (delayed transformation rule). The following uses -> (immediate transformation rule) and does not work: In[11]:= 8 f @ 2 D , f @ a D , g @ 6 D , f @ 5 D< . f @ x_ D -> x ; x > 3 Out[11]= 8 2 ; 2 > 3, a ; a > 3, g @ 6 D , 5 ; 5 > 3 < We can also impose conditions on function definitions using func := definition /; condition In[12]:= func @ x_, y_ D : = Sqrt @ x - y D ; x > y In[13]:= func @ 5, 4 D Out[13]= 1 In[14]:= func @ 5, 6 D Out[14]= func @ 5, 6 D In[15]:= Clear @ func D We need to use := (delayed assignment) for the function definition. The following uses = (immediate assignment) and does not work: In[16]:= func @ x_, y_ D = Sqrt @ x - y D ; x > y Out[16]= x - y ; x > y In[17]:= func @ 5, 4 D Out[17]= 1 ; 5 > 4 2 class20.nb
In[18]:= func @ 5, 6 D Out[18]= ; 5 > 6 In[19]:= Clear @ func D Structural Equivalence between Patterns Mathematica looks for structural equivalence when matching patterns, not mathematical equivalence.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 8

class20 - PHYS 5900 Class 20 Zi-Wei Lin Example on Patterns...

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

View Full Document
Ask a homework question - tutors are online