user3053452
user3053452

Reputation: 675

scipy.integrate.quad gives TypeError: integer argument expected, got float

I am trying to do Fourier series with numpy. I am trying to write functions as they're defined here

But I am having trouble already at defining a0.

# "M1(t)" function definition.
def M1(t, *args):
    tau, M0 = args
    omega = 2 * np.pi / tau
    return (2 * M0 + M0 * np.sin(omega * t - 2 / 3 * np.pi) +
                M0 * np.sin(omega * t - 4/ 3 * np.pi))


# "M2(t)" function definition.
def M2(t, *args):
    tau, M0 = args
    omega = 2 * np.pi / tau
    return (3 * M0 + M0 * np.sin(omega * t) + M0 * np.sin(omega * t - 2 / 3 * np.pi) +
                M0 * np.sin(omega * t - 4/ 3 * np.pi))    

 def a0(tau, *args):
    # limits of integrals; a = lower of 1st integral;
    # b = higher of 1st and lower od 2nd integral;
    # c = higher of 2nd integral
    a, b, c = 0, tau / 2, tau
    i1, err1 = quad(M1, a, b, *args)
    i2, err2 = quad(M2, b, c, *args)
    return 2 / tau * (i1 + i2)

When I run this code I get the following error:

TypeError: integer argument expected, got float

As requested, traceback of error:

Traceback (most recent call last):
  File "C:/Users/Alex/Documents/Faks/Magisterij/1. letnik/VD/2. seminar/periodicno_vzbujanje.py", line 86, in <module>
    a0 = a0(parameters[0], *parameters)
  File "C:/Users/Alex/Documents/Faks/Magisterij/1. letnik/VD/2. seminar/periodicno_vzbujanje.py", line 41, in a0
    i1, err1 = quad(M1, a, b, *args)
  File "C:\Users\Alex\Anaconda3\lib\site-packages\scipy\integrate\quadpack.py", line 315, in quad
    points)
  File "C:\Users\Alex\Anaconda3\lib\site-packages\scipy\integrate\quadpack.py", line 380, in _quad
    return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
TypeError: integer argument expected, got float

Extra question: how can I pass combined function to quad? For example: M1(t, *args) * np.cos(omega * t)? Do I have to define it as a new function and then pass it in or is there quicker way? Because I feel it's kind of redundant to type 4 extra functions.

UPDATE: I realized I've been passing aditional arguments wrong the whole time. I changed i2, err2 = quad(M2, b, c, *args) to i2, err2 = quad(M2, b, c, args). However now I get the following error: ValueError: not enough values to unpack (expected 2, got 1).

Upvotes: 0

Views: 565

Answers (1)

hpaulj
hpaulj

Reputation: 231625

With your M1

In [202]: M1(0,1,1)
Out[202]: 1.9999999999999996
In [203]: integrate.quad(M1,0,1,(1,1))
Out[203]: (2.0, 2.220446049250313e-14)
In [204]: M1(0,*(1,1))
Out[204]: 1.9999999999999996

The args tuple should look like what you'd pass to M1 with the *() syntax.

Upvotes: 1

Related Questions