PDA

View Full Version : [[URGENT]] VBA help with the Taylor Series on Trigonometry

wongccw
04-23-2007, 04:43 AM
Hello, I am new to this forum and I have an urgent problem to be solved. I hope someone here can help me out in writing a VBA While Loop on the Sine x function of the Taylor Series.

the function that i need to write is something like this:

So far I have written a few lines. And my program is like the following:

Function sum()

Dim x() As Double
Dim sine As Double
Dim I As Integer
Dim cell As Range
Dim ValRange As Range

x() = Cells(1, 1).Value

Cells(1, 1).Value = x(1)
Cells(2, 1).Value = x(2)
Cells(3, 1).Value = x(3)
Cells(4, 1).Value = x(4)
Cells(5, 1).Value = x(5)
Cells(6, 1).Value = x(6)

sine = x(I)
Fact = Fact + 2
sine = AddIt * x(I) * x(I) / Fact / Fact - 1
x(I + 1) = x(I)
Wend

End Function

my function is not working

thanks

johnske
04-23-2007, 05:33 AM
Without testing fully - try...

Option Explicit

Sub SinXSeriesExpansion()

Dim M As Long, N As Long, X As Double, SineX As Double

X = InputBox("What is X?")
M = InputBox("How many terms?")

If IsNumeric(M) And M > 0 Then
SineX = 0
On Error GoTo OverFlow
For N = 0 To M \ 1
SineX = SineX + ((-1) ^ (N) * X ^ (2 * N + 1)) / Evaluate("Fact(" & 2 * N + 1 & ")")
Range("A" & N + 1) = SineX
Next
MsgBox SineX
Else
MsgBox "Need the number of terms to be greater than zero!"
End If
Exit Sub

OverFlow:
MsgBox "Too many terms - try fewer terms", , "OverFlow Error!!"
End Sub

wongccw
04-23-2007, 09:09 AM
sorry
but can i hav it as a While Loop program?
coz my lecturer is asking me for a while loop...><
Thank you alot for helping me out...><

mdmackillop
04-23-2007, 09:18 AM