TransWikia.com

Fourier Series plot

Mathematica Asked by user2737248 on June 11, 2021

I’m trying to plot partial sum of Fourier series. I’ve finally managed to make it work but there are several things I don’t understand

f = Piecewise[{{Mod[x, Pi], Mod[x, 2 Pi] < Pi}, {Mod[-x, Pi], Mod[x, 2 Pi] >= Pi}}]
g = FourierSeries[f, x, 2]
Plot[{g, f}, {x, -10, 10}]

enter image description here

My questions are :

  1. Why does it stop working when I directly put the value of g in Plot (only f is plotted) ?

    f = Piecewise[{{Mod[x, Pi], Mod[x, 2 Pi] < Pi}, {Mod[-x, Pi],Mod[x, 2 Pi]>= Pi}}]
    Plot[{FourierSeries[f, x, 2], f}, {x, -10, 10}]
    
  2. Why does it stop working when I define g like this (only f is plotted)?

    g[x_] := FourierSeries[f, x, 2]
    Plot[{g[x], f}, {x, -10, 10}]
    
  3. I also tried to add a slider to set the number terms to add. It seems to work but it makes Mathematica freeze…

    f = Piecewise[{{Mod[x, Pi], Mod[x, 2 Pi] < Pi}, {Mod[-x, Pi],Mod[x, 2 Pi] >= Pi}}]
    Slider[Dynamic[n], {1, 10, 1}]
    Dynamic[g = FourierSeries[f, x, n]]
    Dynamic[Plot[{g, f}, {x, -10, 10}]]
    

Thanks 🙂 !

One Answer

I guess I can put my comment in an answer:

  • Re 1: Because x is set to a numeric value before the series is computed.
  • Re 2: Ditto; use g[x_?NumericQ] := FourierSeries[f, x, 2].
  • Re 3: Totally different. You get into a Dynamic infinite loop because setting g causes another update. Try Dynamic[g = FourierSeries[f, x, n], TrackedSymbols :> {n}].

The first two questions are more or less a duplicate of What are the most common pitfalls awaiting new users?

The third question comes up in so many forms, it's hard to find an exact duplicate. The underlying issue in Can Table be used inside Manipulate? is the same, although it's a rather complicated example compared to this one.

Correct answer by Michael E2 on June 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP