Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

gaps in plot of piecewise function

704 views
Skip to first unread message

Benjamin Hell

unread,
Mar 10, 2010, 6:30:37 AM3/10/10
to
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.

David Park

unread,
Mar 11, 2010, 6:33:55 AM3/11/10
to
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
djm...@comcast.net
http://home.comcast.net/~djmpark/

dh

unread,
Mar 11, 2010, 6:36:22 AM3/11/10
to

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:d...@metrohm.com>

Internet:<http://www.metrohm.com>

Patrick Scheibe

unread,
Mar 11, 2010, 6:36:55 AM3/11/10
to
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 >=

Matthias Hunstig

unread,
Mar 11, 2010, 6:38:13 AM3/11/10
to
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

Sjoerd C. de Vries

unread,
Mar 11, 2010, 6:43:33 AM3/11/10
to
Hi Benjamin,

You could increase MaxRecursion and/or MaxPoints

Plot[s[x], {x, 0, 1},MaxRecursion -> 15]

Cheers -- Sjoerd

Peter Pein

unread,
Mar 11, 2010, 6:43:44 AM3/11/10
to

Try:

Plot[s[x], {x, 0, 1}, Exclusions -> None]

hth


Raffy

unread,
Mar 11, 2010, 6:43:55 AM3/11/10
to

Plot[..., MaxRecursion -> 15]

gekko

unread,
Mar 11, 2010, 6:44:06 AM3/11/10
to

It looks like the point x == 0.5 is being picked up as a possible
discontinuity and so the plot does not attempt to join at this point.
You can disable this using the Exclusions option:

Plot[s[x], {x,0,1}, Exclusions->None]

Cheers, P.

dh

unread,
Mar 11, 2010, 7:47:57 AM3/11/10
to
Hi Beata,
you may e.g. use Reduce like:

lines = t /. {Reduce[{deriv2func == 0, Element[t, Reals]}, t] //
ToRules};
lines = Line[{{ #, -0.2}, {#, 0.2}}] & /@ lines;
Plot[{deriv2func}, {t, First[First[date]], First[Last[date]]},
Frame -> True, GridLines -> Automatic, Epilog -> {Red, lines}]

Daniel


--

Daniel Huber
Metrohm Ltd.
Oberdorfstr. 68
CH-9100 Herisau
Tel. +41 71 353 8585, Fax +41 71 353 8907

Sjoerd C. de Vries

unread,
Mar 12, 2010, 7:11:28 AM3/12/10
to
The latter functions are equal for all x. Doesn't hold for the former two.

If you increase PlotPoints to well over a couple of hundred with
MaxRecursion at 15 I don't see a gap.

Cheers -- Sjoerd

> -----Original Message-----
> From: Patrick Scheibe [mailto:psch...@trm.uni-leipzig.de]
> Sent: 11 March 2010 14:23
> To: David Park; Benjamin Hell; Sjoerd C. de Vries; Peter Pein; gekko;
> Matthias Hunstig
> Cc: math...@smc.vnet.net
> Subject: Re: Re: gaps in plot of piecewise
> function
>
> Hi again,
>
> I hope everyone saw now that Exclusions->None or using not Piecewise
> but
> e.g. Which will do the trick. In the documentation it sounds to me that
> many functions are generally connected to Piecewise (look at Properties
> and Relations in the Piecewise doc).
>
> My question is, why would it be wrong to connect the plot in Piecewise
> when the Limits are the same? Following example:
>
> Manipulate[
> Plot[Piecewise[{{Exp[1] x, x < 1}}, Exp[x]], {x, 0, 3},


> MaxRecursion -> mr, MeshStyle -> {Red, PointSize[0.005]},
> Mesh -> All, PlotPoints -> pp,

> ImageSize -> 500], {{pp, 5, "PlotPoints"}, 3, 30,


> 1}, {{mr, 1, "MaxRecursion"}, 1, 10, 1}]
>

> The function has the same limit at x->1 and the same derivative. I
> would
> clearly expect a plot without a gap even without the Exclusions
> options.
> Where am I wrong?
>
> Is it too unpredictable to check at least numerically the limits?
> But why is this working?
>
> Manipulate[
> Plot[Piecewise[{{Sin[x], x < 1.334}}, Cos[ x - Pi/2]], {x, 0, 3},


> MaxRecursion -> mr, MeshStyle -> {Red, PointSize[0.005]},
> Mesh -> All, PlotPoints -> pp,

> ImageSize -> 500], {{pp, 5, "PlotPoints"}, 3, 30,


> 1}, {{mr, 1, "MaxRecursion"}, 1, 10, 1}]
>

> What bothers me is that when using PiecewiseExpand you get an
> equivalent
> presentation of one and the same function but you get different plots
> in
> an, say not really predictable way.
>
> Cheers
> Patrick


>
> On Thu, 2010-03-11 at 06:34 -0500, David Park wrote:
> > 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
> > djm...@comcast.net
> > http://home.comcast.net/~djmpark/
> >
> >
> > From: Benjamin Hell [mailto:he...@exoneon.de]
> >

Patrick Scheibe

unread,
Mar 12, 2010, 7:13:04 AM3/12/10
to

Patrick Scheibe

unread,
Mar 13, 2010, 7:55:02 AM3/13/10
to
Hi,

> The latter functions are equal for all x. Doesn't hold for the former two.

yep, but

Sin[x] == (I/2)/E^(I*x) - (I/2)*E^(I*x) // Simplify

Manipulate[
Plot[Piecewise[{{Sin[x], x < 1.334}}, (I/2)/E^(I*x) - (I/2)*
E^(I*x)], {x, 0, 3}, MaxRecursion -> mr,

MeshStyle -> {Red, PointSize[0.005]}, Mesh -> All, PlotPoints -> pp,
ImageSize -> 500], {{pp, 5, "PlotPoints"}, 3, 30,
1}, {{mr, 1, "MaxRecursion"}, 1, 10, 1}]

> If you increase PlotPoints to well over a couple of hundred with


> MaxRecursion at 15 I don't see a gap.

"See" doesn't mean it's not there. Please set PlotPoints to 200 and
MaxRecursion to 15 and check the zoomed result

Manipulate[
Plot[Piecewise[{{Exp[1] x, x < 1}}, Exp[x]], {x, 0, 3},
MaxRecursion -> mr, MeshStyle -> {Red, PointSize[0.005]},

Mesh -> All, PlotPoints -> pp, ImageSize -> 500,
PlotRange -> {{1 - zoom, 1 + zoom}, Automatic}], {{pp, 5,
"PlotPoints"}, 3, 200, 1}, {{mr, 1, "MaxRecursion"}, 1, 15, 1},
{{zoom, 1}, 1, 0}]

Cheers
Patrick

> Cheers -- Sjoerd
>
> > -----Original Message-----
> > From: Patrick Scheibe [mailto:psch...@trm.uni-leipzig.de]
> > Sent: 11 March 2010 14:23
> > To: David Park; Benjamin Hell; Sjoerd C. de Vries; Peter Pein; gekko;
> > Matthias Hunstig
> > Cc: math...@smc.vnet.net
> > Subject: Re: Re: gaps in plot of piecewise
> > function
> >

0 new messages