Example: Let us utilize Mathematica to generate the first 15 values of the difference equation, a[n+1] = a[n] + a[n-1], with the initial values a[0] = a[1] = 1.
To find the first 15 values of a[n], we shall first input the initial values
In[1]:= a[0] = 1 Out[1]= 1 In[2]:= a[1] = 1 Out[2]= 1
It is tempting at this point to input the difference equation in some manner, but to do so would create a great difficulty due to the fact that a difference equation is defined recursively. Mathematica would automatically begin to generate values of a[n] until the internal limits of the system would force it to stop. Thus we must apply some restrictions on the values of n before we can input the difference equation.
One of the most convenient ways in which we can derive the first 15 values of this recursively defined function a[n], is to restrict the possible values that n can attain. Usually when one thinks of generating the values of a function at n = 1, 2, ..., 15, one thinks of some sort of looping devise. In our case we will incorporate a Table command:
In[3]:= Table[a[n+1]=a[n]+a[n-1],{n,1,14}]This generates the values of a[n+1] as n ranges from 1 to 14 (the values a[2], a[3], ..., a[15]), and places everything in a list as follows:
Out[3]= {2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987}These values, along with our initial values, are then the values of a[n] as n ranges from 0 to 15.
Example: Consider the difference equation , a[n+1] = 5*a[n] - 4*a[n-1], with initial values a[0] = 2 and a[1] = 3. We shall use Mathematica to obtain the values of a[n] for the values of n = 2, 3, ..., 15.
To define a[n] as a recurrent function in Mathematica while restricting the value of n, we shall utilize a Table command. We do this as follows:In[1]:= a[0]=2 Out[1]= 2 In[2]:= a[1]=3 Out[2]= 3 In[3]:= Table[a[n+1]=5*a[n]-4*a[n-1],{n,1,14}] Out[3]= {7, 23, 87, 343, 1367, 5463, 21847, 87383, 349527, 1398103, 5592407, > 22369623, 89478487, 357913943}Thus we see that the size of a[n] grows fairly rapidly with n.
Example: The difference equation( y[n] ) y[n+1] = y[n] ( 4 - ----- ) ( 300 )is a form of the discrete logistic equation, describing the size y of a population at time t =n+ 1 units. Suppose that y[0] = 450. Let us use Mathematica to obtain the values of y[n] for n = 1, 2, ..., 15.
Once again, to do this we shall use a Table for the purpose of restricting the values of n. We obtain the solution of this problem as follows:
In[4]:= y[0]=450.0 Out[4]= 450. In[5]:= Table[y[n+1]=y[n]*(4-y[n]/300),{n,0,14}] Out[5]= {1125., 281.25, 861.328, 972.359, 737.83, 1136.68, 239.928, 767.828, > 1106.11, 346.168, 985.231, 705.324, 1163.02, 143.351, 504.906}Note that if we had input the initial value as an integer 450 instead as a real number 450.0, then the output would have been in terms of fractions of integers whose sizes would grow extremely large rather quickly.