This Workbook sets up, simulates and graphs the  equations that describe the reaction chain in section 3.4 of SB2:
x1 <=> x2 <=> x3 --> x4
BOP Sept  15, 2007
Addiel de Alba. April,2010
NS, March 21, 2011

SetDirectory[NotebookDirectory[]];
<<"GraphicsDirectives.m"

Step 1: define the equations

rhs1=b1-v1;
rhs2=v1-v2;
rhs3=v2-v3;
rhs4=v3;

step 2 : define the parmeter values and rate laws

k1=1;k2=0.01;k3=0.0001;
K1=1.0;K2=1.0;
v1=k1*(x1[t]-x2[t]/K1);
v2=k2*(x2[t]-x3[t]/K2);
v3=k3*x3[t];

step 3:  set up the conditions and simulate

b1=0;
tfinal=50000;
solution=NDSolve[{x1'[t]==rhs1,x2'[t]==rhs2,x3'[t]==rhs3,x4'[t]==rhs4,x1[0]==1.0,x2[0]==0,x3[0]==0,x4[0]==0.},{x1,x2,x3,x4},{t,0,tfinal}][[1]];

step 4:  graph the results

tooltipFunc=Tooltip[#/.solution,#]&/@{x1[t],x2[t],x3[t],x4[t]};
Manipulate[
Plot[Evaluate[tooltipFunc],{t,tStart,tStop},AxesLabel->{"Time","Concentration"},PlotLabel->"Concentrations vs time",Epilog->createLegend[{"x1","x2","x3","x4"}]],
{{tStart,0},0.,tfinal},
{{tStop,10.},tStart+0.1,tfinal},Bookmarks->{"Fast time scale":>{tStart=.01,tStop=3},"Intermediate time scale":>{tStart=3,tStop=300},"Slow time scale":>{tStart=300,tStop=tfinal}}
]

ParametricPlot[Evaluate[{x1[t],x2[t]}/.solution],{t,0,tfinal},PlotLabel->"Dynamic Phase portrait",FrameLabel->{"x1","x2"},Epilog->ppAnnotate[{x1,x2},solution,tfinal]]

Quiet@LogLogPlot[Evaluate[(Tooltip[#,ToString[#]]&/@{v1,v2,v3})/.solution],{t,0.1,tfinal},PlotRange->All,AxesLabel->{"Time",None},Epilog->createLegend[{"v1","v2","v3"}]]
Quiet@LogLinearPlot[Evaluate[{x1[t],x2[t],x3[t],x4[t]}/.solution],{t,0.1,tfinal},AxesLabel->{"Time",None},Epilog->createLegend[{"x1","x2","x3","x4"}]]

system={{x1->x2,v1},{x2->x1,-v1},{x2->x3,v2},{x3->x2,-v2},{x3->x4,v3}};
vertexFunc={Opacity[0.5,ColorData["Rainbow"][#2[t]/.solution]],Disk[#1,Evaluate[(#2[t]/.solution)]*.5],Black,Circle[#1,Evaluate[(#2[t]/.solution)]*.5],Style[Text[#2,#1],FontFamily->"Helvetica",FontColor->Black,12]}&;
edgeFunc={ColorData["Rainbow"][#3/.solution],Sequence@@If[(val=#3/.solution)≤0,{Dotted,Thickness[0.]},{Thickness[(Log[10,val]+8)/500.]}],Arrow[#1]}&;

tPoints=Table[10^x,{x,-1,N@Log[10,tfinal],.02}];
legend=createLegend[{"x1","x2","x3","x4"}];
animeFrames=Table[plot=Quiet@LogLinearPlot[Evaluate[{x1[t2],x2[t2],x3[t2],x4[t2]}/.solution],{t2,0.01,t},AxesLabel->{"Time",None},Epilog->createLegend[{"x1","x2","x3","x4"}]];
inset=Inset[GraphPlot[system,VertexRenderingFunction->vertexFunc,MultiedgeStyle->True,EdgeLabeling->False,EdgeRenderingFunction->edgeFunc,PlotLabel->None,ImageSize->{400,400}],Offset[{20,20},Scaled[{.75,.75}]],{Right,Top},Scaled[.5]];
Show[plot,Epilog->{inset,legend}],
{t,tPoints}];
Length@%

superAnime=ListAnimate[animeFrames]

SetDirectory[NotebookDirectory[]];
Export["Section3.4.Animation.flv",animeFrames,"FrameRate"->15]