1
$\begingroup$

First off, I have created the following Fourier series for the below equation/condition:

$$f(t) = \begin{cases} 0, & 0<t<1 \\ 1, & 1<t<2 \end{cases} $$

My Fourier Equation for the Above $f(t)$:

$$f(t) = \frac{1}{2} + \sum_{n=1}^{\infty} \frac{sin(2 \pi n)}{\pi n} cos(n \pi t) + \sum_{n=1}^{\infty} \frac{(-1)^n - cos(2\pi n)}{\pi n} sin(n \pi t)$$

In an earlier post (i.e. Limiting the Domain of a Function), I have asked the community how can I properly plot the $f(t)$ piecewise functions with the given constraints. And, the community here recommended using piecewise to solve the problem. While that worked great, I have a hard time adding any additional argument(s) to the piecewise command.

Beyond that, trying to plot the Fourier series doesn't seem to be working quite well when the plot does not show anything. Below is my code:

f[n_]=( 1/2) + (((sin(2*n*pi))/(pi*n))*(cos(n*pi*t)))+((((-1)^n - cos(2*pi*n))/(pi*n))*(sin(n*pi*t)))

f[t_, Nmax_]:= Sum[f[n], {n, 1, Nmax}]

Plot[{f[t, 2]}, [t, 0, 2.5}]

In short, my problem is as follows: 1. I can't find a way to graph my Fourier Series and $f(t)$ piecewise functions on the same graph. 2. I can't seem to plot out the Fourier Series for some unknown reason.

Any help would be greatly appreciated. Thank you!

$\endgroup$

2 Answers 2

2
$\begingroup$

enter image description here

fApprox[max_, t_] := (1/2) + 
  Sum[ Sin[2 n Pi]/(Pi n)  Cos[
      n Pi t] + ((-1)^n - Cos[2 Pi n])/(Pi n) Sin[n Pi t], {n, 1, max}]
f[t_] := Piecewise[{{0, 0 < t < 1}, {1, 1 < t < 2}}];
Manipulate[
 Plot[{f[t], fApprox[nTerms, t]}, {t, 0, 2},
  PlotRange -> {Automatic, {-0.3, 1.3}},
  PlotStyle -> {{Thick, Blue}, Red},
  Exclusions -> None
  ],
 {{nTerms, 5, "How many terms?"}, 1, 30, 1, Appearance -> "Labeled"},
 TrackedSymbols :> {nTerms}
 ]

Notice the Gibbs effect on where $f(x)$ is discontinuous. There is 9% overshoot at each side which can't be reduced no matter how large the number of terms is.


To plot the periodic extended version:

enter image description here

fApprox[max_, t_] := (1/2) + 
  Sum[ Sin[2 n Pi]/(Pi n)  Cos[
      n Pi t] + ((-1)^n - Cos[2 Pi n])/(Pi n) Sin[n Pi t], {n, 1, max}]
f[t_] := Piecewise[{{0, 0 < t < 1}, {1, 1 < t < 2}}];
fExtended[t_] := If[t < 0 || t > 2, f[Mod[t, 2]], f[t]]

Manipulate[
 Plot[{fExtended[t], fApprox[nTerms, t]}, {t, -4, 4},
  PlotRange -> {Automatic, {-0.3, 1.3}},
  PlotStyle -> {{Thick, Blue}, Red},
  Exclusions -> None
  ],
 {{nTerms, 5, "How many terms?"}, 1, 30, 1, Appearance -> "Labeled"},
 TrackedSymbols :> {nTerms}
 ]

For more cool Fourier series animations, all done using Mathematica, I found this web page (for some reason the Mathematica source code used for those is not shown at this time).

Mathematica is probably the best software for making such animations.

$\endgroup$
1
  • $\begingroup$ Thank you so much for the help. This answer was incredibly insightful. I didn't know Mathematica was this powerful and has such an amazing animation tool. I'll be sure to play around with the software more. :) $\endgroup$
    – Athenian
    Apr 17, 2020 at 20:40
1
$\begingroup$

First off, the code you posted won't even parse properly in Mathematica. Second, built-in Mathematica functions are capitalized and the function pattern in general uses brackets [ and ] instead of parenthesis, so use Sin[x] instead of sin(x). Similarly, $\pi$ is implemented as Pi, not pi.

Then, looking at the Fourier series, your implementation is also incorrect. The fraction $\frac{1}{2}$ is outside the series, so you don't want that in the summand function. Also, in my opinion, having a separate summand function is unnecessary in this case, I think it's cleaner to just implement the series directly.

As for how to get the Fourier series and $f(t)$ in the same graph, use the pattern Plot[{f, g}, ...]. All in all, here's the code I'd write (assuming you want to hand-write the Fourier series instead of using the built-in FourierSeries):

fourier[t_, Nmax_] := 
 1/2 + Sum[Sin[2 π n]/(π n) Cos[n π t], {n, 1, Nmax}] + 
  Sum[((-1)^n - Cos[2 π n])/(π n) Sin[n π t], {n, 1, Nmax}]
f[t_] := Piecewise[{{0, 0 < t < 1}, {1, 1 < t < 2}}]
Plot[{f[t], fourier[t, 10]}, {t, 0, 2.5}]

Note: it'd be better to use the linearity of sums to combine the two Sums to a single one, but I kept them separate for clarity.

Plot

You seem to be shaky on the basic syntax, so I'd suggest reading a getting started guide such as this or this. This way you can get to know the language more quickly and better identify the areas where you need advice.

$\endgroup$
1
  • $\begingroup$ Thank you for the answer. Clearly, as you stated, I should probably go back to the basics and rebuild my fundamental understanding on the commands and syntax of Mathematica. Once again, thanks for the help and I'll be sure to go over the link you've provided. $\endgroup$
    – Athenian
    Apr 17, 2020 at 20:43

Your Answer

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

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