Attribute VB_Name = "Module2" '************************************************************** '* Approximating a function F(x) By Maclaurin's Series * '* ---------------------------------------------------------- * '* This is the general Maclaurin's formula: * '* * '* f'(0) f"(0) f"'(0) fn-1(0) * '* f(x) = f(0) + ---- + ---- + ----- + ... + ------- + ... * '* 1! 2! 3! (n-1)! * '* * '* In the case of sin(x) between x=0 and x=2*PI, the formula * '* becomes: * '* * '* sin(x) = x - x^3/3! + x^5/5! - x^7/7! + x^9/9! ... * '* * '* This small program shows that with 9 terms, we have a fair * '* approximation of sin(x) between 0 and 2*PI. * '* ---------------------------------------------------------- * '* REFERENCE: * '* "Graphisme dans le plan et dans l'espace avec Turbo * '* Pascal 4.0 By R. Dony - MASSON, Paris 1990". * '* * '* Visual Basic Version By J-P Moreau, Paris. * '************************************************************** DefDbl A-H, O-Z DefInt I-N Const B = 6.29 '2PI Const step = 0.05 Dim Nbterms As Integer Dim Example As Integer Dim Y(256) Function Func(x) If Example = 1 Then Func = Sin(x) Else Func = Exp(x) End If End Function Function Fact(N) If N < 1 Then Fact = 1# Exit Function End If tmp = 1# For i = 1 To N tmp = tmp * i Next i Fact = tmp End Function Sub Data() Example = Val(Form1.Text2) Nbterms = Val(Form1.Text1) End Sub Sub DrawFunc() If Example = 1 Then XI = 0# Else XI = -4# End If While XI <= B MoveXY XI, Func(XI) LineXY 1.01 * XI, 1.01 * Func(XI) XI = XI + step Wend End Sub Sub DrawFuncMaclaurin(Nbterms) Dim s As String For i = 1 To 256 Y(i) = 0# Next i ID = -1 For iterm = 1 To Nbterms icpt = 1 If Example = 1 Then ID = ID + 2 x = 0# Else ID = ID + 1 x = -4 End If T = x ^ ID / Fact(ID) If Example = 1 Then If (iterm Mod 2) <> 0 Then Y(icpt) = Y(icpt) + T Else Y(icpt) = Y(icpt) - T End If Else Y(icpt) = Y(icpt) + T End If MoveXY x, Y(icpt) While x <= B x = x + step T = x ^ ID / Fact(ID) icpt = icpt + 1 If Example = 1 Then If (iterm Mod 2) <> 0 Then Y(icpt) = Y(icpt) + T Else Y(icpt) = Y(icpt) - T End If Else Y(icpt) = Y(icpt) + T End If LineXY x, Y(icpt) Wend Next iterm s = Str$(iterm - 1) + " terms." Display 800, 200, s End Sub Sub Exec_Macfunc() Data Form1.Cls MaxX = 11500: MaxY = 8500 If Example = 1 Then Fenetre 0, 6, -2, 2 Else Fenetre -4, 2, -2, 8 End If Cloture 600, MaxX - 10, 200, MaxY - 100 Axes Graduate 1, 1 Grid 0.5, 0.5 Bordure DrawFunc DrawFuncMaclaurin Nbterms End Sub