# 연속체의 무게중심

## 6 days ago by namy0727

x = var('x') @interact def centroid(f = input_box(default = sqrt(1-x^2), type = SR), Start = input_box(default = -1), End = input_box(default = 1), n = slider(1,100,1,4)): pretty_print(html('$$f(x) = %s$$'%latex(f))) func(x) = f a = RDF(Start) b = RDF(End) dx = (b-a)/n mass = [] My = [] Mx = [] midxs = [q*dx+dx/2 + a for q in range(n)] midys = [func(x=midxs[q]) for q in range(n)] rects = Graphics() for q in range(n): xm = midxs[q] ym = midys[q] mass.append(dx*ym) My.append(midxs[q]*midys[q]) Mx.append(0.5*midys[q]*midys[q]) rects+= line([[xm-dx/2,0],[xm-dx/2,ym],[xm+dx/2,ym],[xm+dx/2,0]], rgbcolor = (0,1.5,0)) + point((xm,0.5*ym), rgbcolor = (0,1.5,0), size=30) min_y = min(0, find_local_minimum(func,a,b)[0]) max_y = max(0, find_local_maximum(func,a,b)[0]) My = dx*sum(My) Mx = dx*sum(Mx) m = sum(mass) xbar = My/m; xbar = RDF(xbar) ybar = Mx/m; ybar = RDF(ybar) A = integral(func,x,a,b) rMy = integral(x*func,x,a,b) rMx = integral(0.5*func*func,x,a,b) rxbar = rMy/A; rxbar = RDF(rxbar) rybar = rMx/A; rybar = RDF(rybar) xerr = abs(rxbar-xbar) yerr = abs(rybar-ybar) print("Real center of mass =", (rxbar,rybar)) print("Approximated center of mass =", (xbar,ybar)) print("Error of x-coordinate =", xerr) print("Error of y-coordinate =", yerr) p = plot(func,a,b,fill=True, fillcolor='lightblue') + rects + point((xbar,ybar), rgbcolor = (1,0,0), size=50) p.show(xmin = a, xmax = b, ymin = min_y, ymax = max_y, aspect_ratio=1)

Start
End

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