3
$\begingroup$

Let's consider the following periodic function

f[x_] := Which[-2 < x < 0, x + 2, 0 < x < 2, 2 - 2 x]

I know that if we want to find the complex Fourier series and its coefficient we write the following commands for 10-terms :

FourierSeries[f[x], x, 10]
FourierCoefficient[f[x],x,10]

I would like now to define the following two functions, so I can see if the same results come out $$f(x)=\sum_{n=-\infty}^{+\infty} c_n e^{in\pi x / l}$$ and $$c_n=\frac{1}{2l}\int_{-l}^{l} e^{-in\pi x / l} f(x) \,dx$$ However, I find it hard to define them. I would appreciate any help. Thank you in advance

$\endgroup$

1 Answer 1

4
$\begingroup$

You have to be very careful with Fourier series definitions used as the default setting assumes the period is $2 \pi$ which is not what you have so you have to map the definition to one with period of $4$ for your function.

For what you have you need the following

f[x_] := Piecewise[{{x + 2, -2 <= x <= 0}, {2 - 2 x, 0 < x <= 2}}] 
T = 4; (*period*)
c[n_Integer, T_?NumericQ /; Positive[T], x_Symbol, f_] := 
 1/T*Integrate[Exp[-I*n*2*Pi/T*x]*f[x], {x, -T/2, T/2}];

fApprox[numberTerms_?Positive, x_Symbol, T_?NumericQ /; Positive[T]] :=
  Module[{n}, Sum[ c[n, T, x, f]*Exp[I*n*2*Pi/T*x], {n, -numberTerms, numberTerms}]]

And now

fApprox[5, x, T]

Mathematica graphics

Compare to Mathematica

FourierSeries[f[x], x, 5, FourierParameters -> {1, 2*Pi/T}]

Mathematica graphics

Notice the use of FourierParameters -> {1, 2*Pi/T} this is in order to adjust the definition to the one you used as there are different conventions used.

enter image description here

I started the the last one above in order to determine what a and b should be for your case.

ps. Also you have missed the minus signed on the exp term there. It should be negative.


More examples

Mathematica graphics

Update

So if I use the following command is wrong? FourierSeries[f[x], x, 5, FourierParameters -> {-2, 2}]

The default $\{a,b\}$ used by Mathematica assumes period $T=2\pi$. Hence assumes the definition $\frac{1}{2\pi}\int_{-\pi}^{\pi}e^{-int}f\left( t\right) dt$. But your function has different $T$. So to fix this, we start with the Mathematica definition for the general $\{a,b\}$ which is \begin{equation} I_{1}=\left\vert \frac{b}{2\pi}\right\vert ^{\frac{a+1}{2}}\int_{-\frac{\pi }{\left\vert b\right\vert }}^{\frac{\pi}{\left\vert b\right\vert }} e^{-ibnt}f\left( t\right) dt\tag{1} \end{equation} And given the definition that you want to use, which is \begin{equation} I_{2}=\frac{1}{T}\int_{-\frac{T}{2}}^{\frac{T}{2}}e^{-i\left( \frac{2n\pi} {T}\right) t}f\left( t\right) dt\tag{2} \end{equation} Where in the above $T$ is the period of $f\left( t\right) $. We start by finding $b$ from the equation $$ \frac{\pi}{\left\vert b\right\vert }=\frac{T}{2} $$ Hence $$ b=\frac{2\pi}{T} $$ So now (1) becomes \begin{align} I_{1} & =\left\vert \frac{\frac{2\pi}{T}}{2\pi}\right\vert ^{\frac{a+1}{2} }\int_{-\frac{2\pi}{T}}^{\frac{2\pi}{T}}e^{-i\left( \frac{2\pi}{T}\right) nt}f\left( t\right) dt\nonumber\\ & =\frac{1}{T}^{\frac{a+1}{2}}\int_{-\frac{2\pi}{T}}^{\frac{2\pi}{T} }e^{-i\left( \frac{2\pi}{T}\right) nt}f\left( t\right) dt\tag{3} \end{align} What is left is to find $a$. We want $\frac{a+1}{2}=1$. Hence $a=1$. Therefore $$ \{a,b\}=\left\{ 1,\frac{2\pi}{T}\right\} $$ For specific $T=4$ in your case, this becomes $$ \{a,b\}=\left\{ 1,\frac{\pi}{2}\right\} $$ But it is better to use $\left\{ 1,\frac{2\pi}{T}\right\} $ as it is more general. This means when we use FourierParamters->$\left\{ 1,\frac{2\pi}{T}\right\} $ then (1) becomes (2) automatically which is what we want. If you had used FourierParamters-> $\left\{ -2,2\right\} $ then (1) would become \begin{align*} I_{1} & =\left\vert \frac{2}{2\pi}\right\vert ^{\frac{-2+1}{2}}\int _{-\frac{\pi}{\left\vert 2\right\vert }}^{\frac{\pi}{\left\vert 2\right\vert }}e^{-i2nt}f\left( t\right) dt\\ & =\left\vert \frac{1}{\pi}\right\vert ^{\frac{-1}{2}}\int_{-\frac{\pi}{2} }^{\frac{\pi}{2}}e^{-i2nt}f\left( t\right) dt\\ & =\sqrt{\pi}\int_{-\frac{\pi}{2}}^{\frac{\pi}{2}}e^{-i2nt}f\left( t\right) dt \end{align*} Which is not the same as (2).

The bottom line, if you just use FourierParamters->$\left\{ 1,\frac{2\pi} {T}\right\} $ where $T$ is the period, then you do not need to worry about it anymore. Do not use the default setting. Hopefully this makes it more clear.

$\endgroup$
8
  • $\begingroup$ How could I choose the right FourierParameters for every problem? Why didn't we choose the interval $[-2,2]$? $\endgroup$ Jan 21, 2023 at 20:18
  • $\begingroup$ @AthanasiosParaskevopoulos I did use -2..2 (Period is T, and used -T/2 .. T/2) which is the same but more clear. You need use the correct FourierParameters for your problem as there are different definitions. The default one might not be what you want, that is why you need to be careful. See the above help page, $\endgroup$
    – Nasser
    Jan 21, 2023 at 20:20
  • $\begingroup$ So if I use the following command is wrong? FourierSeries[f[x], x, 5, FourierParameters -> {-2, 2}]. I am trying to understand it but I feel that I miss something. $\endgroup$ Jan 21, 2023 at 20:44
  • $\begingroup$ @AthanasiosParaskevopoulos no, you do not want to use {-2,2}. The FourierParameters are not the limits of integration. I updated the answer. $\endgroup$
    – Nasser
    Jan 21, 2023 at 21:10
  • 1
    $\begingroup$ @AthanasiosParaskevopoulos It was a last second fix on my side and forgot to update the code in the answer. It should be numberTerms_?Positive and not numberTerms_Positive. Here is screen shot !Mathematica graphics will update the code now. $\endgroup$
    – Nasser
    Jan 22, 2023 at 0:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.