# Graphical imperfections when plotting a fourier series

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}]


• You need to use more PlotPoints. – Roman Jul 9 at 9:06
• @Roman: Thanks, it worked fine with PlotPoints->1000. – Ang Jul 9 at 9:16
• 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. – rhermans Jul 9 at 9:53
• 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. – rhermans Jul 9 at 9:53
• What are the graphical imperfections (probably my eyes are missing something)? – Daniel Lichtblau Jul 9 at 16:44

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.

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.

## 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"
]