Here is Marko's solution to Part 3. of Project 2, for your reference. We don't expect you to be this elegant. T. Witten 1/20/02
/*
<html>
<applet code=RootNew.class width=100 height=100></applet>
</html>
<pre>
*/
import java.awt.* ;
import java.applet.* ;
public class RootNew extends Applet implements Runnable
{
Thread thr = new Thread(this) ;
Tracer tr = new Tracer() ; // create Tracer window
final int MAXTRIES=10000 ; // maximum # of NR iterations
double r=3.2 ; // r
int cycle=2 ; // cycle we are looking at
double epsilon=1e-12 ; // desired NR accuracy
public void init ()
{
thr.start() ; // start Thread
}
public void run ()
{
double sx=0, x, newx, delta, h ;
int tries ;
int i ;
do {
sx = tr.trace("startvalue x=", sx) ; // get NR startvalue
r = tr.trace("r=", r) ; // get r value
epsilon = tr.trace("epsilon=", epsilon) ; // get epsilon
cycle = tr.trace("cycle", cycle) ; // get cycle
newx = sx ;
tries = 0 ;
do { // do NR until |f(x)|<epsilon or tries>MAXTRIES
tries++ ;
x = newx ;
delta = -f(x)/dfdx(x) ;
newx = x+delta ;
} while ((tries < MAXTRIES) && (Math.abs(f(x))>epsilon)) ;
if (tries == MAXTRIES) showStatus("Desired accuracy not reached!") ;
// NR didn't converge
tr.trace("Root=", newx) ; // display Root
tr.trace("f(Root)=", f(newx)) ; // check: |f(Root)| < epsilon
tr.trace("Floquet=", floquet(newx, cycle)) ; // floquet multiplier
} while (true) ; // tight loop in thread
}
double floquet (double x, int n) // Floquet multiplier at x
{
double h = 1.0 ;
int i ;
for (i=0; i<n; i++) {
x = r*x*(1-x) ;
h = h*r*(1-2*x) ;
}
return h ;
}
double logistic (double x, int n) // calculate n'th iterate of logistic map
{
int i ;
double h ;
h = r*x*(1-x) ;
for (i=1; i<n; i++) {
h = r*h*(1-h) ;
}
return h ;
}
double d_logistic_dx (double x, int n) // derivative of n'th iterate of logistic map
{
int i ;
double h = 1.0 ;
for (i=0; i<n; i++) {
h = h*r*(1-2*x) ;
x = r*x*(1-x) ;
}
return h ;
}
double f (double x ) {
return x - logistic(x, cycle); // f(x) = x - Logistic Map(x)
}
double dfdx (double x ) { // derivative of f(x)
return 1.0 - d_logistic_dx(x, cycle) ;
}
}