在julia:integration_qawc中使用GSL.jl集成例程
与GSL.jl/examples/quarture.jl中给出的示例类似,我试图集成一个函数。然而,由于这个函数有一个奇点,我需要使用柯西权重。我的想法是使用以下代码在julia:integration_qawc中使用GSL.jl集成例程,julia,gsl,Julia,Gsl,与GSL.jl/examples/quarture.jl中给出的示例类似,我试图集成一个函数。然而,由于这个函数有一个奇点,我需要使用柯西权重。我的想法是使用以下代码 using GSL function Q(p) ws_size = 200 ws = GSL.integration_workspace_alloc(ws_size) f_ = x -> 1/(x+p) f = GSL.@gsl_function(f_) result =
using GSL
function Q(p)
ws_size = 200
ws = GSL.integration_workspace_alloc(ws_size)
f_ = x -> 1/(x+p)
f = GSL.@gsl_function(f_)
result = Cdouble[0][1]
epsrel = 1e-10
epsabs = 1e-10
abserr = Cdouble[0][1]
limit = Csize_t[0][1]
result = integration_qawc(f, 0., 1.e4, p, epsabs,epsrel,limit,ws,result,abserr)
GSL.integration_workspace_free(ws)
return result
end
但是,我得到了以下错误
UndefVarError: f_ not defined
Stacktrace:
[1] (::getfield(Main, Symbol("##117#118")))(::Float64, ::Ptr{Nothing}) at /home/varantir/.julia/packages/GSL/IVE5m /src/manual_wrappers.jl:45
[2] integration_qawc at /home/varantir/.julia/packages/GSL/IVE5m/src/gen/direct_wrappers/gsl_integration_h.jl:570 [inlined]
[3] Q(::Float64) at ./In[250]:14
[4] [251]中的顶级范围:1
这对我来说似乎有点奇怪,因为我已经清楚地定义了f_。有什么想法吗?出于一个奇怪的原因,这不会抛出错误,而是抛出0:
function Q(p)
ws_size = 200
ws = GSL.integration_workspace_alloc(ws_size)
f0(x::Float64)::Float64 = 1/(x+p)
f = GSL.@gsl_function(f0)
result = Cdouble[0][1]
epsrel = 1e-10
epsabs = 1e-10
abserr = Cdouble[0][1]
limit = Csize_t[0][1]
result = integration_qawc(f, 0., 1.e4, p, epsabs,epsrel,limit,ws,result,abserr)
GSL.integration_workspace_free(ws)
return result
end
从集成\u qawc
的文档中:
使用QAG的自适应对分算法,并进行修改,以确保在奇异点x=c处不会发生细分。当子区间包含点x=c或接近点x=c时,使用特殊的25点修正Clenshaw-Curtis规则来控制奇异性。远离奇点,该算法使用普通的15点Gauss-Kronrod积分规则
使用替代方法,使用QuadGK.jl:
using QuadGK
function G2(p)
f(x)=1/(x+p)
a = 0.0
b = 1e4
if a<-p<b
res, err = quadgk(f,a,-p,b,rtol=1e-10,atol=1e-10)
return res
else
res, err = quadgk(f,a,b,rtol=1e-10,atol=1e-10)
return res
end
end
使用QuadGK
功能G2(p)
f(x)=1/(x+p)
a=0.0
b=1e4
如果不回答你的问题,但是如果你想做正交积分,考虑使用多维数值积分,那么Diggk只能把你从可积奇点中拯救出来,而不是像上面的一个那样的主值奇点。尝试运行G2(-10)
。