Ask Your Question

How to plot composition of unit step and sin functions

asked 2012-04-16 03:39:33 -0500

noufalasharaf gravatar image

updated 2015-01-13 15:37:02 -0500

FrédéricC gravatar image

When i was trying to plot the function U(sin(x)) where U is the unit step function it leaves error message, but U(exp(x)) worked nicely

the error message was

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "", line 10, in <module>
    exec compile(u'open("","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("cGxvdChoKHNpbih4KSksICh4LCAtMTAsIDEwKSk="),globals())+"\\n"); execfile(os.path.abspath(""))
  File "", line 1, in <module>

  File "/tmp/tmpeAn7w2/", line 3, in <module>
    exec compile(u'plot(h(sin(x)), (x, -_sage_const_10 , _sage_const_10 ))
  File "", line 1, in <module>

  File "/sage/local/lib/python2.6/site-packages/sage/misc/", line 657, in wrapper
    return func(*args, **kwds)
  File "/sage/local/lib/python2.6/site-packages/sage/misc/", line 504, in wrapper
    return func(*args, **options)
  File "/sage/local/lib/python2.6/site-packages/sage/plot/", line 3071, in plot
    G = _plot(funcs, *args, **kwds)
  File "/sage/local/lib/python2.6/site-packages/sage/plot/", line 3105, in _plot
    funcs, ranges = setup_for_eval_on_grid(funcs, [xrange], options['plot_points'])
  File "/sage/local/lib/python2.6/site-packages/sage/plot/", line 138, in setup_for_eval_on_grid
    return fast_float(funcs, *vars,**options), [tuple(range+[range_step]) for range,range_step in zip(ranges, range_steps)]
  File "fast_eval.pyx", line 1388, in sage.ext.fast_eval.fast_float (sage/ext/fast_eval.c:8901)
TypeError: no way to make fast_float from None
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2012-04-16 03:58:51 -0500

niles gravatar image

updated 2012-04-16 05:15:27 -0500

Can you tell us more about what you're trying to do? I have no trouble with the following:

sage: plot(unit_step(sin(x)), -2*pi,2*pi)

image description


I see; the problem you're running into is that h is a python function, while things like sin(x) and exp(x) are symbolic expressions. When you type h(f(x)), Sage is actually evaluating the function h on the entire symbolic expression f(x). When f(x) is the exponential funciton, Sage knows that exp(x) is always positive, and hence h returns 1. When f(x) is the sine function, neither of the conditions is met and so h returns nothing, hence giving the error you saw. Here is some code demonstrating these things:

sage: def h(x):
...    if x > 0: return 1
...    if x <= 0: return 0
sage: h(sin(x))
sage: h(exp(x))
sage: h(exp(x)-100)
sage: bool(exp(x) > 0)
sage: bool(sin(x) > 0)

sage: def h2(x):
...    if x > 0: return 1
...    if x <= 0: return 0
...    return 'Hello World!'
sage: h2(exp(x))
sage: h2(sin(x))
'Hello World!'

And lastly, here's a solution if you really do want to use a Python function: just make the whole function a Python function:

sage: def c(x):
...     return h(sin(x))
sage: plot(c, (x,-10,10))
edit flag offensive delete link more


i defined unit step my own using def in this way def h(x): if (x>0): return 1 if (x<=0): return 0 and then plot(h(sin(x)), (x, -10, 10))

noufalasharaf gravatar imagenoufalasharaf ( 2012-04-16 04:03:13 -0500 )edit

now i understood Niles thanks for your kindhearted help

noufalasharaf gravatar imagenoufalasharaf ( 2012-04-16 04:05:31 -0500 )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


Asked: 2012-04-16 03:39:33 -0500

Seen: 829 times

Last updated: Apr 16 '12