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}]
My questions are :
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}]
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}]
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 🙂 !
I guess I can put my comment in an answer:
x
is set to a numeric value before the series is computed.g[x_?NumericQ] := FourierSeries[f, x, 2]
.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
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP