(*^
::[	Information =

	"This is a Mathematica Notebook file.  It contains ASCII text, and can be
	transferred by email, ftp, or other text-file transfer utility.  It should
	be read or edited using a copy of Mathematica or MathReader.  If you 
	received this as email, use your mail application or copy/paste to save 
	everything from the line containing (*^ down to the line containing ^*)
	into a plain text file.  On some systems you may have to give the file a 
	name ending with ".ma" to allow Mathematica to recognize it as a Notebook.
	The line below identifies what version of Mathematica created this file,
	but it can be opened using any other version as well.";

	FrontEndVersion = "Macintosh Mathematica Notebook Front End Version 2.2";

	MacintoshStandardFontEncoding; 
	
	fontset = title, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, e8,  24, "Times"; 
	fontset = subtitle, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, e6,  18, "Times"; 
	fontset = subsubtitle, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, italic, e6,  14, "Times"; 
	fontset = section, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, grayBox, M22, bold, a20,  18, "Times"; 
	fontset = subsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, blackBox, M19, bold, a15,  14, "Times"; 
	fontset = subsubsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, whiteBox, M18, bold, a12,  12, "Times"; 
	fontset = text, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
	fontset = smalltext, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  10, "Times"; 
	fontset = input, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeInput, M42, N23, bold, L-5,  12, "Courier"; 
	fontset = output, output, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L-5,  12, "Courier"; 
	fontset = message, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, R65535, L-5,  12, "Courier"; 
	fontset = print, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L-5,  12, "Courier"; 
	fontset = info, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, B65535, L-5,  12, "Courier"; 
	fontset = postscript, PostScript, formatAsPostScript, output, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeGraphics, M7, l34, w282, h287,  12, "Courier"; 
	fontset = name, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, italic,  10, "Geneva"; 
	fontset = header, inactive, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
	fontset = leftheader, inactive, L2,  12, "Times"; 
	fontset = footer, inactive, noKeepOnOnePage, preserveAspect, center, M7,  12, "Times"; 
	fontset = leftfooter, inactive, L2,  12, "Times"; 
	fontset = help, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  10, "Times"; 
	fontset = clipboard, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
	fontset = completions, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
	fontset = special1, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
	fontset = special2, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
	fontset = special3, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
	fontset = special4, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
	fontset = special5, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7,  12, "Times"; 
	paletteColors = 128; currentKernel; 
]
:[font = subsubsection; inactive; Cclosed; preserveAspect; startGroup]
Legal Notice, Disclaimer, and Book Ordering Information:
:[font = input; inactive; preserveAspect; endGroup]
(* Legal Notice, Disclaimer, and Book Ordering Information:

Last Modified: FIO January 1, 1995

Mathematica For Physics
Robert L. Zimmerman and Fredrick I. Olness
MathSource Number: 0206-862 

ISBN 0-201-53796-6
For ordering information, call 1-800-822-6339
Addison-Wesley Publishing Company

Communication with the authors:
Fredrick I. Olness:  olness@mail.physics.smu.edu
Robert L. Zimmerman: bob@zim.uoregon.edu

Copyright 1995, Addison-Wesley Publishing Company, Inc. The material in
this file may be distributed freely  so long as the content remains
unchanged, and the copyright and  reference notices are included. The
publisher grants permission for the noncommercial use of these programs
and program segments.  All other uses require the prior written consent
of the publisher.    

All rights are reserved with regard to the  material in the
"Mathematica For Physics" text, and may not be reproduced, stored in a
retrieval system, or transmitted,  in any form or by any means,
electronic, mechanical, photocopying,  recording, or otherwise, without
the prior written permission  of the publisher. 

The programs and applications presented in this book have been included
for  their instructional value. They have been tested with care but are
not  guaranteed for any particular purpose. The publisher does not
offer any  warranties or representations, nor does it accept any
liabilities with  respect to the programs or applications. 

 *)

:[font = title; inactive; initialization; preserveAspect]
3. OSCILLATING SYSTEMS
:[font = input; initialization; preserveAspect]
*)
(* Some "Mathematica for Physics" Initializations *)
$DefaultFont={"Courier",8};
Off[General::spell];
Off[General::spell1];
PageWidth->70;
(*
:[font = input; initialization; preserveAspect]
*)
Needs["Algebra`SymbolicSum`"];
Needs["Algebra`Trigonometry`"];
Needs["Calculus`FourierTransform`"];
Needs["Calculus`LaplaceTransform`"];
Needs["Graphics`FilledPlot`"];
Needs["Graphics`Animation`"];
Needs["Graphics`Graphics3D`"];
(*
:[font = subsection; inactive; initialization; preserveAspect]
User-Defined Procedures:
:[font = subsubsection; inactive; initialization; preserveAspect; startGroup]
1  Series expansion for second-order equation     
:[font = input; initialization; preserveAspect]
*)
diffSeriesOne::usage=" 
diffSeriesOne[eq,z,t,order,{initialList}]
eq is an equation, z is the function, t is the independent 
variable, order is the order of the Series solution, and 
{initialList} is a list of initial conditions such as
{z[0]->z0}.
This procedure assumes the solution can be written as 
a power series.";
(*
:[font = input; initialization; preserveAspect; endGroup]
*)
Clear[diffSeriesOne];
diffSeriesOne[eqin_,z_,t_,order_,initList__List]:= 
 Module[{eq,zSer,eSer,eqs,vars,sol,zSol},
   eq=eqin[[1]]-eqin[[2]]==0;
   zSer= Series[z[t],{t,0,order}];
   eSer= eq/.(z[t]->zSer)//ExpandAll;
   eqs = Thread[CoefficientList[eSer[[1]],t]==0];
   vars=(Table[D[z[t],{t,j}],{j,2,order+2}]/.t->0);     
   sol = Solve[eqs,vars][[1]];
   zSol= zSer/.sol/.initList;
   Return[ zSol]  
 ]
(*
:[font = subsubsection; inactive; initialization; preserveAspect; startGroup]
2. Phase Plot for one-dimensional System    
:[font = input; initialization; preserveAspect]
*)
phasePlot::usage="phasePlot[q,p,{tList}]
This procedure will plot the phase trajectory {q,p}. 
{tlist} is of the form {t,tMin,tMax}.";
(*
:[font = input; initialization; preserveAspect; endGroup]
*)
Clear[phasePlot];
phasePlot[q_,p_,tList_List]:= 
  ParametricPlot[{p,q},tList
    ,PlotRange->All
    ,Axes->False
    ,PlotLabel->"Phase Trajectory"
    ,Frame->True
    ,FrameLabel->{"p-axis","x"}]
(*
:[font = subsubsection; inactive; initialization; preserveAspect; startGroup]
3. Time behavior of Phase Plot for a one-dimensional System 
:[font = input; initialization; preserveAspect]
*)
timePhasePlot::usage=" timePhasePlot[q,p,{tList}]
This procedure will plot the time evolution of the trajectory. 
{tlist} is of the form {t,tMin,tMax}.";
(*
:[font = input; initialization; preserveAspect; endGroup]
*)
Clear[timePhasePlot];
timePhasePlot[q_,p_,tList_List]:= 
 ParametricPlot3D[{p,q,tList[[1]]},tList  
    ,PlotRange->All
    ,PlotLabel->"Evolution of Phase"
    ,AxesLabel->{"p-axis","x-axis","time"}
    ,PlotPoints->300
    ,BoxRatios->{1,1,1}]    
(*
:[font = subsubsection; inactive; initialization; preserveAspect; startGroup]
4. doublePlot: Phase Plots and Time Evolution for a one-dimensional System 
:[font = input; initialization; preserveAspect]
*)
doublePlot::usage=" doublePlot[q,p,{tList}]
Combines phasePlot and timePhasePlot.";
(*
:[font = input; initialization; preserveAspect; endGroup]
*)
Clear[doublePlot];
doublePlot[q_,p_,tList_List]:= 
Show[GraphicsArray[{    phasePlot[q,p,tList]
                   ,timePhasePlot[q,p,tList]
                   }]
    ];
(*
:[font = subsubsection; inactive; initialization; preserveAspect]
5. Fast Fourier Transform
:[font = input; initialization; preserveAspect]
*)
fftSpectrum::usage="
fftSpectrum[f, t, tMax]
This procedure will plot the fast Fourier transform of 
the function f. The time interval is from 0 to tMax. 
The number of steps is fixed at 512.";
(*
:[font = input; initialization; preserveAspect]
*)
fftSpectrum[f_,t_,tMax_] :=
  Module[{dt,fData,ffData,plot},
     dt    = tMax/(512-1)//N;
     fData = Table[f,{t,0,tMax,dt}]//N;  
     ffData= Abs[Fourier[fData]] ;
     plot  = ListPlot[ffData,PlotJoined->True]
  ]
(*
:[font = subsubsection; inactive; initialization; preserveAspect; startGroup]
6. Eigenvalues and Eigenvectors for Small Oscillating Systems   
:[font = input; initialization; preserveAspect]
*)
 smallOsc::usage="
 smallOsc[T,V,{q1[t],q2[t],...},{q10,q20,...},t]
 Given the expressions for the kinetic and potential energies 
 of the system, this procedure will find the eigenfrequencies
 and eigenvectors. Enter the kinetic energy T, potential V, 
 coordinates, equilibrium positions, and time variable. The 
 first list in the output are the eigenfrequencies and the
 remaining lists are the eigenvectors. ";
(*
:[font = input; initialization; preserveAspect; endGroup]
*)
Clear[smallOsc];
smallOsc[T_,V_ ,coordList__,equlibList__,t_:t]:= 
  Module[{q,p,dim,equlibRule,Vij,Tij,matrix,w2Sol,
              freq,vector,eqs,vSol},
    q=coordList;
    p=D[coordList,t];
    dim=Length[q];
    equlibRule=Thread[coordList-> equlibList];
    Vij=Outer[D[V,#1,#2]&,q,q]; 
    Tij=Outer[D[T,#1,#2]&,p,p];
    matrix= Vij-w2 Tij /.equlibRule; 
    w2Sol= Solve[ Det[matrix]==0,w2]//Simplify;
    freq=  w2/.w2Sol; 
    vector=Array[a,dim];
    eqs= Thread[0==matrix.vector]//ExpandAll//Simplify ;
    vSol=
    Table[ 
       Solve[
          Join[eqs/.w2Sol[[i]],{vector.vector==1}]
      ,vector][[2]]
    ,{i,1,dim}];   
    Return[{freq,vSol}]]
(*
:[font = subsubsection; inactive; initialization; preserveAspect; startGroup]
7. Animation for Linear Motion
:[font = input; initialization; preserveAspect]
*)
 oscMovie::usage="oscMovie[f,tMax,frames,{range}:Automatic] 
 Given a periodic function f of variable t, this procedure
 animates the motion in 1-dimension. 
 tMax is the final value of t, the range 
 determines the graphics window, and frames is the number 
 of frames. The frames are stored in the array plot[i].";
(*
:[font = input; initialization; preserveAspect; endGroup]
*)
Clear[oscMovie];
oscMovie[f_,tMax_,frames_,range_:Automatic]:= 
Module[{t,i},
Do[
 t=i tMax/frames;
 plot[i]= 
   Show[Graphics[{PointSize[.05],Point[{f[t]//N,0}]}
                 ,PlotRange->{range,{-1,1}}]
     ,Axes->True]
,{i,1,frames}]
];
(*
:[font = subsection; inactive; initialization; preserveAspect; startGroup]
Protect User-Defined Procedures:
:[font = input; initialization; preserveAspect; endGroup]
*)
Protect[diffSeriesOne,phasePlot,timePhasePlot,doublePlot
       ,fftSpectrum,smallOsc,oscMovie];
(*
^*)
