# Plotting Piecewise function

I have a large piecewise function (with 180 equations), how do I plot this on sage? Thanks

heres a sample:

[(1234, 1234), 0.0], [(1234,
1234), 0.0020259319286871956*x + 2.5], [(1234, 1234),
0.0060777957860615869*x + 7.5], [(1234, 1234), 0.010129659643435977*x +
12.5], [(1234, 1244), 0.0156672069165316*x + 19.333333335], [(1244,
1254), 0.015541264738745977*x + 19.333333335], [(1254, 1264),
0.015417331208133968*x + 19.333333335], [(1264, 1274),
0.015295358651107592*x + 19.333333335], [(1274, 1312),
0.015175300890894815*x + 19.333333335], [(1312, 1312),
0.014735772358993898*x + 19.333333335], [(1312, 1312),
0.022103658536585365*x + 29.0]

edit retag close merge delete

@TSchwenn; the formatting should be fixed now.

( 2012-04-30 00:31:54 +0100 )edit

Sort by ยป oldest newest most voted

Using Sage's decent-but-needs-an-overhaul piecewise class:

sage: L = [[(1234, 1244), 0.0156672069165316*x + 19.333333335], [(1244,
1254), 0.015541264738745977*x + 19.333333335], [(1254, 1264),
0.015417331208133968*x + 19.333333335], [(1264, 1274),
0.015295358651107592*x + 19.333333335], [(1274, 1312),
0.015175300890894815*x + 19.333333335]]
sage: P = Piecewise(L,x)
sage: P
Piecewise defined function with 5 parts, [[(1234, 1244), x |--> 0.0156672069165316*x + 19.3333333350000], [(1244, 1254), x |--> 0.01554126473874598*x + 19.3333333350000], [(1254, 1264), x |--> 0.01541733120813397*x + 19.3333333350000], [(1264, 1274), x |--> 0.01529535865110759*x + 19.3333333350000], [(1274, 1312), x |--> 0.01517530089089482*x + 19.3333333350000]]
sage: plot(P)


more

Quick and dirty, with a cleaned up sample dataset (I just added *s by hand, but there must be a smarter way to do that). See the output at http://www.sagenb.org/home/pub/4662/

x = var('x')
sample = [(1234, 1234), 0.0020259319286871956*x + 2.5]\
, [(1234, 1234), 0.0060777957860615869*x + 7.5]\
, [(1234, 1234), 0.010129659643435977*x + 12.5]\
, [(1234, 1244), 0.0156672069165316*x + 19.333333335]\
, [(1244, 1254), 0.015541264738745977*x + 19.333333335]\
, [(1254, 1264), 0.015417331208133968*x + 19.333333335]\
, [(1264, 1274), 0.015295358651107592*x + 19.333333335]\
, [(1274, 1312), 0.015175300890894815*x + 19.333333335]

def piecewiser(pieces):
p = 0
for i in pieces:
if i[0][0] == i[0][1]: #if this piece is a point
q = text("o", (i[0][0], i[1].substitute(x=i[0][0]))) #put an "o" at that point
else:
q = plot(i[1], (x, i[0][0], i[0][1])) #otherwise plot it as a function
if p == 0:
p = q
else:
p = p + q
return p
show(piecewiser(sample))

more