diff --git a/src/derivative_operators/derivative_operator.jl b/src/derivative_operators/derivative_operator.jl index 36ed4c6f4..34cd3f470 100644 --- a/src/derivative_operators/derivative_operator.jl +++ b/src/derivative_operators/derivative_operator.jl @@ -20,7 +20,7 @@ struct CenteredDifference{N} end function CenteredDifference{N}(derivative_order::Int, approximation_order::Int, dx::T, len::Int, coeff_func=nothing) where {T<:Real,N} - + @assert approximation_order>1 "approximation_order must be greater than 1." stencil_length = derivative_order + approximation_order - 1 + (derivative_order+approximation_order)%2 boundary_stencil_length = derivative_order + approximation_order dummy_x = -div(stencil_length,2) : div(stencil_length,2) @@ -30,8 +30,8 @@ function CenteredDifference{N}(derivative_order::Int, deriv_spots = (-div(stencil_length,2)+1) : -1 boundary_deriv_spots = boundary_x[2:div(stencil_length,2)] - stencil_coefs = convert(SVector{stencil_length, T}, calculate_weights(derivative_order, zero(T), dummy_x)) - _low_boundary_coefs = SVector{boundary_stencil_length, T}[convert(SVector{boundary_stencil_length, T}, calculate_weights(derivative_order, oneunit(T)*x0, boundary_x)) for x0 in boundary_deriv_spots] + stencil_coefs = convert(SVector{stencil_length, T}, (1/dx^derivative_order) * calculate_weights(derivative_order, zero(T), dummy_x)) + _low_boundary_coefs = SVector{boundary_stencil_length, T}[convert(SVector{boundary_stencil_length, T}, (1/dx^derivative_order) * calculate_weights(derivative_order, oneunit(T)*x0, boundary_x)) for x0 in boundary_deriv_spots] low_boundary_coefs = convert(SVector{boundary_point_count},_low_boundary_coefs) high_boundary_coefs = convert(SVector{boundary_point_count},reverse(SVector{boundary_stencil_length, T}[reverse(low_boundary_coefs[i]) for i in 1:boundary_point_count])) diff --git a/test/error_benchmarking.jl b/test/error_benchmarking.jl new file mode 100644 index 000000000..5753b537a --- /dev/null +++ b/test/error_benchmarking.jl @@ -0,0 +1,28 @@ +using DiffEqOperators, Plots + +n = 10000 +@show n +x = range(0.0; length = n, stop = 2π) +dx = x[2]-x[1] +y = exp.(π*x) +y_ = y[2:(end-1)] + +for dor in 1:4, aor in 2:6 + + D1 = CenteredDifference(dor,aor,dx,length(x)) + + #test result + @show dor + @show aor + #take derivatives + err_abs = abs.(D1*y .- (π^dor)*y_) + err_percent = 100*err_abs./abs.(((π^dor)*y_)) + max_error = maximum(err_percent) # test operator with known derivative of exp(kx) + avg_error = sum(err_percent)/length(err_percent) + + @show max_error + @show avg_error + plot(x[2:(end-1)], err_percent, title="Percentage Error, n=$n aor = $aor, dor = $dor") + + #test result +end