Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating equation using scipy

Tags:

python

scipy

from scipy.integrate import quad

def integrand(a, b):
    return a * x ** 2 + b

a = 2
b = 1

I = quad(integrand, 0, 1, args=(a,b))
I

This is my program. When I tried to run it, it was showing error :

integrand () takes 2 positional arguments but 3 were given ....

I didn't understand why it is asking for 3 arguments when there are only two variables, i.e. a and b.

Can anyone help me? Can anyone clarify my doubts?

like image 581
Niloy Das Avatar asked Aug 22 '18 14:08

Niloy Das


People also ask

What is Scipy integration?

Scipy is the scientific computing module of Python providing in-built functions on a lot of well-known Mathematical functions. The scipy. integrate sub-package provides several integration techniques including an ordinary differential equation integrator.

How does Scipy integrate quad work?

The quad function returns the two values, in which the first number is the value of integral and the second value is the estimate of the absolute error in the value of integral. Note − Since quad requires the function as the first argument, we cannot directly pass exp as the argument.


1 Answers

Just replace

def integrand(a, b):

by

def integrand(x, a, b):

The problem is that in the function, you use a variable x but you do not pass the variable x as an argument to the function. 0 and 1 acts as the limits of the integral but since you are integrating w.r.t. x, you get this error.

Output

(1.6666666666666667, 1.8503717077085944e-14)
like image 113
Sheldore Avatar answered Sep 30 '22 00:09

Sheldore