multiply Python Quad integration by float

Question:

Hello I’m quite new to python and I’m trying to work through a set of equations. I’m trying to multiple the result of the quad integration by a float my code is:

from __future__ import division
import scipy.special as sp
from scipy import integrate
import math

Tb = 7.2
Tc = 9.3
t = Tb / Tc
n = 2*10**-6
L = 50*10**-6
W = 80*10**-9
a = 3*10**-2
s1 = W/ (2*n)
y1 = (L+(W/2)) / (2*n)
x0 = 0.015
r0 = 4*x0
s2 = (r0 / n)/1000000
print s2
y0 = (x0 / n)/1000000
def t1(t):
    return  t**-1*sp.kv(0, s2)
def t2(t):
    return t**-1*sp.iv(0, s2)
print t2
Fk2 = (math.pi**-2) * integrate.quad(t1, s1, s2) 
FI2 = (math.pi**-2) * integrate.quad(t2, s1, s2)
print Fk2 , FI2

I keep getting the error

     25 print t2
---> 26 Fk2 = (math.pi**-2) * integrate.quad(t1, s1, s2)
     27 FI2 = (math.pi**-2) * integrate.quad(t2, s1, s2)
     28 print Fk2 , FI2

TypeError: can't multiply sequence by non-int of type 'float'

I’m quite unsure as to what todo, I’ve tried replacing def t1(t) by lambda function but again that has done nothing. Any help is really appreciated and thank you in advance.

Asked By: user3191569

||

Answers:

Try this:

Fk2 = [(math.pi**-2) * e for e in integrate.quad(t1, s1, s2)]
FI2 = [(math.pi**-2) * e for e in integrate.quad(t2, s1, s2)]
Answered By: denim2x

Since you’re a beginner, I’ll start by saying that whenever you have an error that you don’t understand, split the line up into multiple separate steps so you can figure out exactly what the error refers to, like this:

temp = integrate.quad(t1, s1, s2)
print "temp:", temp
Fk2 = (math.pi**-2) * temp

Then you can see that the error is that temp is a tuple, and there’s no definition for how to multiply a tuple by a float.

Fk2 = (math.pi**-2) * np.array(temp[:2])

And you can see in the documentation what each term refers to.

Answered By: tom10

integrate.quad returns a tuple of length 2 by default, the first entry is the answer and the second is an estimate of the error of the answer. To use the result in a further calculation, try

Fk2 = (math.pi**-2) * integrate.quad(t1, s1, s2)[0]
FI2 = (math.pi**-2) * integrate.quad(t2, s1, s2)[0]

This selects the answer only and multiplies it by the float.

Answered By: RoBDoG