(* Print ["Good Morning Mr. Babbage, hope you slept well."] *)

stmp = OpenWrite["MA-s-23.txt"]
Write[stmp, Good Morning Mr. OrganGrinder]
Write[stmp, Please note - this example is _not_ pipelined]

endDegree = 22.5  (* starting degree assumed to be 0  *)
(* make computing polynomial, assumed to be 8th degree x^7 *)
x=. (* clear any previous value *)
arrayValues = Table[N[Sin[i*endDegree/7*Pi/180],31], {i,0,7}]
?arrayValues
Print ["printing ?arrayValues"]
Write[stmp, "writing ?arrayValues"]
For [i=1, i<9, i++,
   {  Print [i,"  ", N[i*endDegree/7*PI/180], "  ", N[arrayValues[[i]] ,31] ] ;
      Write[stmp, i ,"  " ,arrayValues[[i]] ]   
    }]
arrayPoly = Table[x^i, {i, 0, 8}]
?arrayPoly
arrayFitPoly = Fit[arrayValues,arrayPoly,x]  (* orig polynomial *)
?arrayFitPoly

minutesStep = 1 (* minutes *)
arrayStepValues = Table[ N[Sin[i*minutesStep/60*Pi/180],31], {i, 0, 7}]
(*  arrayStepValues = {0, 1, 8, 27, 64, 125, 216, 343 }  *)
Print ["printing arrayStepValues"]
Write[stmp, "writing ?arrayStepValues"]
For [i=1, i<9, i++,
   {  Print [i," ",N[i*cycleStep*PI/180]," ",N[arrayStepValues[[i]] ,40] ] ;
      Write[stmp, i ,"  " ,arrayStepValues[[i]] ]   
    }]

(* initialize and do difference array *)
arrayDifferences = Table[N[0,31], {i, 1, 8 }]  (* initialize *)
For [ i=1, i<=8, i++,
   { 
    arrayDifferences[[i]] = arrayStepValues[[1]] ;
    For [ ii=1, ii<=8-i, ii++, 
	{arrayStepValues[[ii]]=arrayStepValues[[ii+1]]-arrayStepValues[[ii]]
	}]
    }]
?arrayDifferences

(* lets crank the Babbage machine a few times *)
stringText = "lets crank the Babbage machine a few times"
Print [stringText] ;
Write [stmp, stringText] ;
degrees = 0;
Print ["endDegree = ", endDegree] ;
For [ n=0, n<1500, n++,
   {
     minutes = Mod[{n * minutesStep}, 60] ;
     degrees = Floor[{n * minutesStep / 60} ] ;
    (* Print [      degrees, " ",minutes, " ", arrayDifferences[[1]] ] ; *)
     Write [stmp, degrees, " ",minutes, " ", arrayDifferences[[1]] ] ;
     (* do reference value *)
     reference = N[ Sin[n*minutesStep/60*Pi/180] , 31] ;
     (*Print ["   reference = ", reference ]; *)
     If [ reference != 0,
	{ 
	  error = reference - arrayDifferences[[1]] ;
  	  percentError = N[error/reference*100, 31] ; 
          (* Print [      "       ", percentError, " %" ] ; *)
           Write [stmp, "       ", percentError, " %"  ] ;
	}] ;
     For [ i=1, i<8, i++,
       {arrayDifferences[[i]]=arrayDifferences[[i]]+arrayDifferences[[i+1]] }
	] ;
    }] ;

Close [stmp]
Print ["done"]