Ask Your Question
0

Alternative to piecewise functions?

asked 2015-04-21 15:47:42 +0200

Oderyn gravatar image

updated 2015-04-21 16:54:48 +0200

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 16:59:24 +0200

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 15:26:18 +0200 )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 15:47:42 +0200

Seen: 1,278 times

Last updated: Apr 21 '15