Message Boards Message Boards

0
|
2835 Views
|
5 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Going from points to piecewise function

Posted 1 year ago

Hi, I am trying to go from a set of points to a piecewise function.

I use the following command, but I get only one large step , in the step-wise function.

points = {{1004.9591023489934`, 
    213.55957424496637`}, {998.3619966442955`, 
    297.1229131711409`}, {996.1629614093961`, 
    275.13256082214764`}, {989.5658557046982`, 
    386.55034605704714`}, {982.96875`, 
    360.1619232382552`}, {971.9735738255035`, 
    298.58893666107394`}, {963.1774328859062`, 
    305.186042365772`}, {954.3812919463089`, 
    270.00147860738275`}, {945.5851510067116`, 
    340.3706061241612`}, {941.1870805369128`, 
    296.3899014261747`}, {934.5899748322149`, 
    329.3754299496645`}, {927.9928691275168`, 
    274.3995490771813`}, {921.3957634228188`, 
    307.38507760067125`}, {910.4005872483222`, 
    305.186042365772`}, {908.201552013423`, 
    351.3657822986579`}, {890.6092701342284`, 
    210.62752726510075`}, {881.8131291946311`, 
    417.3368393456377`}, {864.2208473154362`, 
    245.81209102348998`}, {855.4247063758389`, 
    344.76867659395987`}, {848.827600671141`, 
    285.394725251678`}, {842.2304949664431`, 
    377.7542051174498`}, {826.8372483221477`, 
    265.603408137584`}, {820.2401426174498`, 
    274.3995490771813`}, {809.2449664429531`, 
    377.7542051174498`}, {802.6478607382551`, 
    252.40919672818814`}, {793.8517197986578`, 
    267.80244337248337`}, {785.0555788590605`, 
    393.14745176174506`}, {769.66`, 168.85`}, {758.67`, 
    28}, {745.4729446308726`, 
    175.44296350671158`}, {730.0796979865772`, 
    346.96771182885925`}, {723.4825922818793`, 
    410.7397336409398`}, {719.0845218120807`, 
    274.3995490771814`}, {710.2883808724833`, 
    285.39472525167804`}, {705.8903104026846`, 
    252.40919672818808`}, {692.6960989932886`, 
    289.7927957214767`}, {688.29802852349`, 
    415.13780411073844`}, {677.3028523489934`, 
    313.9821833053693`}, {664.1086409395974`, 
    190.83621015100687`}, {655.3125000000001`, 
    443.72526216442975`}, {646.5163590604027`, 
    270.0014786073827`}, {639.9192533557048`, 
    248.01112625838942`}, {631.1231124161075`, 
    327.17639471476525`}, {617.9289010067115`, 
    373.3561346476512`}, {611.3317953020135`, 
    338.17157088926194`}, {598.1375838926175`, 
    221.6227034395975`}, {589.3414429530202`, 
    404.1426279362418`}, {580.5453020134229`, 
    259.00630243288606`}, {573.9481963087248`, 
    305.186042365772`}, {571.7491610738256`, 360.1619232382552`}};
draupnerfun[t_] := 
 Piecewise[
  Table[{points[[i, 2]], points[[i, 1]] > t}, {i, Length[points]}], 0]
p0 = Plot[draupnerfun[t], {t, 570, 1005}]

and I got this

enter image description here

Evidently, this didn't work. What is wrong with this command?

Thanks

POSTED BY: Ser Man
5 Replies

Here is one more approach - for calculating the range of each point I am using a one-dimensional VoronoiMesh[]:

points = SortBy[points, First];
xvals = List /@ points[[All, 1]];
vorm = VoronoiMesh[xvals];
ranges = MeshPrimitives[vorm, 1] /. Line -> Flatten;
func[\[FormalX]_] = Piecewise@MapThread[{#2[[2]], #1[[1]] < \[FormalX] < #1[[2]]} &, {ranges, points}]

The result is somewhat different from that of Gianluca - it depends on what you need.

POSTED BY: Henrik Schachner
Posted 1 year ago

Thank you very much Gianluca! This works great!

POSTED BY: Ser Man

This may work for you:

Clear[x];
f[x_] = Piecewise[
   Partition[Sort[points], 2, 
     1] /. {{a_?NumericQ, b_}, {c_, d_}} :> {b, a <= x < c}];
Plot[f[x], {x, 570, 1000}]
POSTED BY: Gianluca Gorni
Posted 1 year ago

Thanks Marco, however interpolation does not give me an analytic function, as Piecewise does.

POSTED BY: Ser Man

Hi there,

something like this?

f = Interpolation[points, InterpolationOrder -> 0]
Plot[f[x], {x, Min[points[[All, 1]]], Max[points[[All, 1]]]}]

enter image description here

Cheers, Marco

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract