Iterated Equations in Java

The logistic equation is one of the simplest systems that exhibits chaotic behavior. The equation is as follows:

pt+1 = r * pt * (1 - pt)

This is an iterative equation because the value of pi depends on the value of pt-1. Given the values of r and p0 it seems trivial to write a computer program that calculates pt for arbitrary t. In fact matters are a little more complicated than they seem at first. In particular, depending on the value of r, this equation can be chaotic and exhibits sensitive dependence on initial conditions.

Generally the equation is subject to these constraints:

0 <= p0 <= 1
0 <= r <= 4

Normally p is called the population, r the rate, and t the time. With this interpretation the meaning of the first condition above becomes obvious. The population is always between 0% and 100%.

populationt+1 = rate * populationt * (1 - populationt)

If you like you can think of population as a function of three variables, time (t), initial population, and rate. However, the long-term behavior of this equation mostly depends on the rate, not the generation or the initial population. For rates less than 3, the population converges to one value. The speed and nature of that convergence (with or without overshoot) depends on the rate, In particular 1, 2, and 3 are all critical points where the behavior of the equation over time exhibits qualitative change. Between 3 and 3.57, the equation oscillates between values, initially between two values, then four, then eight, then sixteen, and so on until chaotic behavior sets in around 3.57. though there are occasional islands of stability (for instance at ????).

Background Reading

Chaos: Making a New Science
by James Gleick

Does God Play Dice?: The Mathematics of Chaos
by Ian Stewart

An Introduction to Chaotic Dynamical Systems (Addison-Wesley Studies in Nonlinearity)
by Robert L. Devaney

sci.fractals FAQ - Logistic equation (http://www.faqs.org/faqs/fractal-faq/section-9.html)

You may also want to search the Web for more info.

Homework

Note that this is not written in stone and details will change. Furthermore, this is not your only homework for a given class.

Problem 1:

Write a program that prints 200 iterations of the logistic equation for fixed values of rate and initial population (e.g. rate = 2.2, p0 = 0.002). Also print (at the beginning of the output) the values used for rate and initial population.

Use full-length variable names (e.g. population rather than p).

Indent code with spaces rather than tabs.

Do not overdo it. This is a straight-forward and simple problem that requires a straight-forward and simple answer.

Include your name, email address, homework number (1), and a brief description of the problem in header comments. Include other comments as necessary.

Problem 2:

Write a program that reads the initial population, number of generations, and rate from the command line, and prints that many iterations of the logistic equation. Also print (at the beginning of the output) the values used for all three quantities.

Be sure to handle all user errors (e.g. not providing three values on the command line) to the extent possible. Check the ranges of the data the user enters (e.g. don't let population be bigger than 1 or less than 0). Print an error message and exit if a value outside the allowed range is entered. Provide a default value if none is given.

Problem 3:

Write a program that reads the initial population, number of generations, and rate from the command line, and prints that many iterations of the logistic equation. Also print (at the beginning of the output) the values used for all three quantities.

Be sure to handle all user errors (e.g. not providing three values on the command line) to the extent possible. Check the ranges of the data the user enters (e.g. don't let population be bigger than 1 or less than 0). Print an error message and exit if a value outside the allowed range is entered. Provide a default value if none is given.

Detect if convergence has occurred and stop output if so. Be especially careful of equality comparisons between floating point numbers.

Problem 4:

Write a program which prints a table containing the number of generations required for convergence vs. initial population (X axis) and rate (Y axis). Increment initial population from 0 to 1 by 0.1. Increment rate from 1 to 3 by 0.25. For extra brownie points output it as an HTML table rather than ASCII text, and hand in the printout from your Web browser.

Problem 5:

Write a program that reads the initial population, number of generations, and rate from the command line, and prints that many iterations of the logistic equation. Also print (at the beginning of the output) the values used for all three quantities.

Be sure to handle all user errors (e.g. not providing three values on the command line) to the extent possible. Check the ranges of the data the user enters (e.g. don't let population be bigger than 1 or less than 0). Print an error message and exit if a value outside the allowed range is entered. Provide a default value if none is given.

Detect if convergence or static oscillation has occurred and stop output if so. Be especially careful of equality comparisons between floating point numbers.

Problem 6:

Write a LogisticEquation class which represents a particular instance of the logistic equation (that is particular values for rate and initial population). Set these values in the constructor. Provide methods to get the rate, get the population of a specified generation, get the converged value (return -1 if there is no converged value; we'll fix this later), get the list of oscillating values, and the period of oscillation, and to return the complete list of values.

Rewrite Problem 4 and Problem 5 using this LogisticEquation class.

Problem 7:

Add equals(), hashCode(), and toString() methods to the logistic equation class. Be sure to think carefully about what it means for two logistic equations to be equal and document and justify your choice.

Problem 8:

Make the LogisticEquation class cloneable.

Problem 9:

Place the LogisticEquation class in a suitably named package. Then rewrite the LogisticArguments and LogisticTable classes to use this new class. However, leave the LogisticArguments and LogisticTable classes in the default package.

Problem 10:

Write a version of the logistic equation class that uses java.math.BigDecimal instead of doubles internally. (Does it make sense to provide overloaded versions of the public methods that take big decimals or strings as arguments instead of doubles?) Place it in the same package used above. Then rewrite the LogisticArguments and LogisticTable classes to use this new class. However, leave the LogisticArguments and LogisticTable classes in the default package.

Problem 11:

Write an abstract class representing all iterated equations; i.e. not just the logistic equation but any iterated equation. Put as much functionality in this class as possible while still making it quite general. Rewrite the logistic equation class so that it's a subclass of this class. Put it in the same package as above. Then rewrite the LogisticArguments and LogisticTable classes to use this new class. However, leave the LogisticArguments and LogisticTable classes in the default package.

Problem 12:

Write two new subclasses of the iterated equation class that iterate these equations:

Provide table and argument classes for these as well, along with sample output. Investigate and describe their behavior; e.g. where are the fixed points? Where do they converge? Where do they oscillate? Where are they chaotic? Do they exhibit period doubling?

Problem 13:

Rewrite the iterated equation classes to handle problem values (e.g. population greater than 1) by throwing IllegalArgumentExceptions with appropriate messages. Then rewrite the Arguments and Table classes to use these new classes.

Problem 14:

Put the entire compiled set of files in a runnable jar file. Hand in on a standard DOS/Windows floppy disk as well as paper. Use an 8.3 file name for the jar archive (e.g. iterated.jar).

Problem 15:

Write an interface that describes an arbitrary indexed collection of java.lang.Number objects such as that represented by the IteratedEquation class. Make the IteratedEquation class implement this interface.

Problem 16:

Write an applet that draws a graph of p(n) vs. n where p(n) is the nth iteration of the logistic equation (p(i) = r*p(i-1)*(1.0-p(i-1)). (You only need to draw the graph. You do not need to draw axes or legends or anything of that nature.)

Use PARAM tags to specify the rate, the initial population, precision, and the number of iterations to track. Note that it is not necessary to achieve convergence. Thus this problem is a little different from previous ones involving the logistic equation.

Handle all exceptions reasonably. (Printing an error message on System.out and exiting is not reasonable.) Assume one generation per horizontal pixel. Make this applet reasonably quick.

Make the applet available on a Web page. Include at least three instances of the applet on the page: one with a rate between 1 and 2, one with a rate between 2 and 3, and one with a rate between 3 and 4. Include a link to full source code for the applet. Hand in the source code for the applet, sample HTML files for the applet, screenshots of the running applet, and a URL where the applet can be viewed.

Problem 17:

Write a subclass of either Canvas or Component that draws the graph of an iterated equation. (You only need to draw the graph. You do not need to draw axes or legends or anything of that nature.)

Problem 18:

Write an applet that uses the above canvas/component to draw the graph of an arbitrary iterated equation.

Problem 19:

To the LogisticApplet, add the necessary user interface components to allow the user to set the rate (probably a label, a text field, and a button).

Problem 20:

Use the necessary layout managers to arrange the applet.

Problem 21:

Write a new lightweight component or Canvas subclass for plotting X-Y graphs. Rewrite last week's applet to use this new component. At this stage it is enough to merely draw the graph. You do not need to draw axes, legends, tick marks, or similar material. This component should be in a new package, and should not have any knowledge of the logistic equation classes. (Should an interface be defined which classes that want to be graphed can implement, possibly through an adaptor? or should the interface be in the X-Y plot component? In other words, does the X-Y plotter ask the objects the graphing for their data, or do the objects tell the X-Y plotter what to plot? Whichever way it goes, there should almost certainly be a middle layer adaptor between the thing plotted and the plotter. )

Write a new iterated equation applet that uses the X-Y graph component.

Problem 22:

Make last week's applet a panel. Write an applet that uses the panel. Make the panel and the applet that contains it reasonably attractive by using a LayoutManager.

Then write a frame based stand-alone application that uses frames, each containing an instance of the above panel. Don't do anything that would prevent the program from being expanded to a multiple window application. Hand in the application as a runnable jar archive.

Problem 23:

Add alert dialogs that warn the user when they're attempting to set an invalid value.

Problem 24:

Add File and Edit menus to the frame of last week's program. The File menu should contain New, Open, Close, Save, Save As, Page Setup, Print, and Exit menu items. The Edit Menu should contain Undo, Cut, Copy, Paste, and Clear menu items. All menu items should have the standard menu shortcuts. The New, Close, and Exit menu items should work. The rest should be disabled. Use menu separators and ... where customary.

Problem 25:

Make Save, Save As, and Open all work. You will need to design a file format for the data.

Problem 26:

Put the calculation of the iterated data in a separate thread.

Problem 27:

Make printing work.

Problem 28:

Make Copy and Paste work.

Final Exam

Give the applet an option of drawing a bifurcation diagram instead. You will probably need to write a scatter plot component instead of an X-Y plot component.

Related questions

  1. How can the logistic equation be generalized?

  2. What happens if the constraints are relaxed?


Java Course Notes | Cafe au Lait
Last Modified May 29, 2000
Copyright 1999, 2000 Elliotte Rusty Harold
elharo@metalab.unc.edu