I wrote a small module that gives me an incorrect output-set. It should be a single number!
I don't understand what went wrong.
This is the form of summation used: $$\frac{1}{2} (b-a) \sum_{i=1}^n w_i\,f\left(\frac{b-a}{2}x_i+\frac{a+b}{2}\right)$$ where ${x,w}$ are generated by GaussianQuadratureWeights
command:
Needs["NumericalDifferentialEquationAnalysis`"]
GaussLegendreQuadrature[a_, b_, n_] := Module[{weights, i},
(* GaussianQuadratureWeights[n, a, b] gives a list of the
n pairs {Subscript[x, i], Subscript[w, i]} of the elementary
n-point Gaussian formula for quadrature on the interval a to b,
where Subscript[w, i] is the weight of the abscissa
Subscript[x, i].*)
weights = GaussianQuadratureWeights[n, -1, 1];
Return[(b - a)/2 \!\(
\*UnderoverscriptBox[\(\[Sum]\), \(i = 1\), \(n\)]\(
\*SubscriptBox[\(weights\), \(i, 2\)]\ f[
\*FractionBox[\(a + b\), \(2\)] +
\*FractionBox[\(b - a\), \(2\)]
\*SubscriptBox[\(weights\), \(i, 1\)]]\)\)]];
f[x_] := Sin[x];
GaussLegendre[0, 1, 8]
Answer
I can see two problems : you need to include the function as an argument to GaussLegendreQuadrature
and you need to call it with the correct name.
GaussLegendreQuadrature[a_, b_, n_, f_] := Module[{weights, i},
weights = GaussianQuadratureWeights[n, -1, 1];
(b-a)/2 * Sum[weights[[i, 2]] f[(a + b)/2 + (b - a)/2 weights[[i, 1]]], {i, 1,n}]]
GaussLegendreQuadrature[0, 1, 8, Sin]
(* 0.459698 *)
Comments
Post a Comment