Chris kim
Chris kim

Reputation: 121

Integration with piecewise functions

In my computation, I need the following numerically in Python.

enter image description here

z has a range [0, 4].

From the help of Andrey Tyukin, I wrote the codes.

However, I can't determine the following codes.

enter image description here

Upvotes: 2

Views: 382

Answers (1)

Andrey Tyukin
Andrey Tyukin

Reputation: 44957

Essentially, you can get the only relevant k from z simply by int(z * 10). Then, you have to make sure that the function is 0 outside of the interval [0, 4]. The rest is just evaluation of a simple polynomial. Try something like this:

def f(z):
  k = int(z * 10)
  if k < 0 or k > 40: return 0.0
  return (
    ((2 * k + 1) / 20.0 - ((k + 1)**4 - k ** 4) / 1000.0) * 
    (z - k / 10.0) +
    (k*k/200.0 - k**4 / 10000.0)
  )

I assume that there is actually a typo in your formula, and you actually want characteristic functions on [k/10, (k+1)/10) (closed, open), instead of [k/10, (k+1)/10] (closed, closed). The discontinuous jumps between the intervals wouldn't matter while integrating, but it feels as if they shouldn't be there in the first place.

Upvotes: 1

Related Questions