# Complex Fourier method for a function

I am trying to calculate the complex fourier series of $$f(x)=\cosh x$$ for $$-\pi . I tried

f[x_] = If[\pi>x > -pi, Cosh[x], 0];
a[n_] := (2/L)*Integrate[f[x]*Cos[2 n*Pi*x/L], {x, -L/2, L/2}]
a = (1/L)*Integrate[f[x], {x, -L/2, L/2}]
b[n_] := (2/L)*Integrate[f[x]*Sin[2 n*Pi*x/L], {x, -L/2, L/2}]
F[x_, N_] :=
a[o] + Sum[a[n]*Cos[2 n*Pi*x/L] + b[n]*Sin[2 n*Pi*x/L], {n, 1, N}]
p[N_, a_] :=
Plot[Evaluate[F[x, N]], {x, -a, a}, PlotRange -> All,
PlotPoints -> 200]
L = 2Pi;
a[n]
a
b[n]
p[20, 1]




but I cannot the method for the complex Fourier method. Any help?

• Your approach here does not take advantage of the built-in FourierSeries. Since your previous, almost identical question from earlier today received an answer, can you specify why the method proposed in that answer does not work for you here? – MarcoB Jan 16 '20 at 19:20

It looks like you do not want to use FouierSeries ? Because you asked similar question before. But if you want to do it yourself, then you can just use the definition. (You say you want complex F.S. but you are using the standard F.S. in your question).

$$f\left( x\right) \sim\sum_{n=-\infty}^{\infty}c_{n}e^{in\frac{2\pi}{T}x}$$

Where $$T$$ is the period of $$f\left( x\right)$$. And $$c_{n}=\frac{1}{T} \int_{-\frac{T}{2}}^{\frac{T}{2}}f\left( x\right) e^{-in\frac{2\pi}{T}x} \,dx$$

Therefore:

ClearAll[T, x, n, c];
T = 2 Pi; (*period*)
f[x_] := Piecewise[{{Cosh[x], -T/2 < x < T/2}, {0, True}}];
c[n_, T_] := 1/T*Integrate[f[x] Exp[-I 2 n Pi/T x], {x, -T/2, T/2}]
series[nTerms_, T_] := Sum[ c[n, T]* Exp[I 2 n Pi/T x], {n, -nTerms, nTerms}]


And now

series[3,T] (*from -3..3 in the sum*) Verify

 FourierSeries[f[x], x, 3] To plot:

   Plot[{Cosh[x], Evaluate@series[3, T]}, {x, -Pi, Pi}]  Manipulate[
Plot[{Cosh[x], Evaluate@series[nTerms, T]}, {x, -Pi, Pi},
ImageSize -> 400, GridLines -> Automatic,
GridLinesStyle -> LightGray, PlotStyle -> {Blue, Red}],
{{nTerms, 1, "How many terms?"}, 1, 10, 1, Appearance -> "Labeled"},
ContinuousAction -> False,
TrackedSymbols :> {nTerms}
]
`