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:

h ttp://upload.wikimedia.org/math/5/d/3/5d32785481dec2be0c115f510f164dd1.png


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)
AddIt = x(I)
Fact = Fact + 2
While AddIt < 0.00000001
sine = AddIt * x(I) * x(I) / Fact / Fact - 1
AddIt = Abs(AddIt) - 1
x(I + 1) = x(I)
Wend



End Function


my function is not working
i need URGENT HELP... Please help me><

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
Please read this from FAQ (http://www.vbaexpress.com/forum/faq.php?faq=psting_faq_item#faq_hom_faq_item)

johnske
04-23-2007, 01:17 PM
So it's homework is it? We don't do homework.