Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 96
%latex \begin{tikzpicture} \draw[fill=orange] (3,0) arc (0:90:3) -- (0,0) -- cycle; \node at (1.5,-0.5) {$3$}; \draw[step=1] (0,0) grid (3,3); \end{tikzpicture} Try changing step.
gap=1; unitarea=gap^2; x,y=gap,gap area,counter=0,0; while y<=3: if x^2+y^2<=9: counter+=1; area+=unitarea; x+=gap; else: y+=gap; x=gap; print counter,"*",unitarea,"=",area print "Real answer:", N(9*pi/4); print "Try changing gap"
4 * 1 = 4 Real answer: 7.06858347057703 Try changing gap
n=5; x=var("x") f=(sin(x)).function(x); area,pic=integration_approx(f,0,pi,n); show(pic,title="sin(x), x in [0,pi]") print " " print "Divided into %s segments gives the area %s"%(n,N(area)); print "Real answer:", integral(f,(x,0,pi)); print "Try changing n"
Divided into 5 segments gives the area 1.93376559809280 Real answer: 2 Try changing n
x=var("x") f=(x^2).function(x); pic=f.plot(xmin=0,xmax=2,fill=True,figsize=3); show(pic)
#Run this cell before all cells def integration_approx(f,a,b,pieces): """ Input: f: function defined on [a,b]; pieces: number of pieces; Output: By dividing [a,b] into "pieces" pieces, find the area under f. Return area, picture; """ gap=(b-a)/pieces; base=[(a+i*gap,a+i*gap+gap) for i in range(pieces)]; height=[f(a+i*gap) for i in range(pieces)]; area=gap*sum(height); pic=f.plot(xmin=a,xmax=b,fill=True,color="orange"); recs=sum([polygon([(base[i][0],0),(base[i][1],0),(base[i][1],height[i]),(base[i][0],height[i])],edgecolor="black",fill=False) for i in range(pieces)]) xlabel=sum([text("x%s"%i,(base[i][0],-0.2)) for i in range(pieces)]) ylabel=sum([text("f(x%s)"%i,(base[i][0],height[i]/2)) for i in range(pieces)]) return area,pic+recs+xlabel+ylabel;