Prev: ZTransform for a non-causal unstable signal. How to make Mathematica
Next: Setting default zoom factor in notebooks
From: Benjamin Hell on 10 Mar 2010 06:30 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.
From: David Park on 11 Mar 2010 06:33 I'm not certain of the exact underlying mechanics, but basically because of the steep curve as x -> 2 from below, and the piecewise function, Mathematica sees a discontinuity and leaves a gap. The way to overcome this is to use the Exclusions option. s[x_] := Piecewise[{{-Sqrt[2]/2*Sqrt[-x + 0.5] + 2, x < 0.5}, {2, x >= 0.5}}]; Plot[s[x], {x, 0, 1}, Exclusions -> None, Frame -> True, PlotRangePadding -> .1] David Park djmpark(a)comcast.net http://home.comcast.net/~djmpark/ From: Benjamin Hell [mailto:hell(a)exoneon.de] 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.
From: dh on 11 Mar 2010 06:36 use the option: On 10.03.2010 12:30, Benjamin Hell wrote: > at the junctures. An easy example is: > s[x_] := Piecewise[{{-Sqrt[2]/2*Sqrt[-x + 0.5] + 2, x< 0.5}, {2, x>= E-Mail:<mailto:dh(a)metrohm.com> Internet:<http://www.metrohm.com>
From: Patrick Scheibe on 11 Mar 2010 06:36 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. >
From: Matthias Hunstig on 11 Mar 2010 06:38
Hi, > I want to plot a piecewise function, but I don't want any gaps to appear > at the junctures. Try Exclusions->None as an option for Plot. Regards, Matthias |