I have problem in my listcode. In my code, the function f(x) was repeating over and over again, while I just need f(x1) and f(x2).
Here is the detail
Function f(x)
f(x) = tan(x) - ((Cells(16, 2).Value) * (Cells(8, 6).Value)) / (2 * (Cells(4, 2).Value) * (Cells(4, 2).Value) * Cos(x) * Cos(x))
End Function
Sub SolusiNumSudutElevasi()
Dim xm, ym As Double
Dim g, pi As Double
Dim v0, x1, x2, xS As Double
Dim slope, error, i As Double
error = 0.000001
g = Cells(16, 2).Value
pi = Cells(15, 2).Value
v0 = Cells(4, 2).Value
xm = Cells(8, 6).Value
ym = Cells(9, 6).Value
'memasukkan x0,x1
x1 = Cells(13, 6).Value / 180 * pi
x2 = Cells(14, 6).Value / 180 * pi
'hitung slope
slope = (f(x1) - f(x2)) / (x1 - x2)
'hitung x2, setup counter iterasi
xS = x1 - (f(x1) / slope)
i = 0
'Bila f(theta) = 0 maka theta adalah akarnya
'Bila tidak, cari dengan metode Secant
If f(x1) = 0 Then
Cells(15, 6).Value = x1
ElseIf f(x2) = 0 Then
Cells(15, 6).Value = x2
ElseIf f(xS) = 0 Then
Cells(15, 6).Value = xS
Else
Do While (Abs((xS - x2) / xS) > error) And (f(xS) <> 0)
x1 = x2
x2 = xS
i = i + 1
slope = (f(x1) - f(x2)) / (x1 - x2)
xS = x1 - (f(x1)) / slope
Loop
Cells(15, 6).Value = xS
End If
End Sub
In my code, the function is never ending repeating. WHat is wrong? Thanks