# Arc Length

## 22 seconds ago by admin

x = var('x')
f = 1/15 + (1/40) * cos( x^2-pi/2 ) a = 0 b = pi P = plot( f, (x, a, b) ) P.set_axes_range( a, b, 0, 0.12 ) P

def PlotArcLengthWithSegments( f, x, a, b, n ): P = plot( f, (x,a,b), color="blue",alpha=1,thickness=2.5); xvals = [ a + (b-a) * i/n for i in range(n+1) ]; lA = 0; maxy = max( f(x=xv) for xv in [ a + (b-a) * i/400 for i in range(400) ] ); right_angle_segments = []; for i in range(n): right_angle_segments.append( (xvals[i+0], f(x=xvals[i])) ) right_angle_segments.append( (xvals[i+1], f(x=xvals[i])) ) right_angle_segments.append( (xvals[n], f(x=xvals[n])) ) P += line([(xv, f(x=xv)) for xv in xvals], color="green",alpha=0.5,thickness=5); P += line(right_angle_segments, color="green",alpha=0.5,thickness=5); for i in range(n): lA += sqrt( ((b-a)/n)^2 + (f(x=xvals[i+1]) - f(x=xvals[i]))^2 ); P += point( [ (xv, f(x=xv)) for xv in xvals ], color="green", size=75 ); P += text( "Segment Approx: %2.4lf" % lA, (a + (b-a)/2, maxy*(11/10)), color="green", horizontal_alignment="center"); P.set_axes_range(a,b, 0, maxy*12/10) return P;
PlotArcLengthWithSegments( f, x, 1, 3, 4 )
def PlotArcLengthWithAngles( f, x, a, b, n ): df = diff(f, x); P = plot( f, (x,a,b), color="blue",alpha=1,thickness=2.5); xvals = [ a + (b-a) * i/n for i in range(n+1) ]; maxy = max( f(x=xv) for xv in [ a + (b-a) * i/400 for i in range(400) ] ); lA = 0 for i in range(n): P += line( [\ (xvals[i], f(x=xvals[i])), \ (xvals[i+1], f(x=xvals[i])), \ (xvals[i+1], f(x=xvals[i]) + (xvals[i+1]-xvals[i])*df(x=xvals[i])), \ (xvals[i], f(x=xvals[i])) \ ], color="red",alpha=0.5,thickness=5); for i in range(n): lA += sqrt( ((b-a)/n)^2 + (df(x=xvals[i])*(b-a)/n)^2 ); P += point( [ (xv, f(x=xv)) for xv in xvals ], color="red", size=75 ); P += text( "Angle Approx: %2.4lf" % lA, (a + (b-a)/2, maxy*(11.25/10)), color="red", horizontal_alignment="center"); P.set_axes_range(a,b, 0, maxy*12/10) return P;
PlotArcLengthWithAngles( f, x, a, b, 4 )
def AnimateArcLength( f, x, a, b, nrange, use_segments=True, use_angles=False ): Plist = []; if use_segments: for n0 in nrange: Plist.append( PlotArcLengthWithSegments( f, x, a, b, n0 ) ); if use_angles: for n0 in nrange: Plist.append( PlotArcLengthWithAngles( f, x, a, b, n0 ) ); return animate(Plist)
A = AnimateArcLength( f, x, a, b, [1,5,10,20,40,80,160] )
A.show(delay=60)

P = PlotArcLengthWithSegments( f, x, a, b, 5 ) P.show() A = AnimateArcLength( f, x, a, b,[5,10,20,40,80,160] ) A.show(delay=90)
P = PlotArcLengthWithAngles( f, x, a, b, 5 ) P.show() A = AnimateArcLength( f, x, a, b, [5,10,20,40,80,160], use_segments=False, use_angles=True ) A.show(delay=90)
P = PlotArcLengthWithAngles( f, x, a, b, 5 ) + PlotArcLengthWithSegments( f, x, a, b, 5 ) P.show()
P = PlotArcLengthWithAngles( f, x, a, b, 20 ) + PlotArcLengthWithSegments( f, x, a, b, 20 ) P.show()
P = PlotArcLengthWithAngles( f, x, a, b, 50 ) + PlotArcLengthWithSegments( f, x, a, b, 50 ) P.show()
P = PlotArcLengthWithAngles( f, x, a, b, 500 ) + PlotArcLengthWithSegments( f, x, a, b, 500) P.show()