0

I am required to code a function that finds the order of a given quadrature formula and then apply the function to the mid-point rule and the two point Gauss quadrature formula.

I have the following theorem to work with-

Let $b_i$ and $c_i$ be the weights and nodes of a quadrature formula for $i=1,...,s$ Then it holds that if $\sum_{i=1}^{s} b_ic_i^{q-1} = \frac{1}{q},$ $q=1,...,r$ for $r\le k,$ then the quadrature method is of order k.

And this is my python code

def order (weights,nodes):
    for q in range(1,len(weights)+10):
        sum=0
        for i in range(len(weights)):
            sum+=weights[i]*nodes[i]**(q-1)
        if sum!=1/q:
            print(q)
            break

It works for the midpoint rule with weights=[1/2,1/2] and nodes=[0,1], but not for the two point Gauss with nodes $=[-\frac{1}{\sqrt{3}},\frac{1}{\sqrt{3}}]$ and weights=[1,1].

Any suggestion on what I am doing wrong greatly appreciated.

Eiraus
  • 349
  • 1
  • 3
  • 15
  • 2
    The first thing I notice is that you're comparing two real numbers with "sum!=1/q". In general this is not a good idea, due to rounding errors. Instead, I would compare the absolute value of their difference to some small value, for ex. $10^{-12}$. – Matti P. Apr 25 '18 at 08:28
  • @MattiP. Thank you, this is what I wanted to say. – Peter Apr 25 '18 at 08:29
  • @MattiP. I just tried it with abs(sum - 1/q)<=10**(-12), is this what you meant? Still did not work, it just returned "None". – Eiraus Apr 25 '18 at 08:36
  • Are you sure about these weights? in the last case $c_i^{q-1} = 1$, so you end up with $\sum c_i = -1/\sqrt{3} + 1 / \sqrt{3} = 0$ regardless of the value of $q$. The condition will be satisfied! – caverac Apr 25 '18 at 08:44
  • @caverac. Your correct, it was a typo. I mixed up the nodes and weights in the text. I have now corrected it in the original question. – Eiraus Apr 25 '18 at 09:14
  • Can you point me to a reference to that theorem? I've never seen it before – caverac Apr 25 '18 at 11:46

0 Answers0