3
votes

Consider the following definition:

f[x_]=Piecewise[{{0,x<1/2},{Interval[{0,1}],x==1/2},{1,x>1/2}}];

Then when one does the Plot[f[x],{x,0,1}] of the function, the graph does not depict the interval value f[1/2] of the graph.

Any ideas on how to plot interval-valued functions in Mathematica would be much appreciated.

Update #1: I've found a hack:

Plot[ f[x], {x,0,1}, ExclusionsStyle->Opacity[1]];

The hack, however, does not work on a general interval-valued function, such as

f[x_]=Piecewise[{{0,x<1/2},{Interval[{0,1}],1/2<=x<=1}}];

which is the essence of the question.

Update #2:

As a followup to the neat example of @Heike below: it's only a partial solution. For if one tries the following:

f[x_] = Piecewise[{{0, x < 1/2}, {Interval[{x, 1}], 1/2 <= x <= 1}}];
Plot[ {f[x] /. Interval[a_] :> a[[1]], f[x] /. Interval[a_] :> a[[2]]},
{x, 0, 1}, Filling -> {1 -> {2}}]

then the graph depicts a segment at x=1/2 that is equal to the value [0,1] instead of [1/2,1].

1
Hoe do you think an interval should be depicted, actually? - Sjoerd C. de Vries
As a vertical line segment on the graph: since the element (x,f(x)) of the graph of the function at x=1/2 a closed interval on the real line, I would like Mathematica to be able to graph it for me. - Dimitris
@Dimitris: Plot is not capable of plotting multimaps, only injective functions. Also, I think you'd like to write Interval[{0,1}]. As it stands now, this evaluates to Interval[{0,0}, {1,1}] which means the union interval of {0,0} and {1,1}. - bbtrb
thanks @bbtrv ; I meant the interval, as I write it below - fixed it. - Dimitris
Since Mma has built-in interval arithmetic, it's a pity that Plot is not overloaded to actually plot intervals. - Dimitris

1 Answers

3
votes

Maybe you could do something like

f[x_]=Piecewise[{{0,x<1/2},{Interval[{0,1}],1/2<=x<=1}}];

Plot[{f[x] /. Interval[a_] :> a[[1]], 
 f[x] /. Interval[a_] :> a[[2]]}, {x, 0, 1}, Filling -> {1 -> {2}}]

plotting an interval