53 lines
1.1 KiB
Julia
Executable file
53 lines
1.1 KiB
Julia
Executable file
#!/usr/bin/env julia
|
|
|
|
# Jacek Poziemski 272389
|
|
|
|
"""
|
|
f(x::Float64)
|
|
|
|
Calculate `sin(x) + cos(3x)`.
|
|
|
|
# Arguments
|
|
- `x::Float64`: function argument
|
|
"""
|
|
function f(x::Float64)
|
|
return sin(x) + cos(Float64(3) * x)
|
|
end
|
|
|
|
"""
|
|
fderivative(x::Float64)
|
|
|
|
Calculate the derivative of `sin(x) + cos(3x)`, `cos(x) - 3sin(3x)`.
|
|
|
|
# Arguments
|
|
- `x::Float64`: function argument
|
|
"""
|
|
function fderivative(x::Float64)
|
|
return cos(x) - Float64(3) * sin(Float64(3) * x)
|
|
end
|
|
|
|
"""
|
|
derivative(f::Function, x0::Float64, h::Float64)
|
|
|
|
Calculate an approximation of the derivative of `f` at `x0`,
|
|
using `h` in the following formula: `(f(x0 + h) - f(x0)) / h`.
|
|
|
|
# Arguments
|
|
- `f::Function`: function to calculate the derivative of
|
|
- `x0::Float64`: function argument
|
|
- `h::Float64`: value used in the approximation formula
|
|
"""
|
|
function derivative(f::Function, x0::Float64, h::Float64)
|
|
return (f(x0 + h) - f(x0)) / h
|
|
end
|
|
|
|
x = one(Float64)
|
|
|
|
for n in 0:54
|
|
h = 2.0^(-n)
|
|
d = derivative(f, x, h)
|
|
e = abs(fderivative(x) - d)
|
|
println("n: $n; h: $h; h + 1: $(h + 1); derivative: $d; error: $e")
|
|
end
|
|
|
|
println("exact value: $(fderivative(1.0))")
|