|
| 1 | +using LinearAlgebra |
| 2 | +using Printf |
| 3 | + |
| 4 | +function _polynomial(a, x) |
| 5 | + accumulator = a[end] * one(x) |
| 6 | + for i in length(a)-1:-1:1 |
| 7 | + accumulator = accumulator * x + a[i] |
| 8 | + end |
| 9 | + accumulator |
| 10 | +end |
| 11 | + |
| 12 | +# definition of polynom |
| 13 | +struct Polynom{C} |
| 14 | + coefficients::C |
| 15 | + Polynom(coefficients::CC) where CC = coefficients[end] == 0 ? throw(ArgumentError("Coefficient of the highest exponent cannot be zero.")) : new{CC}(coefficients) |
| 16 | +end |
| 17 | + |
| 18 | +# based on https://github.com/JuliaMath/Polynomials.jl |
| 19 | +function from_roots(roots::AbstractVector{T}; aₙ = one(T)) where {T} |
| 20 | + n = length(roots) |
| 21 | + c = zeros(T, n+1) |
| 22 | + c[1] = one(T) |
| 23 | + for j = 1:n |
| 24 | + for i = j:-1:1 |
| 25 | + c[i+1] = c[i+1]-roots[j]*c[i] |
| 26 | + end |
| 27 | + end |
| 28 | + return Polynom(aₙ.*reverse(c)) |
| 29 | +end |
| 30 | + |
| 31 | +(p::Polynom)(x) = _polynomial(p.coefficients, x) |
| 32 | +degree(p::Polynom) = length(p.coefficients) - 1 |
| 33 | + |
| 34 | +function _derivativeof(p::Polynom) |
| 35 | + n = degree(p) |
| 36 | + n > 1 ? Polynom([(i - 1)*p.coefficients[i] for i in 2:n+1]) : error("Low degree of a polynomial.") |
| 37 | +end |
| 38 | +LinearAlgebra.adjoint(p::Polynom) = _derivativeof(p) |
| 39 | + |
| 40 | +function Base.show(io::IO, p::Polynom) |
| 41 | + n = degree(p) |
| 42 | + a = reverse(p.coefficients) |
| 43 | + for (i, c) in enumerate(a[1:end-1]) |
| 44 | + if (c != 0) |
| 45 | + c < 0 && print(io, " - ") |
| 46 | + c > 0 && i > 1 && print(io, " + ") |
| 47 | + print(io, "$(abs(c))x^$(n - i + 1)") |
| 48 | + end |
| 49 | + end |
| 50 | + c = a[end] |
| 51 | + c > 0 && print(io, " + $(c)") |
| 52 | + c < 0 && print(io, " - $(abs(c))") |
| 53 | +end |
| 54 | + |
| 55 | +# default optimization parameters |
| 56 | +atol = 1e-12 |
| 57 | +maxiter = 100 |
| 58 | +stepsize = 0.95 |
| 59 | + |
| 60 | +# definition of optimization methods |
| 61 | +abstract type RootFindingMethod end |
| 62 | +struct Newton <: RootFindingMethod end |
| 63 | +struct Secant <: RootFindingMethod end |
| 64 | +struct Bisection <: RootFindingMethod end |
| 65 | + |
| 66 | +init!(::Bisection, p, a, b) = sign(p(a)) != sign(p(b)) ? (a, b) : throw(ArgumentError("Signs at both ends are the same.")) |
| 67 | +init!(::RootFindingMethod, p, a, b) = (a, b) |
| 68 | + |
| 69 | +function step!(::Newton, poly::Polynom, xᵢ, step_size) |
| 70 | + _, x̃ = xᵢ |
| 71 | + dp = p' |
| 72 | + x̃, x̃ - step_size*p(x̃)/dp(x̃) |
| 73 | +end |
| 74 | + |
| 75 | +function step!(::Secant, poly::Polynom, xᵢ, step_size) |
| 76 | + x, x̃ = xᵢ |
| 77 | + dpx = (p(x) - p(x̃))/(x - x̃) |
| 78 | + x̃, x̃ - stepsize*p(x̃)/dpx |
| 79 | +end |
| 80 | + |
| 81 | +function step!(::Bisection, poly::Polynom, xᵢ, step_size) |
| 82 | + x, x̃ = xᵢ |
| 83 | + midpoint = (x + x̃)/2 |
| 84 | + if sign(p(midpoint)) == sign(p(x̃)) |
| 85 | + x̃ = midpoint |
| 86 | + else |
| 87 | + x = midpoint |
| 88 | + end |
| 89 | + x, x̃ |
| 90 | +end |
| 91 | + |
| 92 | +function find_root(p::Polynom, rfm=Newton, a=-5.0, b=5.0, max_iter=100, step_size=0.95, tol=1e-12) |
| 93 | + x, x̃ = init!(rfm, p, a, b) |
| 94 | + for i in 1:maxiter |
| 95 | + x, x̃ = step!(rfm, p, (x, x̃), step_size) |
| 96 | + val = p(x̃) |
| 97 | + @printf "x = %.5f | x̃ = %.5f | p(x̃) = %g\n" x x̃ val |
| 98 | + abs(val) < atol && return x̃ |
| 99 | + end |
| 100 | + println("Method did not converge in $(max_iter) iterations to a root within $(tol) tolerance.") |
| 101 | + return x̃ |
| 102 | +end |
| 103 | + |
| 104 | +# test code |
| 105 | +poly = Polynom(rand(4)) |
| 106 | +p = from_roots([-3, -2, -1, 0, 1, 2, 3]) |
| 107 | +dp = p' |
| 108 | +p(3.0), dp(3.0) |
| 109 | + |
| 110 | +x₀ = find_root(p, Bisection(), -5.0, 5.0) |
| 111 | +x₀ = find_root(p, Newton(), -5.0, 5.0) |
| 112 | +x₀ = find_root(p, Secant(), -5.0, 5.0) |
0 commit comments