Numerical integration/Gauss-Legendre Quadrature: Difference between revisions

m
→‎{{header|Fortran}}: use of automatic re-allocation, more accurate evaluation of P' using Horner's method
m (→‎{{header|Fortran}}: use of automatic re-allocation, more accurate evaluation of P' using Horner's method)
Line 307:
 
=={{header|Fortran}}==
<lang Fortran>program! Works with gfortran but needs the option gauss
! -assume realloc_lhs
! when compiled with Intel Fortran.
 
program gauss
implicit none
integer, parameter :: p = 16 ! quadruple precision
integer :: n = 10, k
real(kind=p), allocatable :: r(:,:)
real(kind=p) :: z, a, b, exact, z1, z2
do n = 1,20
a = -3; b = 3
b = 3
allocate (r(2,n))
r = gaussquad(n)
z = (b-a)/2*dot_product(r(2,:),exp((a+b)/2+r(1,:)*(b-a)/2))
exact = exp(3.0_p)-exp(-3.0_p)
print "(i0,1x,g0,1x,g10.2)",n, z, z-exact
deallocate(r)
end do
contains
 
function polyval(coeff, x) result (y)
real(kind=p) :: coeff(:), x, y
integer :: k
y = coeff(1)
do k = 2, size(coeff)
y = y*x+coeff(k)
end do
end function
 
function gaussquad(n) result(r)
integer :: n
real(kind=p), parameter :: pi = 4*atan(1._p)
real(kind=p) :: r(2, n), x, f, df, dx
integer :: i, iter
real(kind = p), allocatable :: p0(:), p1(:), tmp(:)
allocate(p0(1), p1(2))
p0 = [1._p]
Line 348 ⟶ 338:
do k = 2, n
allocate(tmp(k+1))
tmp = ((2*k-1)*[p1,0._p]-(k-1)*[0._p, 0._p,p0])/k
deallocate(p0) = p1; p1 = tmp
call move_alloc(p1, p0)
call move_alloc(tmp, p1)
end do
do i = 1, n
Line 358 ⟶ 345:
iter = 0
do iter = 1, 10
f = polyval(p1, x(1); df = 0._p
dfdo k = n/(x*x-1)*(x*f-polyval(p02,x) size(p1)
df = f + x*df
f = p1(k) + x * f
b = 3end do
dx = f / df
x = x - dx
Line 374 ⟶ 364:
n numerical integral error
--------------------------------------------------
1 6.0000000000000000000000000000000000000000000000000000000000000000000 -14.
2 17.48746464105556896436068404624495144874646410555689643606840462449 -2.5
3 19.85369199680558219213091089271583748536919968055821921309108927158 -0.18
4 20.02868839529070085277380544398576000286883952907008527738054439858 -0.71E-02
5 20.03557771838556215392853572527508350355777183855621539285357252751 -0.17E-03
6 20.03574697509234388306545755854994630357469750923438830654575585499 -0.29E-05
7 20.03574981972660077557187293728928870357498197266007755718729372892 -0.35E-07
8 20.03574985449451728822609180416851140357498544945172882260918041684 -0.33E-09
9 20.03574985481743383688644194548531600357498548174338368864419454859 -0.24E-11
10 20.03574985481978987111757669085451180357498548197898711175766908548 -0.14E-13
11 20.03574985481980373055291471597280240357498548198037305529147159695 -0.67E-16
12 20.03574985481980379767595310145150430357498548198037976759531014464 -0.27E-18
13 20.03574985481980379794824581189388130357498548198037979482458119095 -0.94E-21
14 20.03574985481980379794918444838505900357498548198037979491844483597 -0.28E-23
15 20.03574985481980379794918723165592850357498548198037979491872317190 -0.73E72E-26
16 20.03574985481980379794918723894978570357498548198037979491872388913 0-.18E40E-28
17 20.03574985481980379794918723910866720357498548198037979491872389166 0-.18E15E-2728
18 20.03574985481980379794918723972037030357498548198037979491872389259 0-.79E58E-2729
19 20.03574985481980379794918724042906020357498548198037979491872388910 0-.15E41E-2628
20 20.03574985481980379794918723975742520357498548198037979491872388495 0-.83E82E-2728
</pre>
 
Anonymous user