Numerical Differentiation and Integration
Differentiation
The derivative of the function $f$ at $x_0$ is
Three and Five Point formula.
when $x_1 = x_0+h$ and $x_2 = x_0 + 2h$ for some $h\ne0$ gives
Three-Point Formulas
Three-Point Endpoint Formula
Three-Point Midpoint formula
using NumericalAnalysis: NCalculus
NCalculus.ThreePoint(x->sin(x), 0, 0.1)1.0033216789612571
Five-Point MidPoint formula
Five-Point EndPoint formula
NCalculus.FivePoint(x->sin(x), 0, 0.1)0.9999803084008577
Integration
Trapezoidal Rule
NCalculus.Trapezoidal(x->x^2, 0, 2)4.0
Simpson's Rule
NCalculus.Simpson(x->x^2, 0, 2)2.6666666666666665
Newton-Cotes Formulas
will calculate n=1,2,3,4. formula rule. if you input n in $[1,4]$, will get a value.
NCalculus.Newton_cotes(x->sin(x),0,π/4)4-element Array{Float64,1}:
0.30055886494217315
0.29798754218726264
0.2928586591925902
0.2928692281360844NCalculus.Newton_cotes(x->sin(x), 0, π/4, n=4)0.2928692281360844
NCalculus.Newton_cotes(x->sin(x),0,π/4,n=3, method="Open")0.29291070254917145
Romberg Integration
NCalculus.Romberg(x->sin(x), 0, π/4, 5, seq_tab=true)5×5 Array{Float64,2}:
0.27768 0.0 0.0 0.0 0.0
0.28912 0.292933 0.0 0.0 0.0
0.291952 0.292896 0.292893 0.0 0.0
0.292658 0.292893 0.292893 0.292893 0.0
0.292834 0.292893 0.292893 0.292893 0.292893NCalculus.Romberg(x->sin(x), 0, π/4, 5)0.29289321881345187
Gaussian Quadrature
Suppose we want to determine $c_1, c_2, x_1$ and $x_2$ so
we use a Ploynomial $f(x) = a_0 + a_1x+a_2x^2+a_3x^3$
we get: a solution $c_1 = c_2=1, x_1 = -\frac{\sqrt{3}}{3}, x_2=\frac{\sqrt{3}}{3}$
So:
NCalculus.Gaussian_Quad(x->sin(x),n=5, a=0, b=π/4)0.29289321879993296
NCalculus.Gaussian_Quad(x->x^2)0.6666666666485936
Multiple Integrals
In this section, Consider the double integral
where $R = {(x,y) | a \leq x \leq b, c \leq y \leq d}$. for some constants a, b, c, d, is a rectangular in the plane.
we apply the Composite Trapezoidal rule to calculate:
Simpson Double Integral
f(x, y) = log(x + 2y)
y_up(x) = 1.5
y_down(x) = 1.0
NCalculus.SimpsonDoubleIntegral(f, (1.4, 2.0), y_up, y_down)0.4295544977526275
Gaussian Double Integral
NCalculus.GaussianDoubleIntegral(f, (1.4, 2.0), y_up, y_down)0.42955452750549294
Method
NumericalAnalysis.NCalculus — ModuleNCalculusNumerical Differentiation and Integration Module.
NumericalAnalysis.NCalculus.FivePoint — MethodFivePoint(f::Function, x₀::Real, h::Real; method::String="Endpoint")input a function f, x₀ and h.Args is method (default: "Endpoint", if you set method is't "Endpoint", it will return MidPoint ans.)
NumericalAnalysis.NCalculus.GaussianDoubleIntegral — Method GaussianDoubleIntegral(f::Function, interval_x::Tuple, y_up::Function, y_down::Function, m::Int=5, n::Int=5)$f = f(x, y), x ∈ interval_x, y = yup(x) - ydown(x)$, Args m and n is Separation distance.
NumericalAnalysis.NCalculus.Gaussian_Quad — MethodGaussian_Quad(f::Function; n::Int=3, a::Real=-1, b::Real=1)Gaussian Quadrature must set n<6,default calculate int f(x)dx in [-1,1]. you can set interval [a, b] to change default interval.
NumericalAnalysis.NCalculus.MutipleIntegralSimple — MethodMutipleIntegralSimple(f::Function, interval_x::Tuple, interval_y::Tuple)It will apply the Composite Trapezoidal rule to approximate the integral.
NumericalAnalysis.NCalculus.Newton_cotes — MethodNewton_Cotes(f::Function, a::Real, b::Real; n::Int=0, method::String="Midpoint")input a function f, and $x in [a, b]$calculateint_a^b f(x)dx. argsn∈[1,4]`
NumericalAnalysis.NCalculus.Romberg — MethodRomberg(f::Function, a::Real, b::Real, n::Int; seq_tab::Bool=false)input a function f, and $x in [a, b]$, set n.if Args seq_tab=true, will return a table, else return a value.
NumericalAnalysis.NCalculus.Simpson — MethodSimpson(f::Function, a::Real, b::Real)input a function f, and $x in [a, b]$ calculate ∫f(x)dx.
NumericalAnalysis.NCalculus.SimpsonDoubleIntegral — MethodSimpsonDoubleIntegral(f::Function, interval_x::Tuple, y_up::Function, y_down::Function; n::Int =6, m::Int=6)$f = f(x, y), x ∈ interval_x, y = yup(x) - ydown(x)$, Args m and n is Separation distance.
NumericalAnalysis.NCalculus.ThreePoint — MethodThreePoint(f::Function, x₀::Real, h::Real; method::String="Endpoint")input a function f, x₀ and h.Args is method (default: "Endpoint", if you set method is't "Endpoint", it will return MidPoint ans.)
NumericalAnalysis.NCalculus.Trapezoidal — MethodTrapezoidal(f::function, a::Real, b::Real)input a function f, and $x in [a, b]$ calculate ∫f(x)dx.