function intf = guasslengendre(f,a,b) % function intf = guasslengendre(f,a,b) % Approximate the integral of f from a to b using a 6-point Gauss-Legendre rule. % f is either the name of a function file, or an inline function. nodes = [-0.9324695142031520; -0.6612093864662645; -0.2386191860831969; 0.2386191860831969; 0.6612093864662645; 0.9324695142031520]; weights = [ 0.1713244923791703; 0.3607615730481386; 0.4679139345726910; 0.4679139345726910; 0.3607615730481386; 0.1713244923791703]; % change of variables from [-1,1] to [a,b] ab_nodes = a + (b-a)*(nodes+1)/2; ab_weights = weights*(b-a)/2; % apply Guass-Legendre rule intf = sum(ab_weights.*feval(f,ab_nodes));