1
$\begingroup$

When running the following I get the plot below. I'm a beginner, can anyone suggest why we see those graphical imperfections in the plot?

ClearAll[P];
F[x_]:=Piecewise[{{1,Mod[x,2Pi]<Pi},{0,Mod[x,2Pi]>Pi}}];
FourierSeries[F[x],x,100];
Plot[%,{x,0,4Pi}] 

Fourier Series of a rectangular signal

$\endgroup$
  • 3
    $\begingroup$ You need to use more PlotPoints. $\endgroup$ – Roman Jul 9 at 9:06
  • $\begingroup$ @Roman: Thanks, it worked fine with PlotPoints->1000. $\endgroup$ – Ang Jul 9 at 9:16
  • $\begingroup$ Hi Ang, welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context. Thanks for your minimal working example and your code in formatted form. By doing all this you help us to help you and likely you will inspire great answers. The site depends on participation, as you receive give back: vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned. $\endgroup$ – rhermans Jul 9 at 9:53
  • $\begingroup$ Thanks for accepting my answer, but I think you were too hasty doing that. While accepting is one of the things to do after your question is answered, we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all timezones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers. $\endgroup$ – rhermans Jul 9 at 9:53
  • 1
    $\begingroup$ What are the graphical imperfections (probably my eyes are missing something)? $\endgroup$ – Daniel Lichtblau Jul 9 at 16:44
4
$\begingroup$

Answer

As pointed out by @Roman, you should increase the default value for PlotPoints. You should also increase the value of MaxRecursion. Play and find a compromise between PlotPoints and MaxRecursion. By increasing these values you will make computation much slower and the plot figure much heavier (if in vector form).

I recommend at least 4 points per period at your highest frequency and a recursion of at least 3.

You can see that the points that come from the recursion are cleverly placed where needed to create a smoother curve, but the ones from plot points are regularly spaced, so irregular curves benefit more from a larger MaxRecursion.

Mathematica graphics

Points in the Mesh grow very fast with the value of MaxRecursion.

Explanation

From the documentation for Plot, which should be the first thing to read if you are experiencing a problem,

Plot initially evaluates $f$ at a number of equally spaced sample points specified by PlotPoints. Then it uses an adaptive algorithm to choose additional sample points, subdividing a given interval at most MaxRecursion times.

Mathematica graphics

Example

ClearAll[P];
F[x_] := Piecewise[{{1, Mod[x, 2 Pi] < Pi}, {0, Mod[x, 2 Pi] > Pi}}];
Plot[
 Evaluate[FourierSeries[F[x], x, 100]]
 , {x, 0, 4 Pi}
 , PlotPoints -> 800
 , MaxRecursion -> 4
 , PlotTheme -> "Scientific"
 ]

Mathematica graphics

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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