Fitting data using a series of Sin and Cos

I have the following data representing the normal displacement caused by a normal load applied at the middle cross section of a cylinder:

I've made the data available here.

I am trying to fit using the function:

$$w=\sum_{i=1}^{n} A_{i}\cdot sin(j\theta)+B_{i}\cdot cos(j\theta)$$

where $A_{i}$ and $B_{i}$ are the unknown amplitudes to be found. I am using LeastSquares for this task:

theta = data[[All, 1]];
w = data[[All, 2]];

num = Dimensions[theta][[1]];
n = 5;

m = Table[
Flatten[Table[{Sin[i1*theta[[i2]]], 0}, {i1, 1, n}]] +
Flatten[Table[{0, Cos[i1*theta[[i2]]]}, {i1, 1, n}]], {i2, 1,
num}];
x = LeastSquares[m, w];

f[t_] := Total[Flatten[Table[{Sin[i*t], Cos[i*t]}, {i, 1, n}]].x]
Plot[f[t], {t, -Pi, Pi}, PlotRange -> {{-1, 1}, {0, 0.030}}]


But it is resulting in an empty graphic:

Does anyone know what I am doing wrong?

• Your data is screwy - using exponentiation with e has no meaning to Mathematica. Format it correctly and if you still have problems, update the data link.
– ciao
Mar 8, 2014 at 7:19
• Wouldn't Fourier be better for this than fitting ... ? Sep 12, 2016 at 16:05
• @Saullo Castro Boa pergunta! Estava tendo o mesmo problema. Nos fóruns brasileiros é difícil obter uma resposta e aqui em São José dos Campos quase ninguém mexe com estas coisas... Jun 12, 2017 at 15:49
• @LCarvalho sim... a solução sempre são os forums em inglês Jun 12, 2017 at 16:13

After patching your data and fixing/adjusting code (removed unneeded Total, upped samples):

theta = data[[All, 1]];
w = data[[All, 2]];

num = Dimensions[theta][[1]];
n = 150;

m = Table[
Flatten[Table[{Sin[i1*theta[[i2]]], 0}, {i1, 1, n}]] +
Flatten[Table[{0, Cos[i1*theta[[i2]]]}, {i1, 1, n}]], {i2, 1,
num}];
x = LeastSquares[m, w];

f[t_] := Flatten[Table[{Sin[i*t], Cos[i*t]}, {i, 1, n}]].x
Plot[f[t], {t, -Pi, Pi}, PlotRange -> {{-1, 1}, {0, 0.030}}]


• And to make this fitting success, the index of fitted function should start from $i=0$ :) Mar 8, 2014 at 7:48
• @xzczd thank you for this comment, indeed there are cases where the series must start with $i=0$ Apr 7, 2014 at 7:08