def funcgraph(funclist, vw=ViewWindow(), colorlist=None, auto=False, filename="plot" + str(int(time())), gif=False, add=False, avg=False, save=True): images = [] if auto: vw.autograph(funclist) if callable(funclist): funclist = [funclist] data = PlotData(vw) if colorlist is None: colorlist = Color.colors(len(funclist)) for j in range(len(funclist)): f = funclist[j] it = vw.xiterator() x0 = it.next() for x1 in it: if colorlist == "rainbow": color = Color.rbow(x1, 2) else: color = colorlist[j] data.line((x0, f(x0)), (x1, f(x1)), color, add, avg) x0 = x1 if gif: images.append(data.save("", False)) if gif: writegif(filename, images, 0.01) return data.save(filename, save)
def rk4(f, initlist, vw=ViewWindow(), h=.001, colorlist=None, sf=False, sfspread=20, sfcolor=0x0000FF, filename="rk4" + str(int(time())), save=True): data = PlotData(vw) if sf: data.data = list( slopefield(f, vw, sfspread, sfcolor, save=False).getdata()) if colorlist is None: colorlist = Color.colors(len(initlist)) for i in range(len(initlist)): c = colorlist[i] for h1 in [h, -h]: x0, y0 = initlist[i] while vw.xmin < x0 < vw.xmax: if colorlist == "rainbow": c = Color.rbow( 2 * pi * (x0 - vw.xmin) / (vw.xmax - vw.xmin), 2) k1 = f(x0, y0) k2 = f(x0 + h1 / 2, y0 + h1 * k1 / 2) k3 = f(x0 + h1 / 2, y0 + h1 * k2 / 2) k4 = f(x0 + h1, y0 + h1 * k3) x1, y1 = x0 + h1, y0 + (h1 / 6) * (k1 + 2 * k2 + 2 * k3 + k4) data.line((x0, y0), (x1, y1), c) x0, y0 = x1, y1 return data.save(filename, save)
def slopefield(f, vw=ViewWindow(), spread=20, color=0x0000FF, filename="slopefield" + str(int(time())), save=True): tmp = vw.thick vw.thick = 0 data = PlotData(vw) cartspreadx = vw.xpxcart(spread) - vw.xpxcart(0) cartspready = vw.ypxcart(spread) - vw.ypxcart(0) for i in range(vw.dimx): for j in range(vw.dimy): if i % spread == 0 and j % spread == 0: theta = atan(f(*vw.xpxcart((i, j)))) data.line((vw.xpxcart(i) + cartspreadx * cos(theta) / 4, vw.ypxcart(j) - cartspready * sin(theta) / 4), (vw.xpxcart(i) - cartspreadx * cos(theta) / 4, vw.ypxcart(j) + cartspready * sin(theta) / 4), color) vw.thick = tmp return data.save(filename, save)
def rk4(f, initlist, vw=ViewWindow(), h=.001, colorlist=None, sf=False, sfspread=20, sfcolor=0x0000FF, filename="rk4" + str(int(time())), save=True): data = PlotData(vw) if sf: data.data = list(slopefield(f, vw, sfspread, sfcolor, save=False).getdata()) if colorlist is None: colorlist = Color.colors(len(initlist)) for i in range(len(initlist)): c = colorlist[i] for h1 in [h, -h]: x0, y0 = initlist[i] while vw.xmin < x0 < vw.xmax: if colorlist == "rainbow": c = Color.rbow(2 * pi * (x0 - vw.xmin) / (vw.xmax - vw.xmin), 2) k1 = f(x0, y0) k2 = f(x0 + h1 / 2, y0 + h1 * k1 / 2) k3 = f(x0 + h1 / 2, y0 + h1 * k2 / 2) k4 = f(x0 + h1, y0 + h1 * k3) x1, y1 = x0 + h1, y0 + (h1 / 6) * (k1 + 2 * k2 + 2 * k3 + k4) data.line((x0, y0), (x1, y1), c) x0, y0 = x1, y1 return data.save(filename, save)