Re: gaps in plot of piecewise function
- To: mathgroup at smc.vnet.net
- Subject: [mg108234] Re: [mg108217] gaps in plot of piecewise function
- From: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>
- Date: Thu, 11 Mar 2010 06:36:50 -0500 (EST)
- References: <201003101130.GAA10324@smc.vnet.net>
Hi, short answer: use Which instead of Piecewise for plotting. Long answer: I assume it's a hack which should provide that piecewise defined functions are not connected since in cases of step-functions it is usually wanted that plots are not connected: step = Which[x < 0.5, 1, 0.5 < x < 1, 0.5, True, 0] step2 = PiecewiseExpand[step] Plot[#, {x, 0, 2}] & /@ {step, step2} If you want to know a bit more detailed what happens in you example you could compare the two plots with different settings for PlotPoints and MaxRecursion: s[x_] := Piecewise[{{-Sqrt[2]/2*Sqrt[-x + 1/2] + 2, x < 1/2}}, 2]; s2[x_] := Which[x < 1/2, -Sqrt[2]/2*Sqrt[-x + 1/2] + 2, True, 2]; Column[Manipulate[ Plot[#, {x, 0, 1}, MaxRecursion -> mr, MeshStyle -> {Red, PointSize[0.005]}, Mesh -> All, PlotPoints -> pp, ImageSize -> 500], {{pp, 3, "PlotPoints"}, 3, 30, 1}, {{mr, 1, "MaxRecursion"}, 1, 10, 1} ] & /@ {s[x], s2[x]}] If you look really closely you see that the Piecewise-stuff gets always disconnected, no matter how many plotpoints you use. In real-life you just don't see that there is a gap when you have enough plotpoints and a moderate setting for maxrecursion. Cheers Patrick On Wed, 2010-03-10 at 06:30 -0500, Benjamin Hell wrote: > Hi, > I want to plot a piecewise function, but I don't want any gaps to appear > at the junctures. An easy example is: > > s[x_] := Piecewise[{{-Sqrt[2]/2*Sqrt[-x + 0.5] + 2, x < 0.5}, {2, x >= > 0.5}}]; > Plot[s[x], {x, 0, 1}] > > It should be clear, that the piecewise function defined above is > continuous, even at x=0.5. So there should not be any gaps appearing in > the plot, but they do. Maybe it's a feature of mathematica, but > nevertheless I want to get rid of the gaps. Any suggestions on how to > achieve that. > > > Thanks in advance. >
- References:
- gaps in plot of piecewise function
- From: Benjamin Hell <hell@exoneon.de>
- gaps in plot of piecewise function