How to add two functions f(x,n) and f(x,m) and integrate the product of these two functions?
Question:
I want to integrate the product of two functions, but it seems like i keep gettin an error
AttributeError: ‘Mul’ object has no attribute ‘cos’
import numpy as np
import sympy as sp
x = sp.symbols('x')
dp_xi = []
dp_eta = []
for n in range (1,5,1):
for m in range (1,5,1):
dp_xi.append(np.cos((n-2)*(np.pi/180)*x)*(n-2)*np.pi/180)
dp_eta.append(np.cos((m-2)*(np.pi/180)*x)*(m-2)*np.pi/180)
df_xi = sum(dp_xi)
df_eta = sum(dp_eta)
I = sp.integrate(df_xi*df_eta, (x, 0, 1))
print("I=",I)
Any ideas?? Thank you
I tried to replace np with sp but the integral wouldn’t work
Answers:
As mentioned, you should use only symbols if you want to use analytical integration.
n = sp.symbols('n')
f = sp.cos((n-2)*(sp.pi/180*x)*(n-2)*sp.pi/180)
sp.integrate(f, (x, 0, 1))
Where’s the FULL ERROR message?
Look at what I get when running your code
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
AttributeError: 'Mul' object has no attribute 'cos'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
Cell In[25], line 9
7 for n in range (1,5,1):
8 for m in range (1,5,1):
----> 9 dp_xi.append(np.cos((n-2)*(np.pi/180)*x)*(n-2)*np.pi/180)
10 dp_eta.append(np.cos((m-2)*(np.pi/180)*x)*(m-2)*np.pi/180)
12 df_xi = sum(dp_xi)
TypeError: loop of ufunc does not support argument 0 of type Mul which has no callable cos method
It points to the line with the np.cos
. Running that expression by itself:
In [26]: np.cos((n-2)*(np.pi/180)*x)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
AttributeError: 'Mul' object has no attribute 'cos'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
Cell In[26], line 1
----> 1 np.cos((n-2)*(np.pi/180)*x)
TypeError: loop of ufunc does not support argument 0 of type Mul which has no callable cos method
Strip off the cos
, and we see the argument is:
In [28]: print((n-2)*(np.pi/180)*x)
-0.0174532925199433*x
The short answer, as others explain, np.cos
cannot be used a sympy.symbol
or expression.
sympy
‘s own cos
runs, though, since x
is still a symbol, it won’t try to return a numeric value:
In [30]: print(sp.cos((n-2)*(np.pi/180)*x))
cos(0.0174532925199433*x)
With sp
instead:
In [33]: print(df_xi)
2*pi*cos(pi*x/90)/45
In [34]: print(df_eta)
2*pi*cos(pi*x/90)/45
And the integral:
In [35]: I = sp.integrate(df_xi*df_eta, (x, 0, 1))
In [37]: print(I)
8*pi*(sin(pi/90)*cos(pi/90)/2 + pi/180)/45
Your code works fine with sp instead of np if you dedent the code in which sum
is used. (In addition, there is no need for a double loop since each expression only depends on one loop variable.)
import sympy as sp
x = sp.symbols('x')
a = [sp.cos((n-2)*(sp.pi/180)*x)*(n-2)*sp.pi/180 for n in range(1, 5)]
df_xi = 4*sum(a)
df_eta = df_xi.subs(n, m)
I = sp.integrate(df_xi*df_eta, (x, 0, 1)) # or sp.Integral(df_xi*df_eta, (x, 0, 1))
print("I=",I)
outputs
I= 8*pi*(sin(pi/90)*cos(pi/90)/2 + pi/180)/45
which evaluates to
>>> I.n()
0.0194875985645024
I want to integrate the product of two functions, but it seems like i keep gettin an error
AttributeError: ‘Mul’ object has no attribute ‘cos’
import numpy as np
import sympy as sp
x = sp.symbols('x')
dp_xi = []
dp_eta = []
for n in range (1,5,1):
for m in range (1,5,1):
dp_xi.append(np.cos((n-2)*(np.pi/180)*x)*(n-2)*np.pi/180)
dp_eta.append(np.cos((m-2)*(np.pi/180)*x)*(m-2)*np.pi/180)
df_xi = sum(dp_xi)
df_eta = sum(dp_eta)
I = sp.integrate(df_xi*df_eta, (x, 0, 1))
print("I=",I)
Any ideas?? Thank you
I tried to replace np with sp but the integral wouldn’t work
As mentioned, you should use only symbols if you want to use analytical integration.
n = sp.symbols('n')
f = sp.cos((n-2)*(sp.pi/180*x)*(n-2)*sp.pi/180)
sp.integrate(f, (x, 0, 1))
Where’s the FULL ERROR message?
Look at what I get when running your code
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
AttributeError: 'Mul' object has no attribute 'cos'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
Cell In[25], line 9
7 for n in range (1,5,1):
8 for m in range (1,5,1):
----> 9 dp_xi.append(np.cos((n-2)*(np.pi/180)*x)*(n-2)*np.pi/180)
10 dp_eta.append(np.cos((m-2)*(np.pi/180)*x)*(m-2)*np.pi/180)
12 df_xi = sum(dp_xi)
TypeError: loop of ufunc does not support argument 0 of type Mul which has no callable cos method
It points to the line with the np.cos
. Running that expression by itself:
In [26]: np.cos((n-2)*(np.pi/180)*x)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
AttributeError: 'Mul' object has no attribute 'cos'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
Cell In[26], line 1
----> 1 np.cos((n-2)*(np.pi/180)*x)
TypeError: loop of ufunc does not support argument 0 of type Mul which has no callable cos method
Strip off the cos
, and we see the argument is:
In [28]: print((n-2)*(np.pi/180)*x)
-0.0174532925199433*x
The short answer, as others explain, np.cos
cannot be used a sympy.symbol
or expression.
sympy
‘s own cos
runs, though, since x
is still a symbol, it won’t try to return a numeric value:
In [30]: print(sp.cos((n-2)*(np.pi/180)*x))
cos(0.0174532925199433*x)
With sp
instead:
In [33]: print(df_xi)
2*pi*cos(pi*x/90)/45
In [34]: print(df_eta)
2*pi*cos(pi*x/90)/45
And the integral:
In [35]: I = sp.integrate(df_xi*df_eta, (x, 0, 1))
In [37]: print(I)
8*pi*(sin(pi/90)*cos(pi/90)/2 + pi/180)/45
Your code works fine with sp instead of np if you dedent the code in which sum
is used. (In addition, there is no need for a double loop since each expression only depends on one loop variable.)
import sympy as sp
x = sp.symbols('x')
a = [sp.cos((n-2)*(sp.pi/180)*x)*(n-2)*sp.pi/180 for n in range(1, 5)]
df_xi = 4*sum(a)
df_eta = df_xi.subs(n, m)
I = sp.integrate(df_xi*df_eta, (x, 0, 1)) # or sp.Integral(df_xi*df_eta, (x, 0, 1))
print("I=",I)
outputs
I= 8*pi*(sin(pi/90)*cos(pi/90)/2 + pi/180)/45
which evaluates to
>>> I.n()
0.0194875985645024