3

I have a function for acceleration a(t) which I know if I integrate twice with respect to t, I can find position x(t). I am trying to find position at t = 10 seconds.

Since a(t) is not multivariable I am having trouble using the Scipy dblquad function to calculate the double integral I need. Please see what I have so far:

  def a(t):
      return (2.5 / (1 + math.exp((t-8)/0.8)))

  def upperbound():
       return 10

  def lowerbound():
      return 0

  x = dblquad(a,0,10,lowerbound,upperbound)

This does not work as from what I can gather dblquad needs a multivariabled a(t). Can anyone help?

3
  • What is dblquad ? can you explain more broadly Commented Oct 25, 2017 at 19:28
  • Please find documentation here: docs.scipy.org/doc/scipy-0.19.1/reference/generated/… Commented Oct 25, 2017 at 19:29
  • Explain how your function is a A Python function or method of at least two variables: Commented Oct 25, 2017 at 20:52

1 Answer 1

4

You can use scipy's single variable integration twice to accomplish this

import math
from scipy.integrate import quad

def a(t):
    return (2.5 / (1 + math.exp((t-8)/0.8)))

lb, ub = 0, 10

integral = quad(lambda t: quad(a, 0, t)[0], lb, ub)[0]
print(integral)
# 86.28470375472537

This is necessary because what you want to achieve is not to integrate a function of two variables but rather to integrate a function of one variable twice. In the integral statement above, the inner quad integrates the function once but maintains the integral as a function of t. The outer quad integrates the function the second time over the defined limits.

Note that it is necessary to take the first argument from the output of quad because it outputs a tuple. The second argument is an upper bound on the error of the numerical integration.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Chris. I realize this question is a few yers old, but I'm trying to perform a double integration on a function with a single variable and then plot the function and the dual integrated function. Could you please explain why did you use square brackets and why did you define the a(t) function as a lambda in you answer above in the integral =... line
The square brackets are because of the last sentence of the answer. quad returns a tuple and you only need the first element. The lambda is to create a function of one variable (t) that returns the integral of the function a from 0 to t, which can again be integrated by quad. The function in t represents an indefinite integral of a. The a function can also be a normal function. No difference with a lambda.
@user2882635 To reiterate the previous comment. Using lambda is simply a quick way to define a function. It could have been written separately as def first_integral(t); return quad(a, 0, t)[0]. The [0] after each call to quad is used because quad returns a lot of extra information about the integration which is not needed in this case. The first returned argument is the value of the integral and is all that is needed.

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.