john22
john22

Reputation: 395

How to simply integral a function written in Sympy

Through using the ways and obtaining help from Stackoverflow users, I could find half of the solution and I need to complete it. Through using Sympy I could produce my function parametrically and it became 100 different items similar to 0.03149536*exp(-4.56*s)*sin(2.33*s) 0.03446408*exp(-4.56*s)*sin(2.33*s). By using f = lambdify(s,f) I converted it to a NumPy function and I needed to do integral of in the different sthat I already have. The upper limit of the integral is a constant value and the lower limit must be done through afor loop`.


When I try to do, I get some error which I post below. The code that I wrote is below, but for being a reproducible question I have to put a generated data. TypeError: cannot determine truth value of Relational

from sympy import exp, sin, symbols, integrate, lambdify
import pandas as pd
import matplotlib.pyplot as plt
from scipy import integrate
import numpy as np

S = np.linspace(0,1000,100)
C = np.linspace(0,1,100)

s, t = symbols('s t')
lanr = -4.56
lani = -2.33
ID = S[-1]
result=[]
f = C * exp(lanr * s) * sin (lani * s)
f = lambdify(s,f)
#vff = np.vectorize(f)
for i in (S):
    I = integrate.quad(f,s,(i,ID))
    result.append(I)
print(result)

EDIT

  1. I tried to do the same without havingSympy by using just Scipy and wrote the code below and again I could not solve the problem.
from scipy.integrate import quad
import numpy as np

lanr = -6.55
lani = -4.22

def integrand(c,s):
    return c *  np.exp(lanr * s) * np.sin (lani * s)



def self_integrate(c,s):
    return quad(integrand,s,1003,1200)

import pandas as pd

file = pd.read_csv('1-100.csv',sep="\s+",header=None)
s = np.linspace(0,1000,100)
c = np.linspace(0,1,100)
ID = s[-1]
for i in s:
    
    I = self_integrate(integrand,c,s)
    print(I)

and I got this TypeError: self_integrate() takes 2 positional arguments but 3 were given

Upvotes: 0

Views: 107

Answers (1)

hpaulj
hpaulj

Reputation: 231385

Assuming you want to integrate over s, and use c as a fixed parameter (for a given quad call), define:

In [198]: lanr = 1
     ...: lani = 2
     ...: def integrand(s, c):
     ...:     return c *  np.exp(lanr * s) * np.sin (lani * s)
     ...: 

test it by itself:

In [199]: integrand(10,1.23)
Out[199]: 24734.0175253505

and test it in quad:

In [200]: quad(integrand, 0, 10, args=(1.23,))
Out[200]: (524.9015616747192, 3.381048596651226e-08)

doing the same for a range of c values:

In [201]: alist = []
     ...: for c in range(0,10):
     ...:     x,y = quad(integrand, 0, 10, args=(c,))
     ...:     alist.append(x)
     ...: 
In [202]: alist
Out[202]: 
[0.0,
 426.74923713391905,
 853.4984742678381,
 1280.2477114017531,
 1706.9969485356762,
 2133.7461856695877,
 2560.4954228035062,
 2987.244659937424,
 3413.9938970713524,
 3840.743134205258]

From the quad docs:

quad(func, a, b, args=(),...)

func : {function, scipy.LowLevelCallable}
    A Python function or method to integrate. If `func` takes many
    arguments, it is integrated along the axis corresponding to the
    first argument.

and an example:

>>> f = lambda x,a : a*x
>>> y, err = integrate.quad(f, 0, 1, args=(1,))

The docs are a bit long, but the basics should be straight forward.

I was tempted to say you were stuck on the sympy calling pattern, but the second argument for that is either the integration symbol, or a tuple.

>>> integrate(log(x), (x, 1, a))
a*log(a) - a + 1

So I'm puzzled as to why you were stuck on using

quad(integrand,s,1003,1200)

The s, whether a sympy variable or a linspace array does not make sense.

Upvotes: 1

Related Questions