Ask Your Question

# piecewise function with variable within domains

Dear all,

I am trying to define a piecewise function where the domains are parametrized by some variable:

import sage.all as sage
sage.var('x')
sage.var('x0')
sage.var('x1')
sage.assume( 0<x0)
sage.assume(x0<x1)
sage.assume(x1< 1)
sage.piecewise([((0,x0),0), ([x0,x1],1), ((x1,1),1)], var=x)


However, I cannot seem to get it to work:

TypeError: unable to simplify to a real interval approximation


Any idea how to make it work? Thanks!

Martin

edit retag close merge delete

## Comments

a workaround is to define it as linear combination of heaviside, or step functions. these special functions are documented here.

( 2017-10-09 00:21:47 -0600 )edit

The constructor

f = sage.piecewise( [ ((0,x0),0), ([x0,x1],1), ((x1,1),2) ], var=x )


crashes because RLF( x0 ) makes no sense. It is generally not a good idea to assume that code written for a special purpose can do the same job for a similar mathematical, possibly more general situation. But let's say the constructor would have instantiated something (now with possibly broken methods).

Which is the application of the above?

Make "it" work for which purpose?

What should be done now with "that f that works" ?

( 2017-10-09 16:37:53 -0600 )edit

## 1 answer

Sort by ยป oldest newest most voted

What's about a python function?

def my_piecewise(x0,x1):
if 0 < x0 < x1 < 1:
return piecewise([((0,x0),0), ([x0,x1],1), ((x1,1),2)], var=x)

my_piecewise(0.2,0.7).plot()

more

## Your Answer

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

Add Answer

## Stats

Asked: 2017-10-08 12:30:19 -0600

Seen: 40 times

Last updated: Oct 09