Ask Your Question
0

Function with a lower limit

asked 2016-04-22 17:10:44 -0600

Massimo2013 gravatar image

I need to define a function like the following:

y = 5 - 2x  if f(x)>0
    0       otherwise

I tried different solutions:

f(x) = 5 - 2*x
g(x) = max(f(x),0)

or:

f(x) = 5 - 2*x
def g(x):
     return f(x) if f(x)>0 else 0

However, when I try to plot them, they do not seem to work: the first ignores the lower bound, while the second is always zero. This is strange, as when I try [g(x) for x in range(0,10)] it returns the correct values of g(x)

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2016-04-23 14:29:15 -0600

tmonteil gravatar image

Concerning your second definition, as @paulmasson, i can not reproduce your problem.

Concerning you first definition, there is something tricky about symbolics to understand: when you write g(x) = max(f(x),0), the Python builtin max is used, which takes the max between two Python/Sage objects f(x) and 0, not pointwise! Since those two objects are not comparable, it considers that the first is to be returned, so max(f(x),0) is nothing but the object f(x). You can check by changing the order max(0,f(x)):

sage: g(x) = max(f(x),0)
sage: g
x |--> -2*x + 5
sage: g(x) = max(0,f(x))
sage: g
x |--> 0

So, you have to use is the "symbolic maximum", viewed as a mathematical function:

sage: g(x) = max_symbolic(f(x),0)
sage: g
x |--> max(-2*x + 5, 0)
sage: g.plot(-10,10)
edit flag offensive delete link more

Comments

@tmonteil It appears that Sage has native ceil and floor functions that override Python functions and can handle symbolics. Do you know why that wasn't done for max and min as well?

paulmasson gravatar imagepaulmasson ( 2016-04-25 16:37:04 -0600 )edit
1

answered 2016-04-22 20:12:51 -0600

updated 2016-04-24 15:36:47 -0600

Could be how you're plotting the function. This works for me:

f(x) = 5 - 2*x
def g(x):
    return f(x) if f(x)>0 else 0
plot(g,x,-10,10)

With a slight modification to your first solution, this also works:

f(x) = 5 - 2*x
def g(x):
    return max(f(x),0)
plot(g,x,-10,10)

EDIT: Your plotting issues are a result of what Sage documentation calls "accidental evaluation". See item (4) on this page: http://doc.sagemath.org/html/en/tutor...

edit flag offensive delete link more

Comments

You are right: it seems to work with plot(g,x,0,10). However, it does not work properly with plot(g(x),x,0,10) Is there something I'm missing about the working of plot?

Massimo2013 gravatar imageMassimo2013 ( 2016-04-23 01:57:03 -0600 )edit

Besides, the function g(x) does not seem to be used correctly by solve: solve([g(x)==y,x==4],[x,y]) gives a negative value for y

Massimo2013 gravatar imageMassimo2013 ( 2016-04-23 02:20:36 -0600 )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: 2016-04-22 17:10:44 -0600

Seen: 300 times

Last updated: Apr 24 '16