3
$\begingroup$

how could i change This Plot This Plot

to something like this plot this plot

each two plots are concatenation of HeavisidePi

The code for the first plot is :

    listID1={2,0,9,4,3,9,6,7,8};
tCje[l_]:=Table[l[[n]]*HeavisidePi[x-(n+1)],{n,1,9}];
Plot[tCje[listID1],{x,0,20},Exclusions->None]
$\endgroup$
2
  • 3
    $\begingroup$ Please edit to add cross-references between this and the corresponding Wolfram Community posts. It's easier to avoid duplication of effort among respondents when those are shown. $\endgroup$ Commented May 12, 2020 at 20:34
  • $\begingroup$ Up to the documentation, HeavisidePi is a distribution ("HeavisidePi[x] is equivalent to HeavisideTheta[-x2]"), not a function. In view of it, any plot is misleading here. $\endgroup$
    – user64494
    Commented Sep 23, 2023 at 18:16

3 Answers 3

5
$\begingroup$
ListLinePlot[listID1, InterpolationOrder -> 0]

enter image description here

$\endgroup$
3
$\begingroup$

EDIT:

Thanks to J.M. for suggesting a much better way of generating the function. This yields the same plot as below:

plt = Differences[ArrayPad[listID1, 1]].UnitStep[x - Range[1 + 1/2, 10 + 1/2]];
Plot[plt, {x, 0, 20}, Exclusions -> None]

ORIGINAL:

If you turn it into a piecewise function, it should work.

pcw = Piecewise[
  MapThread[{#1, #2} &, {tCje[listID1], 
    Join[{x <= 2.5}, 
     Table[i < x <= i + 1, {i, 2.5, 8.5, 1}], {9.5 < x}]}]]
Plot[pcw, {x, 0, 20}, Exclusions -> None]

enter image description here

I make the first and last conditions of the piecewise to be special so that it plots over any range you want.

$\endgroup$
3
  • 2
    $\begingroup$ Here's another way to generate the function: Differences[ArrayPad[listID1, 1]].UnitStep[x - Range[1 + 1/2, 10 + 1/2]]. $\endgroup$ Commented May 13, 2020 at 0:55
  • $\begingroup$ @J.M. Oh, I like that way better, thanks! $\endgroup$
    – MassDefect
    Commented May 13, 2020 at 7:43
  • 1
    $\begingroup$ Glad you like it, it's my preferred form as well. Of course, that version naturally maps to a Piecewise[] version, if that is the preferred format: FullSimplify[Piecewise[Append[Transpose[{listID1, #1 <= x < #2 & @@@ Partition[Range[1 + 1/2, 10 + 1/2], 2, 1]}], {0, True}]] == Differences[ArrayPad[listID1, 1]].UnitStep[x - Range[1 + 1/2, 10 + 1/2]], x ∈ Reals] $\endgroup$ Commented May 13, 2020 at 9:38
2
$\begingroup$

Using ListStepPlot:

listID1 = {2, 0, 9, 4, 3, 9, 6, 7, 8};
data = Join[{0}, listID1]
ListStepPlot[data, "Center"]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.