sliders

3668 days ago by calkin

plot(sin(3*x),x) 
       
 
       
def bisect_method(f, a, b, eps): try: f = f._fast_float_(f.variables()[0]) except AttributeError: pass intervals = [(a,b)] two = float(2); eps = float(eps) while True: c = (a+b)/two fa = f(a); fb = f(b); fc = f(c) if abs(fc) < eps: return c, intervals if fa*fc < 0: a, b = a, c elif fc*fb < 0: a, b = c, b else: raise ValueError, "f must have a sign change in the interval (%s,%s)"%(a,b) intervals.append((a,b)) html("<h1>Double Precision Root Finding Using Bisection</h1>") @interact def _(f = cos(x) - x, a = float(0), b = float(1), eps=(-3,(-16..-1))): eps = 10^eps print "eps = %s"%float(eps) try: time c, intervals = bisect_method(f, a, b, eps) except ValueError: print "f must have opposite sign at the endpoints of the interval" show(plot(f, a, b, color='red'), xmin=a, xmax=b) else: print "root =", c print "f(c) = %r"%f(c) print "iterations =", len(intervals) P = plot(f, a, b, color='red') h = (P.ymax() - P.ymin())/ (1.5*len(intervals)) L = sum(line([(c,h*i), (d,h*i)]) for i, (c,d) in enumerate(intervals) ) L += sum(line([(c,h*i-h/4), (c,h*i+h/4)]) for i, (c,d) in enumerate(intervals) ) L += sum(line([(d,h*i-h/4), (d,h*i+h/4)]) for i, (c,d) in enumerate(intervals) ) show(P + L, xmin=a, xmax=b) 
       

Click to the left again to hide and once more to show the dynamic interactive window

html('<h2>Tangent line grapher</h2>') @interact def tangent_line(f = input_box(default=sin(x)), xbegin = slider(0,10,1/10,0), xend = slider(0,10,1/10,10), x0 = slider(0, 1, 1/100, 1/2)): prange = [xbegin, xend] x0i = xbegin + x0*(xend-xbegin) var('x') df = diff(f) tanf = f(x0i) + df(x0i)*(x-x0i) fplot = plot(f, prange[0], prange[1]) print 'Tangent line is y = ' + tanf._repr_() tanplot = plot(tanf, prange[0], prange[1], rgbcolor = (1,0,0)) fmax = f.find_maximum_on_interval(prange[0], prange[1])[0] fmin = f.find_minimum_on_interval(prange[0], prange[1])[0] show(fplot + tanplot, xmin = prange[0], xmax = prange[1], ymax = fmax, ymin = fmin) 
       

Click to the left again to hide and once more to show the dynamic interactive window

@interact def julia_plot(expo = slider(-10,10,0.1,2), \ iterations=slider(1,100,1,30), \ c_real = slider(-2,2,0.01,0.5), \ c_imag = slider(-2,2,0.01,0.5), \ zoom_x = range_slider(-2,2,0.01,(-1.5,1.5)), \ zoom_y = range_slider(-2,2,0.01,(-1.5,1.5))): var('z') I = CDF.gen() f(z) = z^expo + c_real + c_imag*I ff_j = fast_callable(f, vars=[z], domain=CDF) def julia(z): for i in range(iterations): z = ff_j(z) if abs(z) > 2: return z return z print 'z <- z^%s + (%s+%s*I)' % (expo, c_real, c_imag) complex_plot(julia, zoom_x,zoom_y, plot_points=200, dpi=150).show(frame=True, aspect_ratio=1) 
       

Click to the left again to hide and once more to show the dynamic interactive window