1
$\begingroup$

I try to plot a step-wise function from a series of points, and I have the commands:

pacificpoints = {{-5, 2}, {-4.8, 6}, {-4.5, 3}, {-4.2, 14.5}, {-4, 
    5}, {-3.8, 8}, {-3.5, 5}, {-3.2, 12}, {-3.1, 10.5}, {-3, 
    13}, {-2.2, 2}, {-1.9, 11}, {-1.7, 5}, {-1.3, 9.8}, {-0.8, 2}, {0,
     25.5}, {0.4, 1}, {0.6, 9}, {1.2, 6}, {1.5, 7.5}, {1.9, 
    1.8}, {2.2, 7}, {2.5, 4}, {3, 13}, {3.5, 5.5}, {3.8, 10}, {4.2, 
    4}, {4.5, 8}, {4.8, 5.5}, {5, 1}};
Remove[pacificfun]

The step-wise function is then prepared as:

pacificfun[x_] := 
  Piecewise[
   Table[{pacificpoints[[i, 2]], pacific[[i, 1]] > x}, {i, 
     Length[pacificpoints]}], 0];
Plot[pacificpoints[x], {x, -5, 5}, PlotRange -> Full]

But I get only a blank plot, while it works on another quite similar point-set:

alwynpoints = {{-5, 0}, {-4.5, 1.5}, {-4, 0}, {-3.8, -2}, {-3.2, 
    0}, {-3, 2}, {-2.8, -1.7}, {-2.0, 0}, {-1.8, 2}, {-1.6, 0}, {-1.2,
     3.5}, {-1, -3.8}, {-0.5, 0}, {0, 14}, {0.8, -5}, {1, 0}, {1.5, 
    5}, {1.8, 0}, {2, -5}, {2.2, 0}, {2.5, 4.5}, {2.8, 
    0}, {3, -3}, {3.5, 0}, {4, 2}, {4.2, 0}, {4.5, -2}, {5, 0}};

I have checked the pacific data set, but nothing is wrong with it.

Any ideas how to plot this as piecewise?

Thanks

$\endgroup$
4
  • 2
    $\begingroup$ You must plot "pacificfun[x]" and not "pacificpoints[x]" $\endgroup$ Nov 30, 2022 at 12:55
  • $\begingroup$ Right, but that gives a really weird plot. It does not correspond to the points $\endgroup$
    – Vangsnes
    Nov 30, 2022 at 12:56
  • $\begingroup$ IM, thanks, but I need a real piecewise function, and it works for the given command on the alwynpoints. $\endgroup$
    – Vangsnes
    Nov 30, 2022 at 13:03
  • 2
    $\begingroup$ The definition of pacificfun contains the undefined array pacific. Replacing pacific by pacificpoints gives a reasonable plot. $\endgroup$
    – bbgodfrey
    Nov 30, 2022 at 15:15

3 Answers 3

3
$\begingroup$

After replacing "pacific" by "pacificpoints" :

pacificfun[x_] =  Piecewise[
   Table[{pacificpoints[[i, 2]], pacificpoints[[i, 1]] > x}, {i,Length[pacificpoints]}], 0];

we can plot the data:

Plot[pacificfun[x], {x, -5, 5}, PlotRange -> Full]

enter image description here

To check the first few data:

pacificfun /@ {-4.9, -4.6, -4.3, -4.1, -3.9}

(* {6, 3, 14.5, 5, 8} *)

This seems to correspnd to the picture.

$\endgroup$
1
  • $\begingroup$ Thank you! .... $\endgroup$
    – Vangsnes
    Nov 30, 2022 at 13:27
3
$\begingroup$

Try the built-in ListStepPlot function if the Piecewise is not essential for further computation.

ListStepPlot[pacificpoints
 , Left
 , Joined -> False
 ]

If the function is indispensable:

pacificfun[x_] = 
  Piecewise@(({Last@#, First@# > x} & /@ pacificpoints)~
     Join~{{0, True}});

Plot[pacificfun[x], {x, Splice[MinMax@pacificpoints[[All, 1]]]}]
$\endgroup$
1
  • $\begingroup$ THanks Syed, but the Piecewise is absolutely essential $\endgroup$
    – Vangsnes
    Nov 30, 2022 at 14:05
2
$\begingroup$

The solution is

pacificfun[x_] := 
  Piecewise[
   Table[{pacificpoints[[i, 2]], pacificpoints[[i, 1]] > x}, {i, 
     Length[pacificpoints]}], 0];
Plot[pacificfun[x], {x, -5, 5}, PlotRange -> Full]
$\endgroup$

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