1
$\begingroup$

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).

$\endgroup$
4
  • 1
    $\begingroup$ Look at this mathematica.stackexchange.com/questions/71426/… $\endgroup$ Commented Apr 8, 2015 at 0:44
  • $\begingroup$ 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. $\endgroup$ Commented Apr 8, 2015 at 16:01
  • $\begingroup$ 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. $\endgroup$ Commented Apr 8, 2015 at 16:49
  • $\begingroup$ @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. $\endgroup$ Commented Apr 10, 2015 at 6:14

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.