Ask Your Question
0

Alternative to piecewise functions?

asked 2015-04-21 08:47:42 -0500

Oderyn gravatar image

updated 2015-04-21 09:54:48 -0500

If I only need indicator functions (the function is 1 in an interval [a, b], 0 outside of it). Is there an alternative to piecewise functions (with all their issues like plotting), which I can use? Thank you!

EDIT: Ok, I had an idea:

indicator(x,a,b)=(sign(x-a)-sign(x-b))/2

That does pretty much what I want.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2015-04-21 09:59:24 -0500

calc314 gravatar image

Here are two other options:

1) Use the Heaviside function:

f(x)=heaviside(x-1)-heaviside(x-3)
plot(f(x),(x,0,5))

2) define a Python function

def f(x,a,b):
    if x>a and x<b:
        return(1)
    else:
        return(0)

plot(lambda x: f(x,1,3),(x,-3,5))
edit flag offensive delete link more

Comments

f(x,a,b) and a<x<b have same value.

candide gravatar imagecandide ( 2016-11-16 08:26:18 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-04-21 08:47:42 -0500

Seen: 371 times

Last updated: Apr 21 '15