Ask Your Question

# Function with a lower limit

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 close merge delete

## 2 Answers

Sort by » oldest newest most voted

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)

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?

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...

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?

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

## Your Answer

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

Add Answer

## Stats

Asked: 2016-04-23 00:10:44 +0200

Seen: 386 times

Last updated: Apr 24 '16