Skip to content

Instantly share code, notes, and snippets.

@chaoxu
Created March 30, 2016 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaoxu/b844526d149033c9227a4da32d833123 to your computer and use it in GitHub Desktop.
Save chaoxu/b844526d149033c9227a4da32d833123 to your computer and use it in GitHub Desktop.
data = {0, 10, 0, 20, 10, 30, 0};
breaks = MapIndexed[
With[{pos = First[#2], y1 = First[#],
y2 = Last[#]}, {y1 (1 + pos - x) + y2 (x - pos),
pos <= x < pos + 1}] &, Partition[data, 2, 1]];
f[x_] := Piecewise[breaks];
Plot[f[x], {x, 1, Length[data]}]
h[lambda_] :=
lambda + Max[
Table[MaxValue[{breaks[[i]][[1]] - x,
Inequality[breaks[[i]][[2]][[1]], breaks[[i]][[2]][[2]],
breaks[[i]][[2]][[3]], breaks[[i]][[2]][[4]],
Min[breaks[[i]][[2]][[5]], lambda]]}, x], {i, 1,
Length[breaks]}]]
Plot[h[zzz], {zzz, 1, Length[breaks]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment