Re: Dotted Lines at Discontinuities
- To: mathgroup at smc.vnet.net
- Subject: [mg73364] Re: Dotted Lines at Discontinuities
- From: "dimitris" <dimmechan at yahoo.com>
- Date: Wed, 14 Feb 2007 05:17:40 -0500 (EST)
- References: <eqs8t7$idu$1@smc.vnet.net>
On Feb 13, 1:54 pm, Bob Hanlon <hanl... at cox.net> wrote: > I am not a professor/educator. > > Send your questions to MathGroup, not to me. > > Use a dummy function name temporarily. > > plotDisc[args___]:=Module[{p,temp}, > p=(Plot[args,DisplayFunction->Identity]//. > (Line[{s___,{x1_,y1_},{x2_,y2_},e___}]/; > (Abs[(y2-y1)/(x2-x1)]>10||Sign[y1]!=Sign[y2])):> > Sequence[Line[{s,{x1,y1}}], > AbsoluteDashing[{5,5}],temp[{{x1,y1},{x2,y2}}], > AbsoluteDashing[{}],Line[{{x2,y2},e}]])/.temp->Line; > Show[p,DisplayFunction->$DisplayFunction]]; > > g=UnitStep[x-3]+UnitStep[x-5]-3UnitStep[x-6]; > > plotDisc[g,{x,0,10},PlotStyle->Red]; > > Bob Hanlon > > ---- Mr Ajit Sen <senr... at yahoo.co.uk> wrote: > > > > > Dear Prof Hanlon, > > > I refer to the nice code you posted in Mathgroup > > (Sun 17 Sept 2006), whereby the vertical lines at > > discontinuities are removed: > > > plotDisc[args___] := Module[{p}, > > p = Plot[args, DisplayFunction -> Identity] //. > > (Line[{s___, {x1_, y1_}, {x2_, y2_}, > > e___}]/; > > (Abs[(y2-y1)/(x2-x1)] > 10^3 && > > Sign[y1] != Sign[y2])):> > > Sequence[Line[{s, {x1, y1}}], Line[{{x2, > > y2}, e}]]; > > Show[p, DisplayFunction -> $DisplayFunction]]; > > > My problem is I'd like to have them in, but as > > dotted lines instead. At the moment I'm using your > > code with some slight modifications (e.g. 10 instead > > of 10^3, || instead of && ) and then include the > > dotted lines one by one. Thus for the function > > > g = UnitStep[x - 3] + UnitStep[x - 5] - > > 3UnitStep[x - 6] , > > > I am doing it as follows : > > > p=plotDisc[g,{x,0,10}] > > p1=Graphics[{Red, AbsoluteDashing[{5,10}], > > Line[{{3,0},{3,1}}]}] > > p2=Graphics[{Red, AbsoluteDashing[{5,10}], > > Line[{{5,1},{5,2}}]}] > > p3=Graphics[{Red, AbsoluteDashing[{5,10}], > > Line[{{6,2},{6,-1}}]}] > > > Show[p,p1,p2,p3] > > > This works fine but I think there should be a neater > > way to go about it for arbitrary discontinuous > > functions. I tried to put > > > Graphics[{Red, AbsoluteDashing[{5,10}], > > Line[{{x1,y1},{x1,y2}}]}] > > > before the last line in your code, but that just > > didn't work ! > > > I would be most grateful if you could please help me > > out on this. > > > Thank you very much in advance. > > > Ajit Sen.- Hide quoted text - > > - Show quoted text - Hi Ajit. Here is another solution; not so elegant and smart as Bob Hanlon's solution of course but it does the job and I think (in my point of view) it is more easy to understand. BUT it has the drawback that you MUST specify the discontinuities of the function you want to plot. First I will decompose the code and after I will write down the final one-liner. First note that for example Partition[{a, b, c, d, e}, 2, 1] {{a, b}, {b, c}, {c, d}, {d, e}} Also note the Output of the following commands (Plot[ff[x], {x, #1[[1]], #1[[2]]}, PlotStyle -> Red] &) /@ Partition[{0, 3, 5, 6, 10}, 2, 1]; and Show[Graphics[{Blue, AbsoluteDashing[{3}], (Line[{{#1, Limit[ff[x], x -> #1, Direction -> 1]}, {#1, Limit[ff[x], x -> #1, Direction -> -1]}}] & ) /@ {3, 5, 6}}]]; So a combination of these commands can provide you with the desirable graph. Indeed ff[x_] = UnitStep[x - 3] + UnitStep[x - 5] - 3*UnitStep[x - 6]; (Plot[ff[x], {x, #1[[1]], #1[[2]]}, DisplayFunction -> Identity, PlotStyle -> Red] & ) /@ Partition[{0, 3, 5, 6, 10}, 2, 1]; Graphics[{Blue, AbsoluteDashing[{3}], (Line[{{#1, Limit[ff[x], x -> #1, Direction -> 1]}, {#1, Limit[ff[x], x -> #1, Direction -> -1]}}] & ) /@ {3, 5, 6}}]; Show[%%, %, DisplayFunction -> $DisplayFunction, Axes -> False, Frame - > {True, True, False, False}, PlotRange -> All] It is time to write down the relevant one-liner code: Clear[AsymPlot] AsymPlot[f_, {x_, a_, b_, c___}, {plotopts___}, {lineopts___}] := Show[{(Plot[f, {x, #1[[1]], #1[[2]]}, DisplayFunction -> Identity, plotopts] & ) /@ Partition[{a, c, b}, 2, 1], Graphics[{lineopts, (Line[{{#1, Limit[f, x -> #1, Direction -> 1]}, {#1, Limit[f, x -> #1, Direction -> -1]}}] & ) /@ {c}}]}, DisplayFunction -> $DisplayFunction, Axes -> False, Frame -> {True, True, False, False}, PlotRange -> All] Example of application AsymPlot[(x/10)*Sign[x + 5] + Sign[x + 3] + UnitStep[x - 3] + UnitStep[x - 4] - 3*UnitStep[x - 6], {x, -10, 10, -5, -3, 3, 4, 6}, {PlotStyle -> Red}, {Blue, AbsoluteDashing[{3, 4}]}] Best Regards Dimitris