19cjl/figs/numerical.fig/simpleq/integration.jl

29 lines
687 B
Julia

# approximate \int_a^b f using Gauss-Legendre quadratures
function integrate_legendre(f,a,b,weights)
out=0
for i in 1:length(weights[1])
out+=(b-a)/2*weights[2][i]*f((b-a)/2*weights[1][i]+(b+a)/2)
end
return out
end
# \int f*g where g is sampled at the Legendre nodes
function integrate_legendre_sampled(f,g,a,b,weights)
out=0
for i in 1:length(weights[1])
out+=(b-a)/2*weights[2][i]*f((b-a)/2*weights[1][i]+(b+a)/2)*g[i]
end
return out
end
# approximate \int_a^b f/sqrt((b-x)(x-a)) using Gauss-Chebyshev quadratures
function integrate_chebyshev(f,a,b,N)
out=0
for i in 1:N
out=out+pi/N*f((b-a)/2*cos((2*i-1)/(2*N)*pi)+(b+a)/2)
end
return out
end