Numerical integration/Gauss-Legendre Quadrature: Difference between revisions

Add expressions and SegmentBinding
(Add expressions and SegmentBinding)
Line 49:
RECORD ==> Record(x : List Fraction Integer, w : List Fraction Integer)
 
gaussCoefgaussCoefficients(n : NNI, eps : Fraction Integer) : RECORD ==
p := legendreP(n,z)
q := n/2*D(p, z)*legendreP(subtractIfCan(n,1)::NNI, z)
Line 56:
[x,w]
 
gaussIntgaussIntegrate(fe : Float ->Expression Float, a : Float, bsegbind : SegmentBinding(Float), n : NNI) : Float ==
eps := 1/10^100
u := gaussCoefgaussCoefficients(n,eps)
interval := segment segbind
var := variable segbind
a := lo interval
b := hi interval
c := (a+b)/2
h := (b-a)/2
h*reduce(+,[wi*fsubst(e,var=c+xi*h) for xi in u.x for wi in u.w])</lang>Example:<lang Axiom>digits(50)
gaussIntgaussIntegrate(x +-> 4/(1+x^2), x=0, ..1, 20)
</lang>Examples:<lang Axiom>digits(50)
gaussInt(x +-> 4/(1+x^2), 0, 1, 20)
 
(1) 3.1415926535_8979323846_2643379815_9534002592_872901276
Line 69 ⟶ 72:
% - %pi
 
(2) - 0.3463549483_9378821092_475 E -26</lang>
Type: Float
gaussInt(exp,-3,3,5)
(3) 20.0355777183_8556215392_8535725275_0939315016_27207447
Type: Float
% - integrate(exp(x),x=-3..3)
 
(4) - 0.0001721364_3424164402_0651513656_5621888545_81038919
Type: OrderedCompletion(Expression(Float))</lang>
 
=={{header|C}}==
136

edits