Writing an infinite sum (a function that has an integral) in Python

I need to show that:

Sum from i = 1 to infinity of the square of the absolute value of c sub i equals 1

It is annoying that c_i is equal to the integral of the function G. Here is my attempt.

import numpy as np
from scipy.integrate import quad

def G(x,n):
    P = (np.sqrt(735))*(np.sqrt(2))*np.sin(n*np.pi*x)*((x**3.0) - (11.0/7.0)*(x**2.0) + (4.0/7.0)*(x))
    return P

def Sum(x, n):
    i = 1
    S = 0
    I, err = quad(G, 0, 1, args=(i))
    while (i<n):
        S = S + I
        i = i + 1
    return S
x = np.linspace(0, 1, 250)    
print Sum(x, 200)

The problem I am facing is writing the summing part. When I run this, I get a larger number than the more values ​​I give it. If n is chosen high enough (instead of infinity), we can show how the sum tends to 1

+4
source share
2 answers

This problem has a lot of pedagogical value.

@alko, . , , , .

, , , . , , . , , - , .

, @alko.

import numpy as np
import scipy.integrate as integ

def g(x) :
    return np.sqrt(1470) * (x**3-11./7*x**2+4./7*x)

def G(x,n) :
    return np.sin(n*np.pi*x) * g(x)

def do_integral (N) :
    res = np.zeros(N)
    err = np.zeros(N)
    for n in xrange(1,N) :
        (res[n],err[n]) = integ.quad(G, 0, 1, args=(n,))
    return (res,err)

(c,err) = do_integral(500)
S = np.cumsum(c[1:]**2) # skip n=0

(, "G", .)

S n. . , ipython, , , , , . , ( ipython), , , ? , , .

S, , , , S[200], , ! ?! , . quad() . , . S , . Integration of G function

, , , S , quad() , ! , , .

? . G(x,n) , n . , , , . .

, . quad() quad_explain(), . , , . , ( , g(x):

def do_integral_weighted (N) :
    res = np.zeros(N)
    err = np.zeros(N)
    for n in xrange(1,N) :
        (res[n],err[n]) = integ.quad(g, 0, 1, weight='sin', wvar=n*np.pi)
    return (res,err)

(cw,errw) = do_integral_weighted(500)
Sw = np.cumsum(cw[1:]**2) # skip n=0

, , Integration of g function , , .

, .

  • , , .
  • , .
  • , , .
  • . numpy/scipy . , .
+7

. quad ( sum (c_i)) :

def Sum(x, n):
    S = 0
    for i in range(n):
        I, _err = quad(G, 0, 1, args=(i))
        S = S + I**2
    return S

, , 1? , . .

+5

All Articles