package org.opensourcephysics.sip.ch9; import java.awt.Color; import org.opensourcephysics.controls.*; import org.opensourcephysics.display.*; // graphical solution for trajectory of logistic map public class GraphicalSolutionApp implements Animation { Control myControl; Thread animationThread; DrawingFrame drawingFrame; PlottingPanel plottingPanel; DatasetCollection dataset = new DatasetCollection(); double r; // control parameter int iterate; // iterate of f(x) double x, y; double x0, y0; public GraphicalSolutionApp() { plottingPanel = new PlottingPanel("iterations", "x", "Graph Sol"); plottingPanel.addDrawable(dataset); drawingFrame = new DrawingFrame(plottingPanel); } public void setControl(Control control) { myControl = control; resetAnimation(); } public void resetAnimation() { dataset.clear(); plottingPanel.repaint(); r = 0.89; x = 0.2; iterate = 1; myControl.setValue("r", r); myControl.setValue("x", x); myControl.setValue("iterate", iterate); } public void stopAnimation() { Thread runningThread = animationThread; animationThread = null; // stop the animation try { // wait for the thread to die if (runningThread != null) { runningThread.join(); } } catch(InterruptedException e) {} } public void initializeAnimation() { r = myControl.getDouble("r"); x = myControl.getDouble("x"); x0 = x; iterate = myControl.getInt("iterate"); drawFunction(); y0 = 0; x = x0; } public void startAnimation() { animationThread = new Thread(this); animationThread.start(); } public void run() { while (Thread.currentThread() == animationThread) { stepAnimation(); try { Thread.sleep(100); } catch(InterruptedException e) {} } } public void stepAnimation() { y = f(x, r, iterate); dataset.append(1, x0, y0); dataset.append(1, x0, y); dataset.append(1, y, y); plottingPanel.render(); x0 = y; y0 = y; x = y; } public void drawFunction() { int nplot = 200; // # of points at which function computed double delta = 1.0/nplot; x = 0; y = 0; for (int i = 0; i <= nplot; i++) { y = f(x, r, iterate); dataset.append(0, x, y); x = x + delta; } } // f defined by recursive procedure public double f(double x, double r, int iterate) { if (iterate > 1) { double y = f(x, r, iterate - 1); return 4*r*y*(1 - y); } else { return 4*r*x*(1 - x); } } public static void main(String[] args) { Animation app = new GraphicalSolutionApp(); Control c = new AnimationControl(app); app.setControl(c); } }