'------------------------------- ' Simpson の公式 ' Tiny Basic for Windows 用 ' Ver. 01 2002/11/04 '------------------------------- '関数式 '--------------------' DF$="1/(1+x^2)" Function F(x) F = 1/(1+x^2) End Function '------------------------- a=0 :'左点のx座標 b=1 :'右点のx座標 m=3 :'分割数 2*m '------------------------ 'グラフの範囲 '------------------------ x0=-0.5 :y0=-0.5 x1=1.5 :y1=1.5 '------------------------- 'ここまで設定する。 '------------------------- Call InitGraph(x0,y0,x1,y1) Call DrawGraph(a,b) Call DrawSimpson(a,b,m) Print "Simpson の公式による定積分の計算" Print "関数 ";DF$;" 範囲 (";a;",";b;")" Print "分割数 = ";2*m Print "赤で囲まれた部分の面積の総和=" Print Simpson(a,b,m) End '------------------------------------------- Function Simpson(a,b,m) d = (b-a)/(2*m) S = F(a) for i=1 to 2*m-1 x = a + i * d if (i mod 2)=1 then S= S + 4 * F(x) if (i mod 2)=0 then S= S + 2 * F(x) next i S = S + F(b) Simpson = S * d/3 End Function Sub DrawSimpson(a,b,m) X1 = a n=2*m d = (b-a)/n Y1 = F(X1) For i = 1 TO 2*m step 2 X2 = a + i *d Y2 = F(X2) X3 = a + (i+1) *d Y3 = F(X3) Line (X1,0)-(X1,Y1),12 Line (X2,0)-(X2,Y2),15 Call DrawPCurve(x1,x3) Line (X3,0)-(X3,Y3),12 Line (X3,0)-(X1, 0),12 X1 = X3 Y1 = Y3 next i End Sub Sub DrawPCurve(x0,x1) c=F(x0) : b=F(x1)-F(x0) a=2*(F(x0)+F(x1))-4*F((x0+x1)/2) Px0=x0 Py0=c For i=1 to 10 t=i/10 Px1=(x1-x0)*t+x0 Py1=a*t*(t-1)+b*t+c Line (Px0,Py0)-(Px1,Py1),12 Px0=Px1 Py0=Py1 Next i End Sub Sub InitGraph(ax,ay,bx,by) BackColor ="#BBDDFF" GScreen(400,400) MathGraph On Window (ax,ay)-(bx,by) ForeColor = "Black" Line (ax,0)-(bx,0) Line (0,ay)-(0,by) End Sub Sub DrawGraph(a,b) MV=Max(abs(a),abs(b)) ForeColor = "Black" X1 = a : Wd = b-a Y1 = F(X1) For I = 1 TO 100 : '100の点折れ線で描く X2 = a + Wd*I/100 : 'X1の設定 Y2 = F(X2) : 'Y1の設定 if abs(Y1)<=MV then Line (X1,Y1)-(X2,Y2) X1 = X2 Y1 = Y2 Next I End Sub