-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.jl
57 lines (38 loc) · 1013 Bytes
/
basic.jl
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
53
54
55
56
57
#Basic function to use
"""
absolute_error(x, x̂)
return the Absolute error of x and x̂
"""
absolute_error(x, x̂) = abs(x .- x̂)
"""
relative_error(x, x̂)
The relative error between X1 and X2
"""
relative_error(x, x̂) = abs((x .- x̂) ./ x)
"""
ξ(x₀, x)
return the number ξ(x) bwteen x₀ and x.
"""
@inline ξ(x₀, x) = rand(1)[1] * (x - x₀)/x + x₀
"""
NthDrivative(f::Function, x::Real, n::Int)
return the nth derivative value at x
"""
@inline function NthDerivative(f::Function, x::Real, n::Int)
if n == 0
return f(x)
else
return ForwardDiff.derivative(x->NthDerivative(f, x, n-1), x)
end
end
"""
TaylorPolynomials(f::Function, x::Real, x₀::Real, n::Int)
return the value of nth Taylor Ploynomial at x.
"""
@inline function TaylorPolynomials(f::Function, x::Real, x₀::Real, n::Int)
result = []
for i in 0:n
push!(result, NthDerivative(f, x₀, i)*(x - x₀)^i/factorial(i))
end
return sum(result)
end