Group Abstract Group Abstract

Message Boards Message Boards

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

Going from points to piecewise function

Posted 2 years ago
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

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 2 years ago

Thank you very much Gianluca! This works great!

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
Posted 2 years ago

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

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