(*THIS IS A SAMPLE MATHEMATICA CODE PROGRAM by D .  
              Kirschner*)

(*The first thing we must always do is clear all the variables and parameters*)

Clear[k,  P,  ode1, ode2]


(*Then,  
          we must define the ode we are to solve .  
              The first term on the LHS is only a label (so we can call it later) the ODE is defined by the next two terms .  
              The format is specific.*)
	ode1 =  P'[t] == k P[t];

(*Then,  
          we must define the values for parameters in the equations.*)
		k = 0.50;

(*Next,  
          is the solver call .  
                the LHS again is only a label that we can use later .  
                The solver
is NDSolve and the callstatement has many options .  You can go to onl - 
            line help
to see all the options.*)

sol = 
      NDSolve[{ode1,  P[0. ] == 10.0},  {P[t]},  {t, 0, 10}]

(*This line is the output given by Mathematica after it runs.*)

(*Suppose you want to see the value for the solution at a given point,  
          you can use this command to get it:*)

Pat5 = 
        P[5] /. sol[[1]]

(*Then,  
          I would like to plot the solution to see it on a time plot graph .  
              the LHS is the label, 
the RHS excutes the plot statement .  Notice the use of the ' 
            sol' from the label of the solution to the ODE .  
              Again,  there are many options for the plot command*)

\(p1 = 
        Plot[Evaluate[{P[t]} /. sol], \ {t, 0, 
            10}, 
		PlotRange -> {0, 100}, \ 
          PlotStyle -> {RGBColor[1, 0, 
                0]}, 
		PlotLabel -> "Population size"]\)

(*Suppose you want to take the output and print it to a file that you
can read into EXCEL and manipulate the graph for presentation .  
            Here is how to do that and write to a file in your directory called exp . 
            m .  (it can be anything .   The  . 
                  m
extension reminds me it is from mathematica)*)

tab1 = 
        N[Table[{t, Log[10, P[t]]} /. 
              sol[[1]], {t, 0, 10, 
              1}]];
WriteString["exp.m", 
        StringReplace[
            ToString[
              MatrixForm[
                Map[ToString[CForm[#1]] &, 
                  tab1, {2}]]], "\n\n" -> "\n"] <> "\n\
"];

(*Now,  
          we redo all as above using the logistic equation rather than the exponential
growth equation.*)

\(	\(ode2 = \ \(P'\)[t] == k\ \((1.0 - P[t])\)\ P[t];\)\)
\(		\(k =  .50;\)
\)
\(\(\(sol2\)\(=\)\(NDSolve[{ode2, \ P[0. ] ==  .1}, \ {P[t]}, \ {t, 0, 
          50}]\)\(
\)\)\)

\(\(\(
\)\(p2 = 
      Plot[Evaluate[{P[t]} /. sol2], \ {t, 0, 50}, 
	
        PlotStyle -> {RGBColor[1, 0, 
              0]}, 
		PlotLabel -> "Population size", \ 
        PlotRange -> {0, 1}]\)\)\)

tab2 = 
        N[Table[{t, Log[10, P[t]]} /. 
              sol2[[1]], {t, 0, 50, 
              1}]];
WriteString["log.m", 
        StringReplace[
            ToString[
              MatrixForm[
                Map[ToString[CForm[#1]] &, 
                  tab2, {2}]]], "\n\n" -> "\n"] <> "\n\
"];