Example 09
Iterative Equations and 
Modular Programming

Copyright © 1993 by Bill Titus, Carleton College, 
Department of Physics and Astronomy, Northfield, MN  55057-4025
September 6, 1993

Topics and Skills

Problem

Solution - Using a Sequence of Iterative Equations

Step 1 - Briefly describe how you would implement Euler's algorithm if you were doing linear programming using arrays.

Comment 1

Step 2 - You'll develop the algorithm symbolically to begin so you can make sure it's working correctly.  In particular, look over the following code and then activate the cell.

Clear[t, a, v, x]
	Clear[to, tf, dt, n, vo, xo, i]

	n = 4;
	t[0] = to;
	v[0] = vo;
	x[0] = xo;

	For[i = 1, i <= n, i++,
	    t[i] = t[i-1] + dt;
	    v[i] = v[i-1] + a[i-1] dt;
	    x[i] = x[i-1] + v[i-1] dt;
	    Print[t[i]];
	    Print["  ", v[i]];
	    Print["  ", x[i]];
	    Print["  "];
	   ];

Comment 2

Step 3 -  Get help on  For  and summarize what you find.

Comment 3

Step 4 - Get help on  Print[].  What do you find?

Comment 4

Step 5 - Copy the code from Step 2 to a cell below and complete the algorithm by adding in the following lines at appropriate places;  keep  n equal to  4  for a while.  Run your program to see if your numerical output looks reasonable.

Comment 5

Step 6 - Use  Table[]  to create a table of  {t, x}  values.  Give your table the name   tempList   so you can refer to it later.

Step 7 - It would be nice to plot the  {t, x}  data.  What MMA command might plot a list of data?   Find the command and describe what it does.

Comment 7

Step 8 -  Use  ListPlot[]  to plot your  {t, x}  points on a graph, but don't connect them by lines.  Use the option

    PlotStyle ->
     {RGBColor[1, 0, 0], PointSize[0.02]}
    
to color your points red with point size of  0.02.  Also be sure to label your axes.

Step 9 - Copy your program from Step 5 to a cell below and insert code that allows you to displays two plots:  x  versus  t   and   v  versus  t.  Eliminate the  Print[] commands from your code.  Use a name for the tables so you can refer to them in the  ListPlot[]  command, but don't print out the tables.  Keep  n   at  4  until your code is working correctly; then switch to  n  equal to  100.

Comment 9

Solution - Encapsulating the Euler Algorithm and Plotting Routine to a Single Function Using  Module[]

Step 1 - Get help on the MMA function  Module[].

?Module

Comment 1

Step 2 - Look over the following code which creates a function which plots the logistic equation and prints out a table of values.  Identify the local and global variables.  Then activate the cell and try out the function with  xo = 0.4,  r = 0.78, and  n = 20.

Clear[xo, r, n, logisticPlot]
logisticPlot[xo_, r_, n_] :=
  Module[
   {i, x, tempList},
   
    x[0] = xo; 
    
    For[i = 1, i <= n, i++,
        x[i] = 4*r*x[i-1]*(1 - x[i-1])];
       
    tempList = Table[x[i], {i, 0, n}];   
    ListPlot[
          tempList,
          PlotStyle  ->
              {RGBColor[1, 0, 0], PointSize[0.02]},
          AxesLabel  -> {"i", "x[i]"},
          PlotRange  -> {0, 1},
          AxesOrigin -> {0, 0}
        ];
        
    Table[{i, x[i]}, {i, 0, n}] // TableForm
  ];

logisticPlot[0.4, 0.78, 20]

Comment 2

Step 3 -  Use  Module[]  to convert your sequence of code for the Euler proplem into a single function,

              eulerDEQ[to, tf, n, vo, xo]. 
  
Then call your function with different parameters to verify that it's working correctly.

Comment 3

Step 4 -  When doing a loop calculation, you may want to abort the calculation either because it's taking too long or you did something to produce an infinite loop.  If this happens, there are several ways to abort, ranging from gentle to harsh:

For[i = 1, i < 1000000, i++]

Save Your Notebook