I used an initial guess of 6 for exp(x)-3*x^2 and 1e-16 as the tolerance for x. I also made another code version where the tolerance is applied to abs(f(x)).
Here is the Excel VBA code that I used to test for convergence of the guess refinment:
Code:
Option Explicit
Function Fx(ByVal X As Double) As Double
Fx = Exp(X) - 3 * X ^ 2
End Function
Sub go()
Dim X As Double, h As Double, diff As Double, toler As Double
Dim f0 As Double, fp As Double, fm As Double
Dim Deriv1 As Double, Deriv2 As Double
Dim row As Integer
Range("B2:Z10000").Clear
' Newton's method
X = [A2].Value
toler = [A4].Value
row = 2
Do
h = 0.001 * (1 + Abs(X))
f0 = Fx(X)
diff = h * f0 / (Fx(X + h) - f0)
X = X - diff
Cells(row, 2) = Fx(X)
Cells(row, 3) = X
row = row + 1
Loop Until Abs(diff) < toler Or row > 100
'Newton's method using fx(x) approximation
X = [A2].Value
toler = [A4].Value
row = 2
Do
h = 0.001 * (1 + Abs(X))
fp = Fx(X + h)
fm = Fx(X - h)
diff = (fp + fm) / (fp - fm) * h
X = X - diff
Cells(row, 4) = Fx(X)
Cells(row, 5) = X
row = row + 1
Loop Until Abs(diff) < toler Or row > 100
MsgBox diff
' Halley's method
X = [A2].Value
toler = [A4].Value
row = 2
Do
h = 0.001 * (1 + Abs(X))
f0 = Fx(X)
fp = Fx(X + h)
fm = Fx(X - h)
Deriv1 = (fp - fm) / 2 / h
Deriv2 = (fp - 2 * f0 + fm) / h ^ 2
diff = 2 * f0 * Deriv1 / (2 * Deriv1 ^ 2 - f0 * Deriv2)
X = X - diff
Cells(row, 6) = Fx(X)
Cells(row, 7) = X
row = row + 1
Loop Until Abs(diff) < toler Or row > 100
' Halley's method using fx(x) approximation
X = [A2].Value
toler = [A4].Value
row = 2
Do
h = 0.001 * (1 + Abs(X))
fp = Fx(X + h)
fm = Fx(X - h)
f0 = (fp + fm) / 2
Deriv1 = (fp - fm) / 2 / h
Deriv2 = (fp - 2 * f0 + fm) / h ^ 2
diff = 2 * f0 * Deriv1 / (2 * Deriv1 ^ 2 - f0 * Deriv2)
X = X - diff
Cells(row, 8) = Fx(X)
Cells(row, 9) = X
row = row + 1
Loop Until Abs(diff) < toler Or row > 100
End Sub
Here is the output with calculated fx(x):
Code:
Fx X
100.0016498 5.198995948
31.47661127 4.534269054
8.480862488 4.058729991
1.53371327 3.806921961
0.096624566 3.738034628
0.000853428 3.733122997
3.75091E-06 3.733079222
1.63358E-08 3.733079029
Here is the output with approximated value of fx(x):
Code:
Fx X
99.54394976 5.195937045
31.16026661 4.529457029
8.320740809 4.053939407
1.470250565 3.804052193
0.083803449 3.737379665
-7.03483E-05 3.733075404
-0.000401073 3.733058364
-0.00040107 3.733058364
The second set shows reduced accuracy in the refined guess for the root.
Here is the Excel VBA code that I used to test the abs(fx(x)):
Code:
Option Explicit
Function Fx(ByVal X As Double) As Double
Fx = Exp(X) - 3 * X ^ 2
End Function
Sub go()
Dim X As Double, h As Double, diff As Double, toler As Double
Dim f0 As Double, fp As Double, fm As Double
Dim Deriv1 As Double, Deriv2 As Double
Dim row As Integer
Range("B2:Z10000").Clear
X = [A2].Value
toler = [A4].Value
row = 2
Do
h = 0.001 * (1 + Abs(X))
f0 = Fx(X)
diff = h * f0 / (Fx(X + h) - f0)
X = X - diff
Cells(row, 2) = Fx(X)
Cells(row, 3) = X
row = row + 1
Loop Until Abs(Fx(X)) < toler Or row > 100
X = [A2].Value
toler = [A4].Value
row = 2
Do
h = 0.001 * (1 + Abs(X))
fp = Fx(X + h)
fm = Fx(X - h)
diff = (fp + fm) / (fp - fm) * h
X = X - diff
Cells(row, 4) = Fx(X)
Cells(row, 5) = X
row = row + 1
Loop Until Abs(Fx(X)) < toler Or row > 100
' Halley's method
X = [A2].Value
toler = [A4].Value
row = 2
Do
h = 0.001 * (1 + Abs(X))
f0 = Fx(X)
fp = Fx(X + h)
fm = Fx(X - h)
Deriv1 = (fp - fm) / 2 / h
Deriv2 = (fp - 2 * f0 + fm) / h ^ 2
diff = 2 * f0 * Deriv1 / (2 * Deriv1 ^ 2 - f0 * Deriv2)
X = X - diff
Cells(row, 6) = Fx(X)
Cells(row, 7) = X
row = row + 1
Loop Until Abs(Fx(X)) < toler Or row > 100
' Halley's method 2
X = [A2].Value
toler = [A4].Value
row = 2
Do
h = 0.001 * (1 + Abs(X))
fp = Fx(X + h)
fm = Fx(X - h)
f0 = (fp + fm) / 2
Deriv1 = (fp - fm) / 2 / h
Deriv2 = (fp - 2 * f0 + fm) / h ^ 2
diff = 2 * f0 * Deriv1 / (2 * Deriv1 ^ 2 - f0 * Deriv2)
X = X - diff
Cells(row, 8) = Fx(X)
Cells(row, 9) = X
row = row + 1
Loop Until Abs(Fx(X)) < toler Or row > 100
End Sub
Again, the results I get tell me that approximating f(x) with (f(x+h)+f(x+h))/2 reduces the accuracy of the result.
We can use any version that fit our conviction.