"Import the data file dat_var_pendulum.xls. It contains the experimentally measured data set for a variable 
pendulum. The data comprised of average period of the pedulum (in seconds) as a function of the angle Phi. 
The data was measured for three fixed lengths, namely, L= 16.5cm, 22.1 cm and 28.5 cm. 
See the experimental manual, VARIABLE_G_PENDULUM.PDF";
"This code import the *.xls file into Mathematica so that it could manipulate the raw data without needing 
to use Excel. Graphs and calculations are done by the Mathematica code automatically"
                                 
                                  T(Φ)=2π(Sqrt[(L/(g CosΦ))])

"Basically we wish to plot T vs =\!\(
\*SqrtBox[
FractionBox[\(L\), \(Cos\\ Φ\)]]\), hence determining  the value of g via 
m=\!\(
\*FractionBox[\(2  π\), \(
\*SqrtBox[\(g\)]\(\\ \)\)]\).

"There are three sheets of data in dat_var_pendulum.xls"
"Each sheet contains data measured using different length"
"See the manual of this experiment, VARIABLE_G_PENDULUM.PDF"

SetDirectory[NotebookDirectory[]];
"sheets displays the names of each sheets"
sheets=Import["dat_var_pendulum.xls","Sheets"]

"Now import the raw data in XLSX format from each sheet. Note that the information imported are mixed up with additional informatino. They are not pure, unformatted data"
data1=Import["dat_var_pendulum.xls","XLSX"][[1]];
data2=Import["dat_var_pendulum.xls","XLSX"][[2]];
data3=Import["dat_var_pendulum.xls","XLSX"][[3]];

"Automatically abstract the numerical values of L for each sheet"
length1=ToExpression[StringCases[sheets[[1]],NumberString][[1]]]/100.0
length2=ToExpression[StringCases[sheets[[2]],NumberString][[1]]]/100.0
length3=ToExpression[StringCases[sheets[[3]],NumberString][[1]]]/100.0

"The raw data are not all pure number. Need to selective the pure data, using Take "
data1=Take[data1,{2,-1}];
data2=Take[data2,{2,-1}];
data3=Take[data3,{2,-1}];

"Cast the data into the form {Cos[phi],T}, and then form three different tables. Each set of table will provide an independent determination of the gravity constant. Hence these data sets have to be treated independently"
angle1[i_]:=data1[[i,1]];period1[i_]:=data1[[i,2]];
angle2[i_]:=data2[[i,1]];period2[i_]:=data2[[i,2]];
angle3[i_]:=data3[[i,1]];period3[i_]:=data3[[i,2]];
x1[i_]:=Sqrt[length1/Cos[angle1[i] Degree]];
x2[i_]:=Sqrt[length1/Cos[angle2[i] Degree]];
x3[i_]:=Sqrt[length1/Cos[angle3[i] Degree]];

txperiod1=Table[{x1[i],period1[i]},{i,1,Length[data1]}];
txperiod2=Table[{x2[i],period2[i]},{i,1,Length[data2]}];
txperiod3=Table[{x3[i],period3[i]},{i,1,Length[data3]}];
"ListPlot the three sets of data "
lps=ListPlot[{txperiod1,txperiod2,txperiod3},Mesh->All,PlotRange->{{0,1.2},{0,2}}]

"Use LinearModelFit[data,{1,x},x] to find the best fit parameters to the first data set, which is to fit against the form y = a + b*x "
ff1=LinearModelFit[txperiod1,{1,x},x]

"Use Normal to print out the result of LinearModelFit"
Normal[ff1]

"Plot the linear equation with best fit parameters "
plotff1=Plot[ff1[x],{x,0,5},Frame->True,PlotLabel->Normal[ff1]]

"Plot both raw data and the lienar equation together"
Show[plotff1,ListPlot[txperiod1]]

"Error in the best fit parameters can be printed out "

ff1["ParameterTable"]

"How to print out the parameter a? First take a look at the output Normal[ff1]"
Normal[ff1]

"Check the output of Normal[ff1][[1]]"
Normal[ff1][[1]]

"Check the output of Normal[ff1][[2]],Normal[ff1][[2,1]]"
Normal[ff1][[2]]
Normal[ff1][[2,1]]

"Hence the best fit values for parameters a and b as in y = a + b*x are "
"a = Normal[ff1][[1]]"
a = Normal[ff1][[1]]
"b = Normal[ff1][[2,1]]"
b = Normal[ff1][[2,1]]

"Now, modify the code to print out the best fit parameters for the other two sets of data, txperiod2 and txperiod3"

"Find also the values of constant g from each set of data"
"What are the error in each of these g?"
"Based on these three sets of experimental data, how would you report the value of g measured experimetally?"