1

I have a function defined by its slope in a continuous sequence of intervals, e.g.

intervals = {{0, 10}, {10, 15}, {15, 25}}
slopes = {4, 5, 6}
fun = Piecewise@MapThread[{#2*(b - #1[[1]]), #1[[1]] < b <= #1[[2]]} &, {intervals,slopes}]
Plot[fun, {b, Min@Flatten@intervals, Max@Flatten@intervals}]

piecewise

I would like this function to be continuous. What I thought is to accumulate the constants one interval at a time and then go through and add them back to the function, which works fine but is in principle O(N) in the number of intervals:

constants = Prepend[Accumulate[fun /. ({b -> #} & /@ (intervals\[Transpose][[2]]))][[;; -2]], 0]
funWithConstants = Piecewise@MapThread[{#1[[1]] + #2, #1[[2]]} &, {fun[[1]], constants}]
Plot[funWithConstants, {b, Min@Flatten@intervals,Max@Flatten@intervals}]

enter image description here

Then I noticed in the documentation for Piecewise that there seems to be a native way to compute the necessary constants for the integral of the function:

Integrate[fun, b]
Plot[%, {b, Min@Flatten@intervals, Max@Flatten@intervals}]

piecewiseintegrated

Is there a way to use this native algorithm find such constants for the function itself? I would guess it is probably faster then mine, and maybe faster than O(N).

ninemileskid
  • 529
  • 3
  • 7
  • 1
    Look at this http://mathematica.stackexchange.com/questions/71426/function-for-a-series-of-joined-slopes – Basheer Algohi Apr 08 '15 at 00:44
  • nine, I propose closing this as a duplicate of the question linked above. (Incidentally my own.) If you feel this is not a duplicate please edit your question to clearly explain why. – Mr.Wizard Apr 08 '15 at 16:01
  • I agree with you. Could you add some tags related to Piecewise to your question? My preliminary search didn't turn it up, nor was it suggested in the sidebar. – ninemileskid Apr 08 '15 at 16:49
  • @ninemileskid Your question will remain for others to find and serve as a redirect to the earlier question. That is a primary purpose of marking questions as duplicates. – Mr.Wizard Apr 10 '15 at 06:14

0 Answers0