SubName: Newton
Author: Newton & Raphson
Description: Find root of a function
The attached algorithm finds a root of a single argument function (with limitations on the type of function).
It finds one root depending on the starting point which is an input to the algorithm.
The attached function sub is an example.
Tags: root, function, Newton
Author: Newton & Raphson
Description: Find root of a function
The attached algorithm finds a root of a single argument function (with limitations on the type of function).
It finds one root depending on the starting point which is an input to the algorithm.
The attached function sub is an example.
B4X:
Sub Newton(x As Double) As Double
Dim tolerance As Double = .000000001 ' Stop If you're close enough
Dim max_count As Int = 200 ' Maximum number of iterations
Dim dx As Double = 0.0001
For count = 1 To max_count
If Abs(function(x)) > tolerance Then
x = x - 2*dx*function(x)/ (function(x + dx) - function(x -dx))
Else
Return x
End If
Next
Log("Failed to find a zero")
Return -999
End Sub
Sub function(x As Double) As Double
Return Power(x,4)-5*x*x-x+1
End Sub
Tags: root, function, Newton
Last edited: