-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewton-raphson.jl
More file actions
52 lines (41 loc) · 1.41 KB
/
Copy pathnewton-raphson.jl
File metadata and controls
52 lines (41 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using ForwardDiff
# Function to find first derivative of a function in point x
function d(f::Function, x::T) where {T <: Real}
return ForwardDiff.derivative(f, x);
end
"""
function Newton_Raphson(f::Function, x::Real)
In numerical analysis, the Newton–Raphson method, also known simply as Newton's method,
named after Isaac Newton and Joseph Raphson, is a root-finding algorithm which produces
successively better approximations to the roots (or zeroes) of a real-valued function.
# Arguments:
- `f` : A real-valued function
- `x` : Real value representing starting point
- 'criteria' : A function which checks whether we obtained an acceptable solution.
Accepts current root estimate and iteration
# Examples:
```julia
function y(x)
return (x-1)^2 - 4;
end
println(Newton_Raphson(y, -2)) # ~ -1
println(Newton_Raphson(y, 5)) # ~ 3
```
# Overloads:
"""
function Newton_Raphson(f::Function, x::Real)
return Newton_Raphson(f,
x,
(x,i) -> return abs(f(x)) < 0.001 || i > 10;)
end
function Newton_Raphson(f::Function, x::Real, criteria::Function)
i = 0; # Iteration counter
xi = x; # Solution in i-th iteration
x_best = x; # Best of all solutions
while !criteria(xi, i)
i+=1;
xi = xi - f(xi)/d(f,xi);
x_best = (abs(f(xi)) <= abs(f(x_best))) ? xi : x_best;
end
return x_best
end