2

I would like to write a program that solves the definite integral below in a loop which considers a different value of the constant c per iteration.

I would then like each solution to the integral to be outputted into a new array.

How do I best write this program in python?

enter image description here

with limits between 0 and 1.

from scipy import integrate

integrate.quad

Is acceptable here. My major struggle is structuring the program.

Here is an old attempt (that failed)

# import c
fn = 'cooltemp.dat'
c = loadtxt(fn,unpack=True,usecols=[1])

I=[]
for n in range(len(c)):

    # equation
    eqn = 2*x*c[n]

    # integrate 
    result,error = integrate.quad(lambda x: eqn,0,1)

    I.append(result)

I = array(I)
5
  • 4
    Welcome to Stack Overflow! We encourage you to research your questions. If you've tried something already, please add it to the question - if not, research and attempt your question first, and then come back. Commented Aug 16, 2012 at 12:47
  • 2
    What method of numerical integration do you wish to use? Trapezoidal rule? Simpson's rule? Gaussian quadrature? Monte Carlo integration? Or do you just want the built-in scipy.integrate.quadrature function? Please specify some of these details and show your current progress and we will be happy to help. Commented Aug 16, 2012 at 12:49
  • integrate.quad is acceptable here. It's more structuring the program to iterate through the constants that I struggle with. Commented Aug 16, 2012 at 12:51
  • If c is a constant, why not use the standard solution c*x^2? Commented Aug 16, 2012 at 13:04
  • @RolandSmith, here c stands for a constant of integration. Commented Aug 16, 2012 at 13:07

3 Answers 3

5

For instance to compute the given integral for c in [0, 9] :

[scipy.integrate.quadrature(lambda x: 2 * c * x, 0, 1)[0] for c in xrange(10)]

This is using list comprehension and lambda functions.

Alternatively, you could define the function which returns the integral from a given c as a ufunc (thanks to vectorize). This is perhaps more in the spirit of numpy.

>>> func = lambda c: scipy.integrate.quadrature(lambda x: 2 * c * x, 0, 1)[0]
>>> ndfunc = np.vectorize(func)
>>> ndfunc(np.arange(10))
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.])
Sign up to request clarification or add additional context in comments.

2 Comments

This looks good. Rather than xrange(10) how do I get it to compute the integral for c equalling values from an array?
See alternative solution given above
3

You're really close.

fn = 'cooltemp.dat'
c_values = loadtxt(fn,unpack=True,usecols=[1])

I=[]
for c in c_values: #can iterate over numpy arrays directly.  No need for `range(len(...))`

    # equation
    #eqn = 2*x*c[n] #This doesn't work, x not defined yet.

    # integrate 
    result,error = integrate.quad(lambda x: 2*c*x, 0, 1)

    I.append(result)

I = array(I)

I think you're a little confused about how lambda works.

my_func = lambda x: 2*x

is the same thing as:

def my_func(x):
    return 2*x

If you still don't like lambda, you can do this:

f(x,c):
   return 2*x*c

#...snip...
integral, error = integrate.quad(f, 0, 1, args=(c,) )

1 Comment

@ElizabethPor -- I'm just glad to be helpful. Good luck with your integrations.
0
constants = [1,2,3]

integrals = []                                  #alternatively {}

from scipy import integrate

def f(x,c):
    2*x*c

for c in constants:
    integral, error = integrate.quad(lambda x: f(x,c),0.,1.)
    integrals.append(integral)                 #alternatively integrals[integral]

This will output a list of just like Nicolas answer, for whatever list of constants.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.