Numerical integration/Gauss-Legendre Quadrature: Difference between revisions

→‎{{header|J}}: Use more descriptive names and tacit
(→‎{{header|J}}: Use more descriptive names and tacit)
Line 823:
=={{header|J}}==
'''Solution:'''
<lang j>P =: 3 :0 NB. list ofreturns coefficientscoefficents for yth-order Legendre polynomial
getLegendreCoeffs=: verb define M.
if. y<:1 do. 1 {.~ ->: y+1 return. end.
y%~ (<:(,~+:)y) -/@:* (0,P<:y),:(P y-2)
(%~ <:@(,~ +:) -/@:* (0;'') ,&> [: getLegendreCoeffs&.> -&1 2) y
)
 
getPolyRoots=: 1 {:: p. NB. returns the roots of a polynomial
getpoints =: 3 :0 NB. points,:weights for y points
getGaussLegendreWeights=: 2 % -.@*:@[ * (*:@p.~ p..) NB. form: roots getGaussLegendreWeights coeffs
x=. 1{:: p. p=.P y
getGaussLegendrePoints=: (getPolyRoots ([ ,: getGaussLegendreWeights) ])@getLegendreCoeffs
w=. 2% (-.*:x)**:(p..p)p.x
x,:w
)
 
NB.*integrateGaussLegendre a Integrates a function u with a n-point Gauss-Legendre quadrature rule over the interval [a,b]
GaussLegendre =: 1 :0 NB. npoints function GaussLegendre (a,b)
NB. form: npoints function integrateGaussLegendre (a,b)
integrateGaussLegendre=: adverb define
:
'xnodes wwgts'=.getpoints getGaussLegendrePoints x
-: (-~/ y) * wgts +/w@:* u -:( nodes p.~ (+/ , -~/) y)p.x
)</lang>
{{out|Example use}}
<lang j> 5 ^ GaussLegendreintegrateGaussLegendre _3 3
20.0356</lang>
-~/ ^ _3 3 NB. true value
20.0357</lang>
 
=={{header|Java}}==
892

edits