First time here? Check out the FAQ!

Ask Your Question
0

Alternative to piecewise functions?

asked 9 years ago

Oderyn gravatar image

updated 9 years ago

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.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
2

answered 9 years ago

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))
Preview: (hide)
link

Comments

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

candide gravatar imagecandide ( 8 years ago )

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: 9 years ago

Seen: 1,698 times

Last updated: Apr 21 '15