(* ::Package:: *)


t0=-4;
Nstep=60000;
stepCompare = {1, 10000, 40000, 60001};
tSegment = {-4, -3, 0, 2};
step = {{1, 10000}, {5000, 45001}, {40000, 60001}};
tstep=Table[t0+(i-1)*0.0001,{i,Nstep+1}];
myPath="/deac/phy/andersonGrp/gaol18/order2code/modifiedM4/lambda1.5/";
file = ToFileName[myPath,"scalefresult4m2i0t4.dat"];
atot = Import[file,{"Data"}];
adata=Transpose[atot];
a1=Transpose[{tstep,adata[[1,1;;Nstep+1]]}];
a2=Transpose[{tstep,adata[[2,1;;Nstep+1]]}];
af1=FindFit[a1,e+b*t+c*t^2+d*t^3+g*t^4+l*t^5+p*t^6+i*t^7+o*t^8+m*t^9+k*t^10+q*t^11+v*t^12+j*t^13+h*t^14,{e,b,c,d,g,l,p,i,o,m,k,q,v,j,h},t];
afit1 = {e,b,c,d,g,l,p,i,o,m,k,q,v,j,h}/.af1;
outputpath=myPath;
Export[outputpath<>"afit4m2i1t4.dat",afit1,"Table"];
ap1f1=FindFit[a2,e+b*t+c*t^2+d*t^3+g*t^4+l*t^5+p*t^6+i*t^7+o*t^8+m*t^9+k*t^10+q*t^11+v*t^12+j*t^13+h*t^14,{e,b,c,d,g,l,p,i,o,m,k,q,v,j,h},t];
ap1fit1 = {e,b,c,d,g,l,p,i,o,m,k,q,v,j,h}/.ap1f1;
Export[outputpath<>"ap1fit4m2i1t4.dat",ap1fit1,"Table"];


tstep = Table[0, {i, Length[step]}];
a3 = Table[0, {i, Length[step]}];
ap2f1 = Table[0, {i, Length[step]}];
ap2fit1 = Table[0, {i, Length[step]}];
For[i = 1, i <= Length[step], i++,
 NstepInitial = step[[i, 1]];
 NstepFinal = step[[i, 2]];
 If[i == 1, 
  tstep[[i]] = 
   Table[t0 + (i - 1)*0.0001, {i, NstepInitial, NstepFinal}];
a3[[i]] = 
 Transpose[{tstep[[i]], adata[[3, NstepInitial ;; NstepFinal]]}],
  tstep[[i]] = 
   Table[t0 + (i - 1)*0.0001, {i, NstepInitial + 1, NstepFinal}];
  a3[[i]] = 
   Transpose[{tstep[[i]], 
     adata[[3, NstepInitial + 1 ;; NstepFinal]]}]];
 ap2f1[[i]] = 
  FindFit[a3[[i]], 
   e + b*t + c*t^2 + d*t^3 + g*t^4 + l*t^5 + p*t^6 + i7*t^7 + o*t^8 + 
    m*t^9 + k*t^10 + q*t^11 + v*t^12 + j*t^13 + h*t^14, {e, b, c, d, 
    g, l, p, i7, o, m, k, q, v, j, h}, t]; 
 ap2fit1[[i]] = {e, b, c, d, g, l, p, i7, o, m, k, q, v, j, h} /. 
   ap2f1[[i]]]
AppendTo[ap2fit1, tSegment];
ap2fit1=Flatten[ap2fit1];
Export[outputpath <> "ap2fit4m2i1t4.dat", ap2fit1, "Table"];
