I'm new to VBA and am trying to make a program to solve the Taylor Series Expansion of Sin(x).
There is a '<Type Mismatch>' error on the line:
"deriv = ((h ^ i) * PLUSorMINUS * EVENorODD) / (Fact(i))"
It says "Fact(i)" is the problem. Any help would be appreciated.
Sub fsolver()
xLOW = 0.01
xHIGH = 1
imax = 100
Es = 0.0005
x = 0
h = x
i = 1
Ea = 0.01
EVENorODD = Cos(x)
deriv = 0
fSIN = 0
PLUSorMINUS = 1
Fact = Application.WorksheetFunction.Fact(i)
isEven = Application.WorksheetFunction.isEven(i)
Do
xOLD = x
x = (xLOW + xHIGH) / 2
h = x
If Ea <> 0 Then
Ea = Abs((x - xOLD) / (x)) * 100
End If
deriv = ((h ^ i) * PLUSorMINUS * EVENorODD) / (Fact(i))
fSIN = Sin(x) + deriv
fLOW = fSIN(xLOW)
fHIGH = fSIN(xHIGH)
ABOVEorBELOW = fLOW * fHIGH
If ABOVEorBELOW < 0 Then
xHIGH = x
Else
If ABOVEorBELOW > 0 Then
xLOW = x
End If
End If
Cells(1, 5) = "x"
Cells(i + 1, 5) = x
Cells(1, 6) = "Ea"
Cells(i + 1, 6) = Ea
Cells(1, 3) = "Result" 'fSIN value
Cells(i + 1, 3) = fSIN
Cells(1, 4) = "fLOW*fHIGH above or below 0"
Cells(i + 1, 4) = ABOVEorBELOW
Cells(1, 2) = "Truncated value" 'deriv value
Cells(i + 1, 2) = deriv
Cells(1, 1) = "i"
Cells(1 + i, 1) = i
i = i + 1
If isEven(i) = True Then
EVENorODD = Sin(x)
Else: EVENorODD = Cos(x)
End If
If i = [1,4,5,8,9] = True Then
PLUSorMINUS = (1)
Else: PLUSorMINUS = (-1)
End If
If Es > Ea Or i > imax Then Exit Do
Loop
End Sub
Bookmarks