x^2-sin(x)-1 의 해를 이분법으로 구해야하는데요..오류가 떠서 질문해봐요 ..ㅠ
Option Explicit
Function f(x)
Dim ratio As Double, pi As Double
ratio = 0.1
pi = Application.WorksheetFunction.pi()
pi = 3.141592
f = x * x - Sin(x) - 1
End Function
Function Bisection(a, b, eps, MaxIt, x)
Dim c As Double, fa As Double, fb As Double, fc As Double
Dim It As Integer
fa = f(a)
fb = f(b)
If fa * fb >= 0 Then
MsgBox ("Wrong [a,b]")
x = 0
Bisection = 0
Exit Function
End If
For It = 1 To MaxIt
c = (a + b) / 2
fc = f(c)
If fa * fc < 0 Then
b = c
fb = fc
ElseIf fa * fc > 0 Then
a = c
fa = fc
Else
Exit For
End If
If (b - a) < eps Then
End If
Next It
x = c
Bisection = It
End Function
Sub Main()
Dim a As Double, b As Double, th As Double, height As Double, eps As Double
Dim MaxIter As Integer, It As Integer
Dim x As Double
a = Application.WorksheetFunction.Radians(0)
b = Application.WorksheetFunction.Radians(180)
eps = 0.00000001
MaxIter = 100
It = Bisection(a, b, eps, MaxIter, th)
If It > MaxIter Or It = 0 Then
MsgBox ("Error! it=" & It)
Exit Sub
End If
MsgBox ("x= " & x)
End Sub
댓글 0