Ask Your Question
1

Two piecewise defined functions in one plot

asked 2011-11-03 11:40:56 +0100

sagefan gravatar image

updated 2015-02-26 14:30:19 +0100

slelievre gravatar image

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_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,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"); 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),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"); 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__'
}}}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2011-11-03 11:52:05 +0100

kcrisman gravatar image

updated 2011-11-04 12:54:39 +0100

It should be. That's a bug.

In fact, it's the first thing on the list at Ticket #11225.

It probably wouldn't even be that hard to fix. The problem is that setup_for_eval_on_grid wants to use fast_float but Piecewise guys don't have that.

Edit to answer the question about a workaround:

Just make two separate plots and add them! It's fun and easy, I never cease to be thankful for this functionality...

sage: f = Piecewise([[(-2,1),1],[(1,4),x]])
sage: g =  Piecewise([[(-2,1),1],[(1,4),2*x]])
sage: P = plot(f,color='green')
sage: Q = plot(g,linestyle='--')
sage: P+Q
edit flag offensive delete link more

Comments

1

Thanks. Since the Ticket is open for 7 month I guess that it will not be fixed in the next few weeks. So is there any workaround?

sagefan gravatar imagesagefan ( 2011-11-04 11:18:13 +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: 2011-11-03 11:40:56 +0100

Seen: 890 times

Last updated: Feb 26 '15