Ask Your Question
0

How to plot composition of unit step and sin functions

asked 2012-04-16 10:39:33 +0100

noufalasharaf gravatar image

updated 2015-01-13 22:37:02 +0100

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 "_sage_input_68.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("cGxvdChoKHNpbih4KSksICh4LCAtMTAsIDEwKSk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>

  File "/tmp/tmpeAn7w2/___code___.py", 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/decorators.py", line 657, in wrapper
    return func(*args, **kwds)
  File "/sage/local/lib/python2.6/site-packages/sage/misc/decorators.py", line 504, in wrapper
    return func(*args, **options)
  File "/sage/local/lib/python2.6/site-packages/sage/plot/plot.py", line 3071, in plot
    G = _plot(funcs, *args, **kwds)
  File "/sage/local/lib/python2.6/site-packages/sage/plot/plot.py", 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/misc.py", 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
1

answered 2012-04-16 10:58:51 +0100

niles gravatar image

updated 2012-04-16 12:15:27 +0100

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

UPDATE:

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))
1
sage: h(exp(x)-100)
sage: bool(exp(x) > 0)
True
sage: bool(sin(x) > 0)
False

sage: def h2(x):
...    if x > 0: return 1
...    if x <= 0: return 0
...    return 'Hello World!'
...    
sage: h2(exp(x))
1
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

Comments

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 11:03:13 +0100 )edit

now i understood Niles thanks for your kindhearted help

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

Stats

Asked: 2012-04-16 10:39:33 +0100

Seen: 1,387 times

Last updated: Apr 16 '12