I am trying to evaluate Fourier series expansion of flow rate as a function of crank angle in a duplex positive displacement pump. I have evaluated Fourier series coefficients for the function. However the Fourier series expansion does not match the parent function even if I sum the series to a large number of terms. I would appreciate any help in identifying what I could be doing wrong.
Clear[fsum, fourier, n, nmax, coeff];
f = 5; n =.; t =.; \[Omega] = 2 \[Pi] f; r = 1.; L =
5 r; \[Theta] = \[Omega] t;
Clear[P1, P2, coeff, a, b];
P1[\[Theta]_] := -r Sin[\[Theta]] - (r^2 Sin[\[Theta]] Cos[\[Theta]])/
Sqrt[L^2 - r^2 (Sin[\[Theta]])^2];
P2[\[Theta]_] := P1[\[Theta] + Pi];
fsum[\[Theta]_] := Max[P1[\[Theta]], 0] + Max[P2[\[Theta]], 0];
coeff[n_Integer] :=
coeff[n] = (1/\[Pi]) Quiet@
NIntegrate[
fsum[\[Theta]] Exp[-I n \[Theta]], {\[Theta], -\[Pi], -3/
4 \[Pi], -\[Pi]/2, -1/4 \[Pi], 0, 1/4 \[Pi], 1/2 \[Pi],
3/4 \[Pi], \[Pi]}, AccuracyGoal -> 8];
a[n_] = Re@coeff[n];
b[n_] = Im@coeff[n];
Clear[fourier, ul];
ul = 1000;
fourier[n_] :=
a[0] /2 +
Sum[Re@coeff[n] Cos[n \[Theta]] + Im@coeff[n] Sin[n \[Theta]], {n,
1, ul}];
Plot[{fsum[\[Theta]], fourier[n]}, {\[Theta], 0, 2 \[Pi]},
Frame -> True, Axes -> True, PlotRange -> { {0, 2 \[Pi]}, {0, 1.5}},
FrameLabel -> {"Crank Angle (rad)", "Flow rate"},
GridLines -> Automatic, AspectRatio -> .65,
PlotStyle -> {{Red, Thickness[0.005]}, {Green,
Thickness[0.0051]}, {Gray, Thickness[0.01]}},
BaseStyle -> {FontWeight -> "Bold", FontSize -> 15,
FontFamily -> "Arial"}]
Attachments: