# Fourier Series calculated by Mathematica gives wrong result?

**URL:** <https://community.wolfram.com/t/fourier-series-calculated-by-mathematica-gives-wrong-result/21624>\
**Category:** Questions\
**Tags:** signal-processing, calculus, graphics-and-visualization\
**Created:** [February 18, 2023, 2:11pm UTC](https://community.wolfram.com/t/fourier-series-calculated-by-mathematica-gives-wrong-result/21624 "2023-02-18T14:11:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cornelbejan15](https://avatars.discourse-cdn.com/v4/letter/c/97f17d/32.png) [@cornelbejan15](https://community.wolfram.com/u/cornelbejan15)\
**Post date:** [February 18, 2023, 2:11pm UTC](https://community.wolfram.com/t/fourier-series-calculated-by-mathematica-gives-wrong-result/21624/1 "2023-02-18T14:11:14Z")

</div>

Hello,

Why for the function f1 the Fourier series calculated by Mathematica does not give the same graph as the Fourier series calculated with the sum of sin and cos ([https://en.wikipedia.org/wiki/Fourier\_series](https://en.wikipedia.org/wiki/Fourier_series))?

In the case of function f2, we can see that the two calculation methods give the same results. I don’t understand why in the case of function f1 the two methods do not give the same results.

I saw that Mathematica gives the Fourier series in the exponential form, but the exponential form and the sine and cos form should give the same results, right?

```
f1[x_] := \!\(\*
TagBox[GridBox[{
{"\[Piecewise]", GridBox[{
{"0", 
RowBox[{
RowBox[{"-", "5"}], "<", "x", "<", "0"}]},
{"3", 
RowBox[{"0", "<", "x", "<", "5"}]}
},
AllowedDimensions->{2, Automatic},
Editable->True,
GridBoxAlignment->{"Columns" -> {{Left}}, "Rows" -> {{Baseline}}},
GridBoxItemSize->{"Columns" -> {{Automatic}}, "Rows" -> {{1.}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.84]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}},
Selectable->True]}
},
GridBoxAlignment->{"Columns" -> {{Left}}, "Rows" -> {{Baseline}}},
GridBoxItemSize->{"Columns" -> {{Automatic}}, "Rows" -> {{1.}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.35]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}}],
"Piecewise",
DeleteWithContents->True,
Editable->False,
SelectWithContents->True,
Selectable->False,
StripWrapperBoxes->True]\)
Plot[f1[x], {x, -5, 5}, Exclusions -> None]
T = 10;
A0 = 2/T*\!\(
\*SubsuperscriptBox[\(\[Integral]\), \(-
\*FractionBox[\(T\), \(2\)]\), 
FractionBox[\(T\), \(2\)]]\(f1[x] \[DifferentialD]x\)\) // Simplify
An = 2/T*\!\(
\*SubsuperscriptBox[\(\[Integral]\), \(-
\*FractionBox[\(T\), \(2\)]\), 
FractionBox[\(T\), \(2\)]]\(f1[x]*Cos[
\*FractionBox[\(2*\[Pi]*n*x\), \(T\)]] \[DifferentialD]x\)\) // 
  Simplify
Bn = 2/T*\!\(
\*SubsuperscriptBox[\(\[Integral]\), \(-
\*FractionBox[\(T\), \(2\)]\), 
FractionBox[\(T\), \(2\)]]\(f1[x]*Sin[
\*FractionBox[\(2*\[Pi]*n*x\), \(T\)]] \[DifferentialD]x\)\) // 
  Simplify
s1[k_, x_] := A0/2 + \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(n = 1\), \(k\)]\((An*Cos[
\*FractionBox[\(2*\[Pi]*n*x\), \(T\)]] + Bn*Sin[
\*FractionBox[\(2*\[Pi]*n*x\), \(T\)]])\)\) // Simplify
Testf1 = FourierSeries[f1[x], x, 40];
Plot[{f1[x], s1[40, x], Testf1}, {x, -5, 5}, Exclusions -> None, 
 PlotLegends -> "Expressions", 
 PlotStyle -> {Thickness[0.008], Thickness[0.005], Thickness[0.003]}]
Plot[{s1[40, x], Testf1}, {x, -5, 5}, Exclusions -> None, 
 PlotLegends -> "Expressions", 
 PlotStyle -> {Thickness[0.008], Thickness[0.005], Thickness[0.003]}]

```

 ![63831.JPG](https://global.discourse-cdn.com/wolfram/original/3X/e/b/ebb5a132d589df4136e21d310966b2d606ddb862.jpeg)

 ![65291.JPG](https://global.discourse-cdn.com/wolfram/original/3X/2/a/2ab271e13c4ab70630f378961470215b351f9b6d.jpeg)

 ![39571.JPG](https://global.discourse-cdn.com/wolfram/original/2X/4/4d5f0e2140e67f8fbc99056f7315e2f7968f7b37.jpeg)

```
f2[x_] := \!\(\*
TagBox[GridBox[{
{"\[Piecewise]", GridBox[{
{
RowBox[{"-", "1"}], 
RowBox[{
RowBox[{"-", "\[Pi]"}], "<", "x", "<", "0"}]},
{"1", 
RowBox[{"0", "<", "x", "<", "\[Pi]"}]}
},
AllowedDimensions->{2, Automatic},
Editable->True,
GridBoxAlignment->{"Columns" -> {{Left}}, "Rows" -> {{Baseline}}},
GridBoxItemSize->{"Columns" -> {{Automatic}}, "Rows" -> {{1.}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.84]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}},
Selectable->True]}
},
GridBoxAlignment->{"Columns" -> {{Left}}, "Rows" -> {{Baseline}}},
GridBoxItemSize->{"Columns" -> {{Automatic}}, "Rows" -> {{1.}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.35]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}}],
"Piecewise",
DeleteWithContents->True,
Editable->False,
SelectWithContents->True,
Selectable->False,
StripWrapperBoxes->True]\)
Plot[f2[x], {x, -\[Pi], \[Pi]}, Exclusions -> None]
T = 2*\[Pi];
A0 = 2/T*\!\(
\*SubsuperscriptBox[\(\[Integral]\), \(-
\*FractionBox[\(T\), \(2\)]\), 
FractionBox[\(T\), \(2\)]]\(f2[x] \[DifferentialD]x\)\) // Simplify
An = 2/T*\!\(
\*SubsuperscriptBox[\(\[Integral]\), \(-
\*FractionBox[\(T\), \(2\)]\), 
FractionBox[\(T\), \(2\)]]\(f2[x]*Cos[
\*FractionBox[\(2*\[Pi]*n*x\), \(T\)]] \[DifferentialD]x\)\) // 
  Simplify
Bn = 2/T*\!\(
\*SubsuperscriptBox[\(\[Integral]\), \(-
\*FractionBox[\(T\), \(2\)]\), 
FractionBox[\(T\), \(2\)]]\(f2[x]*Sin[
\*FractionBox[\(2*\[Pi]*n*x\), \(T\)]] \[DifferentialD]x\)\) // 
  Simplify
s2[k_, x_] := A0/2 + \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(n = 1\), \(k\)]\((An*Cos[
\*FractionBox[\(2*\[Pi]*n*x\), \(T\)]] + Bn*Sin[
\*FractionBox[\(2*\[Pi]*n*x\), \(T\)]])\)\) // Simplify
Testf2 = FourierSeries[f2[x], x, 40];
Plot[{f2[x], s2[40, x], Testf2}, {x, -\[Pi], \[Pi]}, 
 Exclusions -> None, PlotLegends -> "Expressions", 
 PlotStyle -> {Thickness[0.008], Thickness[0.005], Thickness[0.003]}]
Plot[{s2[40, x], Testf2}, {x, -\[Pi], \[Pi]}, 
 PlotLegends -> "Expressions", 
 PlotStyle -> {Thickness[0.008], Thickness[0.005], Thickness[0.003]}]

```

 ![43191.JPG](https://global.discourse-cdn.com/wolfram/original/3X/2/c/2cfaf995ae35d5c5b6b738e3d65de42ef3fda1f8.jpeg)

 ![76171.JPG](https://global.discourse-cdn.com/wolfram/original/3X/f/9/f9074f924f736687a032cf7f6f8d3736757536cf.jpeg)

 ![103801.JPG](https://global.discourse-cdn.com/wolfram/original/3X/d/2/d27214013ecee72520e10a6b56ebef02cbf6e88d.jpeg)

Mathematica 13.2 Notebook file attached.

Thank you.

[FourierSeries.nb](https://community.wolfram.com/uploads/short-url/zpRH7OBmAUe5MVBKRfZhYLVnQkc.nb) (501 KB)

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [February 18, 2023, 4:16pm UTC](https://community.wolfram.com/t/fourier-series-calculated-by-mathematica-gives-wrong-result/21624/2 "2023-02-18T16:16:06Z")

</div>

The function `FourierSeries` assumes by default that the interval is between `-Pi` and `Pi`. For other intervals you have to use the option `FourierParameters`.

---

<div class="post-metadata">

**Author:** ![cornelbejan15](https://avatars.discourse-cdn.com/v4/letter/c/97f17d/32.png) [@cornelbejan15](https://community.wolfram.com/u/cornelbejan15)\
**Post date:** [February 18, 2023, 4:26pm UTC](https://community.wolfram.com/t/fourier-series-calculated-by-mathematica-gives-wrong-result/21624/3 "2023-02-18T16:26:17Z")

</div>

Ok. Thank you.

```
Testf1 = 
  FourierSeries[f1[x], x, 40, 
   FourierParameters -> {-(1/5)*\[Pi], 1/5*\[Pi]}];

```

![87401.JPG](https://global.discourse-cdn.com/wolfram/original/1X/a84921111bb156dc665c41912bdcdf3f1d2bfb1e.jpeg)

![75471.JPG](https://global.discourse-cdn.com/wolfram/original/3X/b/5/b53d367505c08d827a524b3b1e62538609c7b18b.jpeg)

 ![32531.JPG](https://global.discourse-cdn.com/wolfram/original/3X/d/8/d8519239baec099c98d04a40f08c3dde82338b38.jpeg)

---

<div class="post-metadata">

**Author:** ![cornelbejan15](https://avatars.discourse-cdn.com/v4/letter/c/97f17d/32.png) [@cornelbejan15](https://community.wolfram.com/u/cornelbejan15)\
**Post date:** [February 18, 2023, 4:46pm UTC](https://community.wolfram.com/t/fourier-series-calculated-by-mathematica-gives-wrong-result/21624/4 "2023-02-18T16:46:36Z")

</div>

Another thing:

 ![24131.JPG](https://global.discourse-cdn.com/wolfram/original/3X/2/f/2f29e63bed9c271a78bb95ece69da3058c62aa0d.jpeg)

 ![20431.JPG](https://global.discourse-cdn.com/wolfram/original/3X/d/9/d9769020b610e94674b1b7cadf917a406996ef4a.jpeg)

When I use subscripts for 0 and n at A0, An, and Bn then it doesn’t calculate… how can I use subscripts at A0, An, and Bn? Is such a thing possible?

 ![58441.JPG](https://global.discourse-cdn.com/wolfram/original/3X/e/0/e05f72b46aedc0b0c425ab09eca94b37c4f592bb.jpeg)

 ![29641.JPG](https://global.discourse-cdn.com/wolfram/original/3X/b/2/b25e631d6b309a56004c04eeee89d65f794f832c.jpeg)

In principle, I think I found out why those sums are not calculated when I use subscripts at those variables. For example:

 ![109481.JPG](https://global.discourse-cdn.com/wolfram/original/2X/5/5a9b9f3c9961728d15115ea3e3386bf57701fd0b.jpeg)

But I’m interested if there is any way to use subscripts for those sums…?

```
f1[x_] := \!\(\*
TagBox[GridBox[{
{"\[Piecewise]", GridBox[{
{"0", 
RowBox[{
RowBox[{"-", "5"}], "<", "x", "<", "0"}]},
{"3", 
RowBox[{"0", "<", "x", "<", "5"}]}
},
AllowedDimensions->{2, Automatic},
Editable->True,
GridBoxAlignment->{"Columns" -> {{Left}}, "Rows" -> {{Baseline}}},
GridBoxItemSize->{"Columns" -> {{Automatic}}, "Rows" -> {{1.}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.84]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}},
Selectable->True]}
},
GridBoxAlignment->{"Columns" -> {{Left}}, "Rows" -> {{Baseline}}},
GridBoxItemSize->{"Columns" -> {{Automatic}}, "Rows" -> {{1.}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.35]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}}],
"Piecewise",
DeleteWithContents->True,
Editable->False,
SelectWithContents->True,
Selectable->False,
StripWrapperBoxes->True]\)
Plot[f1[x], {x, -5, 5}, Exclusions -> None]
T = 10;
Subscript[A, 0] = 2/T*\!\(
\*SubsuperscriptBox[\(\[Integral]\), \(-
\*FractionBox[\(T\), \(2\)]\), 
FractionBox[\(T\), \(2\)]]\(f1[x] \[DifferentialD]x\)\) // Simplify
Subscript[A, n] = 2/T*\!\(
\*SubsuperscriptBox[\(\[Integral]\), \(-
\*FractionBox[\(T\), \(2\)]\), 
FractionBox[\(T\), \(2\)]]\(f1[x]*Cos[
\*FractionBox[\(2*\[Pi]*n*x\), \(T\)]] \[DifferentialD]x\)\) // 
  Simplify
Subscript[B, n] = 2/T*\!\(
\*SubsuperscriptBox[\(\[Integral]\), \(-
\*FractionBox[\(T\), \(2\)]\), 
FractionBox[\(T\), \(2\)]]\(f1[x]*Sin[
\*FractionBox[\(2*\[Pi]*n*x\), \(T\)]] \[DifferentialD]x\)\) // 
  Simplify
s1[k_, x_] := Subscript[A, 0]/2 + \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(n = 1\), \(k\)]\((
\*SubscriptBox[\(A\), \(n\)]*Cos[
\*FractionBox[\(2*\[Pi]*n*x\), \(T\)]] + 
\*SubscriptBox[\(B\), \(n\)]*Sin[
\*FractionBox[\(2*\[Pi]*n*x\), \(T\)]])\)\) // Simplify
Testf1 = 
  FourierSeries[f1[x], x, 40, 
   FourierParameters -> {-(1/5)*\[Pi], 1/5*\[Pi]}];
Plot[{f1[x], s1[40, x]}, {x, -5, 5}, Exclusions -> None, 
 PlotLegends -> "Expressions", 
 PlotStyle -> {Thickness[0.008], Thickness[0.005], Thickness[0.003]}]

```

[Untitled-1.nb](https://community.wolfram.com/uploads/short-url/lw636HaUnRHSa51iXatyfGU9y51.nb) (19.6 KB)

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [February 18, 2023, 5:39pm UTC](https://community.wolfram.com/t/fourier-series-calculated-by-mathematica-gives-wrong-result/21624/5 "2023-02-18T17:39:32Z")

</div>

You have to use patterns: `Subscript[A, n_] =`, just as in function definitions.

---

<div class="post-metadata">

**Author:** ![cornelbejan15](https://avatars.discourse-cdn.com/v4/letter/c/97f17d/32.png) [@cornelbejan15](https://community.wolfram.com/u/cornelbejan15)\
**Post date:** [February 18, 2023, 5:56pm UTC](https://community.wolfram.com/t/fourier-series-calculated-by-mathematica-gives-wrong-result/21624/6 "2023-02-18T17:56:55Z")

</div>

Can you write an example for this line of code?

```
Subscript[A, n] = 2/T*\!\(
\*SubsuperscriptBox[\(\[Integral]\), \(-
\*FractionBox[\(T\), \(2\)]\), 
FractionBox[\(T\), \(2\)]]\(f1[x]*Cos[
\*FractionBox[\(2*\[Pi]*n*x\), \(T\)]] \[DifferentialD]x\)\)

```

![50641.JPG](https://global.discourse-cdn.com/wolfram/original/3X/0/e/0ef9f38bdf23a030f7672849051880d58b9eb00a.jpeg)

Do you mean something like this?

![72081.JPG](https://global.discourse-cdn.com/wolfram/original/3X/b/5/b56296c9d75093023b7e6bec407989e28f0823f9.jpeg)

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [February 18, 2023, 7:41pm UTC](https://community.wolfram.com/t/fourier-series-calculated-by-mathematica-gives-wrong-result/21624/7 "2023-02-18T19:41:00Z")

</div>

The pattern, that is, the underscore:

```
Subscript[A, n_] = 
 2/T Integrate[Exp[x] Cos[2 Pi n x/T], {x, -T/2, T/2}]
Sum[Subscript[A, n], {n, 0, 5}]

```

Without the underscore, the definition is used only with literal `n`.
