the cow and the silo redux

I really need to leave Sage Notebook alone and study, but I felt that I could improve the “cow” application:

Click to see the animation

here is the code:

# an application to plot "the cow tied to a silo" problem.

from sage.plot.line import Line
from sage.plot.arc import Arc

# the radius of the silo
r = 1

@interact
def _( r = slider( [1..25],1, label = "r = Silo Radius") ):  

#the area labels
    labl1 = text(('$ \\frac{A_1}{2} $'), (2.5*r,1.5*r),fontsize=20,color='black') 
    labl2 = text(('$A_2$'),(.5*r,1.5*r),fontsize=20, color='blue')
    labl3 = text(('$A_2 = \\frac{1}{2} \int_0^\pi (\pi r - r \\theta)^2 d \\theta$'),(-r*1.2,3.1*r),fontsize=14, color='blue')
    labl5 = text(('$\\theta$'),(.1*r,.1*r), color='red', fontsize=16)
    labl7 = text(('$r_2 = \pi r - r \\theta$'),(-r*1.6,2.7*r),fontsize=14, color='blue')
# x and y on the silo     
    def x1(t): return r*cos((t))
    def y1(t): return r*sin((t))

#the area in the half-circle
    f = sqrt( (pi*r)^2 - (x -r)^2 ) 
    L1 = plot(f, r, r*(pi+1), fill=True, color="green", thickness=3) + labl1

# the silo
    L2 = parametric_plot( (x1(t), y1(t) ), (t, 0, float(pi)), color='green')

    def silo_rad(a):
        # line = (y-0)= M(x-0)

        def x2(a) : 
            new_x = x1(a) - abs( r*(pi-a)*sin(a) ) 
            return new_x

        def y2(a) : 
            new_y = y1(a) + ( r*(pi-a)*cos(a) )
            return new_y
     # the radius of the silo
        s_rad = line( ( ( ( 0,0 ) ),( x1(a), y1(a) ) ), color="red" ) 
     # the tangent line of the taught rope
        s_tan = line( ( (  x1(a), y1(a) ) , ( ( x2(a),y2(a) ) ) ) )
        labl6 = text(('$r_2$'),( (.5*r*(x1(a)+x2(a))+.2*cos(a)) ,(.5*r*(y1(a)+y2(a))+.2*sin(a)) ),fontsize=16)

        cow = parametric_plot( (x2(t), y2(t) ), (t, 0, float(pi)), color='green', thickness=3)
        labl4 = text( ('$r$'),( (.5*r*(x1(a)-abs(.2*sin(a))) ),(.5*r*(y1(a)+.2*cos(a)))), color='red', fontsize=16 )  

        return s_rad + s_tan + cow + labl2 + labl3 + labl4 + labl5 + labl6 + labl7

# the area in the half-circle
    def A1(a) : 
        return round( (pi^3)*(a^2)/2, 4)

# twice thearea of the integral
    def A2(a) : 
        return round( (pi^3)*(a^2)/3, 4)

# point of tangent to silo
    def f(a):
        p1 = point( ( x1(a), y1(a) ) )
        return L1 + L2 + p1 + silo_rad(a)

    print( html('$A_1= \\frac{\pi^3 r^2}{2} {\\approx} $' + str(A1(r) ) + '$\hspace{12 mm}2 \\times A_2 = \\frac{\pi^3 r^2}{3} {\\approx} $' + str(A2(r)) + '$\hspace{12 mm}Total\ Area =  \\frac{5\pi^3 r^2}{6} {\\approx} $' + str(A2(r) + A1(r)) ) )

    a1 = animate([f(i) for i in srange(0,float(pi),.1)]  , xmin = float(-2*r), xmax = float((1.1+pi)*r), ymin = float(-.2), ymax = float(1.1*pi*r))

    a1.gif(savefile='my_animation.gif', show_path=False )
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s