Clear[a,b,c]

(*Question 2c*)

v1=kf*a[t]-kr*b[t]*c[t]
odes={D[a[t],t]==-v1,D[b[t],t]==v1,D[c[t],t]==v1}
pars={kf-> 23,kr-> 0.5}
inits={a[0]==5,b[0]==0,c[0]==0}

eqs=Join[odes,inits]/.pars

vars={a[t],b[t],c[t]};

sol=NDSolve[eqs,vars,{t,0,20}];

Plot[Evaluate[vars/.sol],{t,0,2}]

a[t]/.sol/.t->2

b[t]*c[t]/a[t]/.sol/.t->10

(*Question 2d*)
pars={kf-> 23,kr-> 0.5,a->5};
Keq=kf/kr/.pars;
v1=kf*(a-b[t]*c[t]/Keq);
vars={b[t],c[t]};
init={b[0]==0,c[0]==0};
odes={D[b[t],t]==v1,D[c[t],t]==v1};

eqs=Join[odes,init]/.pars

sol=NDSolve[eqs,vars,{t,0,20}]

massActionRatio=b[t]*c[t]/a;

Plot[c[t]/.sol,{t,0,10},PlotRange->All,AxesOrigin->{0,0},Frame->True,FrameLabel->{"time", "c[t]"}]
Plot[massActionRatio/.sol/.pars,{t,0,10},PlotRange->All,AxesOrigin->{0,0},Frame->True,FrameLabel->{"time","mass action ratio"}]

massActionRatio/.sol/.pars/.t->10

(*Question 3*)

(*Put the csv files in the same folder as your notebook*)

datai=Import[NotebookDirectory[]<>"A.csv"]
dp=ListPlot[datai,Frame->True,FrameLabel->{"A","rate"}]

dataA=datai

v1

Clear[kf,a]

forwardFit=NonlinearModelFit[dataA,kf*a,kf,a]

forwardFit["BestFitParameters"]

forwardFit["ParameterTable"]

({{"", "Estimate", "Standard Error", "t\[Hyphen]Statistic", "P\[Hyphen]Value"}, {kf, 21.558093075742622`, 0.2956470906050922`, 72.91833324529021`, 8.676318079481967`*^-14}})

forwardFit["RSquared"]

dataBC=Import[NotebookDirectory[]<>"BC.csv"]
ListPlot3D[dataBC,AxesLabel->{"B","C","rate"}]

ListPointPlot3D[dataBC,AxesLabel->{"B","C","rate"}]

Clear[kr,b,c]

reverseFit=NonlinearModelFit[dataBC,-kr*b*c,kr,{b,c}]

fitRule=reverseFit["BestFitParameters"]

func=-kr*b*c;
func/.fitRule

reverseFit["ParameterTable"]

reverseFit//Normal

Plot[reverseFit[1,c],{c,0,10}]

(*Fitting the complete expression to all of the data*)

v1=kf*a[t]-kr*b[t]*c[t]

newData1={#[[1]],0.,0.,#[[2]]}&/@dataA;
newData2={0.,#[[1]],#[[2]],#[[3]]}&/@dataBC;

finalData=Join[newData1,newData2];

fullFit=NonlinearModelFit[finalData,v1,{kf,kr},{a[t],b[t],c[t]}]

fullFit["ParameterTable"]

fullFit["BestFitParameters"]