Qiang Li
Qiang Li

Reputation: 10855

misleading plot issue in mathematica

I want to study some "strange" functions by plotting them out in mathematica. One example is the following:

mod2[x_] := Which[Mod[x, 2] >= 1, -2 + Mod[x, 2], True, Mod[x, 2]];
f[x_] := Which[-1 <= x <= 1, Abs[x], True, Abs[mod2[x]]];
fn[x_, n_] := Sum[(3/4)^i*f[4^n*x], {i, 0, n}]
Plot[{fn[x, 0], fn[x, 1], fn[x, 2], fn[x, 5]}, {x, -2, 2}]

However, the plot I got from mma is misleading, in the sense that the maxima and minima of fn[x, 5] should be on the same two levels. But due to high oscillation of the function, and the fact that clearly mma only takes limited number of points to draw the function, you see the plot exhibit strange behavior. Is there any option in plot to remedy for this?

plot of fn

Many thanks.

Upvotes: 3

Views: 324

Answers (1)

Brett Champion
Brett Champion

Reputation: 8577

You need to increase the setting for PlotPoints quite a bit to get a 'good' result.

Plot[Evaluate[
  Reverse[{fn[x, 0], fn[x, 1], fn[x, 2], fn[x, 5]}]], {x, -2, 2}, 
 PlotPoints -> 4000]

(I also reversed the order of the functions, so as to be able to see all the curves.) enter image description here

Upvotes: 9

Related Questions