Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Two piecewise defined functions in one plot

Is it possible to plot 2 piecewise defined functions to one plot?

I tried:

f = Piecewise([[(-2,1),1],[(1,4),x]])
g =  Piecewise([[(-2,1),1],[(1,4),2*x]])
plot([f,g])

but I get the following error message:

{{{id=6| x_1 = Piecewise([[(-100,t_R),v_0t],[(t_R,t_R + v_0/a),-1/2a(t-t_R)^2 + v_0t],[(t_R + v_0/a,100),v_0^2/(2a) + v_0t_R]]) x_2 = Piecewise([[(-100,2t_R),v_0t + s_S],[(t_R,2t_R + v_0/a),-1/2a(t-2t_R)^2 + v_0t + s_S],[(2t_R + v_0/a,100),v_0^2/(2a) + v_0(2*t_R) + s_S ]]) /// }}}

{{{id=7| xp = plot([x_1(t),x_2(t)]); xp.show(axes_labels=["Zeit t in s","x_1 in m"],xmin=0,xmax=v_0/t_R + t_R,ymin=0) /// Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_137.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -- coding: utf-8 --\n" + _support_.preparse_worksheet_cell(base64.b64decode("eHAgPSBwbG90KFt4XzEodCkseF8yKHQpXSk7CnhwLnNob3coYXhlc19sYWJlbHM9WyJaZWl0IHQgaW4gcyIsInhfMSBpbiBtIl0seG1pbj0wLHhtYXg9dl8wL3RfUiArIHRfUix5bWluPTAp"),globals())+"\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in <module>

File "/tmp/tmpjQcRVV/___code___.py", line 3, in <module> xp = plot([x_1(t),x_2(t)]); File "/opt/sage-4.7.1-linux-64bit-ubuntu_10.04.1_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/functions/piecewise.py", line 648, in __call__ raise ValueError,"Value not defined outside of domain." ValueError: Value not defined outside of domain. }}}

{{{id=13| f = Piecewise([[(-2,1),1],[(1,4),x]]) g = Piecewise([[(-2,1),1],[(1,4),2x]]) plot([f,g]) /// Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_145.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -- coding: utf-8 -*-\n" + _support_.preparse_worksheet_cell(base64.b64decode("ZiA9IFBpZWNld2lzZShbWygtMiwxKSwxXSxbKDEsNCkseF1dKQpnID0gIFBpZWNld2lzZShbWygtMiwxKSwxXSxbKDEsNCksMip4XV0pCnBsb3QoW2YsZ10p"),globals())+"\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in <module>

File "/tmp/tmpSARQLu/___code___.py", line 5, in <module> exec compile(u'plot([f,g])' + '\n', '', 'single') File "", line 1, in <module>

File "/opt/sage-4.7.1-linux-64bit-ubuntu_10.04.1_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/misc/decorators.py", line 657, in wrapper return func(args, kwds) File "/opt/sage-4.7.1-linux-64bit-ubuntu_10.04.1_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/misc/decorators.py", line 504, in wrapper return func(args, *options) File "/opt/sage-4.7.1-linux-64bit-ubuntu_10.04.1_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/plot/plot.py", line 3067, in plot G = _plot(funcs, (xmin, xmax), *kwds) File "/opt/sage-4.7.1-linux-64bit-ubuntu_10.04.1_lts-x86_64-Linux/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 "/opt/sage-4.7.1-linux-64bit-ubuntu_10.04.1_lts-x86_64-Linux/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 1357, in sage.ext.fast_eval.fast_float (sage/ext/fast_eval.c:8418) File "fast_eval.pyx", line 1377, in sage.ext.fast_eval.fast_float (sage/ext/fast_eval.c:8704) AttributeError: PiecewisePolynomial instance has no attribute '__float__' }}}

Two piecewise defined functions in one plot

Is it possible to plot 2 piecewise defined functions to one plot?

I tried:

f = Piecewise([[(-2,1),1],[(1,4),x]])
g =  Piecewise([[(-2,1),1],[(1,4),2*x]])
plot([f,g])

but I get the following error message:

{{{id=6|
x_1 = Piecewise([[(-100,t_R),v_0t],[(t_R,t_R + v_0/a),-1/2a(t-t_R)^2 + v_0t],[(t_R + v_0/a,100),v_0^2/(2a) + v_0t_R]])
Piecewise([[(-100,t_R),v_0*t],[(t_R,t_R + v_0/a),-1/2*a*(t-t_R)^2 + v_0*t],[(t_R + v_0/a,100),v_0^2/(2*a) + v_0*t_R]])
x_2 = Piecewise([[(-100,2t_R),v_0t + s_S],[(t_R,2t_R + v_0/a),-1/2a(t-2t_R)^2 + v_0t + s_S],[(2t_R + v_0/a,100),v_0^2/(2a) + v_0(2*t_R) Piecewise([[(-100,2*t_R),v_0*t + s_S],[(t_R,2*t_R + v_0/a),-1/2*a*(t-2*t_R)^2 + v_0*t + s_S],[(2*t_R + v_0/a,100),v_0^2/(2*a) + v_0*(2*t_R) + s_S ]])
///
}}}

}}} {{{id=7| xp = plot([x_1(t),x_2(t)]); xp.show(axes_labels=["Zeit t in s","x_1 in m"],xmin=0,xmax=v_0/t_R + t_R,ymin=0) /// Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_137.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -- -*- coding: utf-8 --\n" + _support_.preparse_worksheet_cell(base64.b64decode("eHAgPSBwbG90KFt4XzEodCkseF8yKHQpXSk7CnhwLnNob3coYXhlc19sYWJlbHM9WyJaZWl0IHQgaW4gcyIsInhfMSBpbiBtIl0seG1pbj0wLHhtYXg9dl8wL3RfUiArIHRfUix5bWluPTAp"),globals())+"\n"); -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("eHAgPSBwbG90KFt4XzEodCkseF8yKHQpXSk7CnhwLnNob3coYXhlc19sYWJlbHM9WyJaZWl0IHQgaW4gcyIsInhfMSBpbiBtIl0seG1pbj0wLHhtYXg9dl8wL3RfUiArIHRfUix5bWluPTAp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in <module>

<module> File "/tmp/tmpjQcRVV/___code___.py", line 3, in <module> xp = plot([x_1(t),x_2(t)]); File "/opt/sage-4.7.1-linux-64bit-ubuntu_10.04.1_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/functions/piecewise.py", line 648, in __call__ raise ValueError,"Value not defined outside of domain." ValueError: Value not defined outside of domain. }}}

}}} {{{id=13| f = Piecewise([[(-2,1),1],[(1,4),x]]) g = Piecewise([[(-2,1),1],[(1,4),2x]]) Piecewise([[(-2,1),1],[(1,4),2*x]]) plot([f,g]) /// Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_145.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -- -*- coding: utf-8 -*-\n" + _support_.preparse_worksheet_cell(base64.b64decode("ZiA9IFBpZWNld2lzZShbWygtMiwxKSwxXSxbKDEsNCkseF1dKQpnID0gIFBpZWNld2lzZShbWygtMiwxKSwxXSxbKDEsNCksMip4XV0pCnBsb3QoW2YsZ10p"),globals())+"\n"); -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("ZiA9IFBpZWNld2lzZShbWygtMiwxKSwxXSxbKDEsNCkseF1dKQpnID0gIFBpZWNld2lzZShbWygtMiwxKSwxXSxbKDEsNCksMip4XV0pCnBsb3QoW2YsZ10p"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in <module>

<module> File "/tmp/tmpSARQLu/___code___.py", line 5, in <module> exec compile(u'plot([f,g])' + '\n', '', 'single') File "", line 1, in <module>

<module> File "/opt/sage-4.7.1-linux-64bit-ubuntu_10.04.1_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/misc/decorators.py", line 657, in wrapper return func(args, kwds) func(*args, **kwds) File "/opt/sage-4.7.1-linux-64bit-ubuntu_10.04.1_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/misc/decorators.py", line 504, in wrapper return func(args, *options) func(*args, **options) File "/opt/sage-4.7.1-linux-64bit-ubuntu_10.04.1_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/plot/plot.py", line 3067, in plot G = _plot(funcs, (xmin, xmax), *kwds) **kwds) File "/opt/sage-4.7.1-linux-64bit-ubuntu_10.04.1_lts-x86_64-Linux/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 "/opt/sage-4.7.1-linux-64bit-ubuntu_10.04.1_lts-x86_64-Linux/local/lib/python2.6/site-packages/sage/plot/misc.py", line 138, in setup_for_eval_on_grid return fast_float(funcs, *vars,options), *vars,**options), [tuple(range+[range_step]) for range,range_step in zip(ranges, range_steps)] File "fast_eval.pyx", line 1357, in sage.ext.fast_eval.fast_float (sage/ext/fast_eval.c:8418) File "fast_eval.pyx", line 1377, in sage.ext.fast_eval.fast_float (sage/ext/fast_eval.c:8704) AttributeError: PiecewisePolynomial instance has no attribute '__float__' }}}

}}}