Ask Your Question
1

Plotting Piecewise function

asked 2012-04-29 16:18:48 +0100

Ellie gravatar image

updated 2012-04-30 00:34:39 +0100

kcrisman gravatar image

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 flag offensive close merge delete

Comments

@TSchwenn; the formatting should be fixed now.

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

2 Answers

Sort by ยป oldest newest most voted
4

answered 2012-04-30 00:34:15 +0100

kcrisman gravatar image

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)

image description

edit flag offensive delete link more
1

answered 2012-04-29 18:20:18 +0100

TSchwenn gravatar image

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))
edit flag offensive delete link more

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-29 16:18:48 +0100

Seen: 2,367 times

Last updated: Apr 30 '12