(*
   This mathematica file creates the file differential.anim to
   demonstrate the definition of the differential

                    dy = f'(x) dx = f'(x) delta x

   and particularly to show that dy is a good approximation for

                  delta y = f(x + dx) - f(x).

   It aims to clarify what is meant by the statement that "delta y - dy
   is small compared to dy".

   The animation displays two views of the graph of y=x^2 and its
   tangent line through x=.3, together with the lines representing dx =
   delta x, dy, and delta y - dy, for various values of dx.  The first
   view leaves the limits of the area plotted fixed, so dx and related
   quantities become smaller.  The second view zooms in as dx gets
   smaller, so it occupies a fixed length on the plot.  A grid is
   drawn, to provide cueing for the zoom.

   Note: It is advisable to compress the file--it is big, but
   compresses a lot).

   Sample viewing command:

     motifps -geometry 1000x500+70+0 differential.anim

   Douglas N. Arnold, 10/11/92

   Douglas N. Arnold  <dna@math.psu.edu>
   Department of Mathematics
   Penn State
   University Park, PA  16802

   Online documentation related to this file can be found at WWW URL
   http://www.math.psu.edu/dna/graphics.html.*)

BeginPackage["Differential`", "Graphics`Animation`", "Graphics`Colors`"]

Begin["Private`"]

(* Describe the individual semiframes. *)

(*
    FramedWithFixedLimits[expr,x,x0,xmin,xmax,ymin,ymax,e,gx,gy]
    shows a graph of expr as x ranges from xmin to xmax, and y ranges
    from ymin to ymax.  The tangent line through x=x0 is shown,
    as is the triangle built on dx = e.  The plot is framed with
    tick labels, and the values of dx, dy, and del y are displayed.
    A grid with spacing determined by gx and gy is also displayed.
*)
FrameWithFixedLimits[expr_,x_,x0_,xmin_,xmax_,ymin_,ymax_,e_,gx_,gy_] :=
Module[{nx,deltax,y0,f,g,graph,tangent,line1,line2,line3,grid,n},
  f[t_] := (expr /. x -> t);
  g[t_] := y0 + f'[x0](t-x0);
  y0 = f[x0];
  nx = 100;
  deltax = (xmax - xmin) / nx;
  graph = Line[Table[{xmin + n deltax,f[xmin + n deltax]},{n,0,nx}]];
  tangent = Line[{{xmin,g[xmin]},{xmax,g[xmax]}}];
  grid = Join[ Table[Line[{{n,ymin},{n,ymax}}],{n,xmin,xmax,(xmax-xmin)/gx}],
               Table[Line[{{xmin,n},{xmax,n}}],{n,ymin,ymax,(ymax-ymin)/gy}] ];
  line1 = Line[{{x0,f[x0]},{x0 + e,f[x0]}}];
  line2 = Line[{{x0 + e,f[x0]},{x0 + e,g[x0 + e]}}];
  line3 = Line[{{x0 + e,g[x0 + e]},{x0 + e,f[x0 + e]}}];
  Graphics[
    Flatten[
      { GrayLevel[.5], Thickness[0.001], grid,
        Magenta, Thickness[0.007], graph,
        Cyan, tangent,
        Green, line1, Red, line2, Yellow, line3,
        Text["   dx =" PaddedForm[N[e],{6,4}],Scaled[{.5,.9}],{1,0}],
        Text["   dy =" PaddedForm[N[g[x0 + e]-f[x0]],{6,4}],
               Scaled[{.5,.8}],{1,0}],
        Text["del y =" PaddedForm[N[f[x0 + e]-f[x0]],{6,4}],
               Scaled[{.5,.7}],{1,0}] }
    ] , PlotRange -> {{xmin,xmax},{ymin,ymax}}, AspectRatio ->1, 
        Background -> Black, DefaultColor -> White, Frame -> True,
        FrameTicks -> { {-0.2, {-0.15, ""}, {-0.1, ""}, {-0.05, ""},
                          0., {0.05, ""}, {0.1, ""}, {0.15, ""},
                          0.2, {0.25, ""}, {0.3, ""}, {0.35, ""},
                          0.4, {0.45, ""}, {0.5, ""}, {0.55, ""},
                          0.6, {0.65, ""}, {0.7, ""}, {0.75, ""},
                          0.8, {0.85, ""}, {0.9, ""}, {0.95, ""},
                          1.},
                        {{-0.3, ""}, {-0.25, ""},
                          -0.2, {-0.15, ""}, {-0.1, ""}, {-0.05, ""},
                          0., {0.05, ""}, {0.1, ""}, {0.15, ""},
                          0.2, {0.25, ""}, {0.3, ""}, {0.35, ""},
                          0.4, {0.45, ""}, {0.5, ""}, {0.55, ""},
                          0.6, {0.65, ""}, {0.7, ""}, {0.75, ""},
                          0.8, {0.85, ""}, {0.9, ""}},
                        None,None},
        PlotRegion -> {{0,1},{.025,.975}},
        DefaultFont -> {"Helvetica-Bold",24.}
  ]
]


(*
    FramedWithFixedTriangle[expr,x,x0,a,b,c,d,e,s,gx,gy]
    shows the same graph and tangent line as FramedWithFixedLimits, but
    the plot range is x from x0 - s a to x0 + s b, y from y0 -s c to
    y0 + s d, where y0 is the function value at x0, and the triangle is
    shown with base dx = s e.  If e is fixed but s varied, this results
    in the triangle being of fixed length in device coordinates and the
    point (x0,y0) having fixed device coordinates.  The values of dx,
    dy, and del y are displayed. A grid with spacing determined by gx
    and gy is also displayed.
*)
FrameWithFixedTriangle[expr_,x_,x0_,a_,b_,c_,d_,e_,s_,gx_,gy_] :=
Module[{xmin,xmax,ymin,ymax,nx,deltax,y0,f,g,graph,tangent,
        line1,line2,line3,grid,n},
  f[t_] := (expr /. x -> t);
  g[t_] := y0 + f'[x0](t-x0);
  y0 = f[x0];
  xmin = x0 - s a;
  xmax = x0 + s b;
  ymin = y0 - s c;
  ymax = y0 + s d;
  nx = 100;
  deltax = (xmax - xmin) / nx;
  graph = Line[Table[{xmin + n deltax,f[xmin + n deltax]},{n,0,nx}]];
  tangent = Line[{{xmin,g[xmin]},{xmax,g[xmax]}}];
  line1 = Line[{{x0,f[x0]},{x0 + s e,f[x0]}}];
  line2 = Line[{{x0 + s e,f[x0]},{x0 + s e,g[x0 + s e]}}];
  line3 = Line[{{x0 + s e,g[x0 + s e]},{x0 + s e,f[x0 + s e]}}];
  grid = Join[ Table[Line[{{n,ymin},{n,ymax}}],{n,x0-a,x0+b,(a+b)/gx}],
               Table[Line[{{xmin,n},{xmax,n}}],{n,y0-c,y0+d,(c+d)/gy}] ];
  Graphics[
    Flatten[
      { GrayLevel[.5], Thickness[0.001], grid,
        Magenta, Thickness[0.007], graph,
        Cyan, tangent,
        Green, line1, Red, line2, Yellow, line3  (*,
        Text["   dx =" PaddedForm[N[s e],{6,4}],Scaled[{.5,.9}],{1,0}],
        Text["   dy =" PaddedForm[N[g[x0 + s e]-f[x0]],{6,4}],
               Scaled[{.5,.8}],{1,0}],
        Text["del y =" PaddedForm[N[f[x0 + s e]-f[x0]],{6,4}],
               Scaled[{.5,.7}],{1,0}]*)
         } ] , 
        PlotRange -> {{xmin,xmax},{ymin,ymax}}, AspectRatio ->1, 
        Background -> Black,
        PlotRegion -> {{.1,1},{.12,.975}},
            (* The xmin and ymin values have been adjusted
	       to compensate for the axis labels on the first view.
	       This depends on the font and on the geometry of the
               viewing window. *)
        DefaultFont->{"Helvetica-Bold",24.}
  ]
]

(* Create the animations. *)

nframes = 20  (* 20 seems reasonable for a SparcStation 2 with 32 MB,
                larger values give smoother animation, but use more memory *)

(* I don't know why I get this error message, so let's suppress it! *)
Off[Show::gmed]

ShowAnimation[Table[GraphicsArray[{
      FrameWithFixedLimits[x^2,x,.3,-.2,1,-.3,.9,e,12,12],
      FrameWithFixedTriangle[x^2,x,.3,.5,.7,.39,.81,.5,e/.5,12,12]}],
        {e,.5,.5/nframes,-.5/nframes}],
  AnimationFunction->(DisplayAnimation["differential.anim",#]&)]

(* Restore the error message. *)

End[] (* end of context private *)

EndPackage[]

(* Write an exit message. *)

Print["-- wrote file differential.anim --"]
