Issue
I want to plot the gamma function, This is my script
from math import *
import matplotlib.pyplot as plt
from scipy.integrate import quad
import numpy as np
def f(x,n):
result = (x**(n-1))*(exp(-x))
return result
def gamma(n):
r1 = quad(f, 0, np.inf, args=n)
r2 = np.array(r1)
r3 = r2[0]
return round(r3,2)
n = np.arange(1,10, step=0.1)
plt.plot(n, gamma(n))
plt.show()
but i got this error
File "/home/yassir/python/desktop/plot_gamma.py", line 17, in <module>
plt.plot(n, gamma(n))
File "/home/yassir/python/desktop/plot_gamma.py", line 11, in gamma
r1 = quad(f, 0, np.inf, args=n)
File "/usr/lib/python3/dist-packages/scipy/integrate/quadpack.py", line 341, in quad
retval = _quad(func, a, b, args, full_output, epsabs, epsrel, limit,
File "/usr/lib/python3/dist-packages/scipy/integrate/quadpack.py", line 455, in _quad
return _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit)
TypeError: only size-1 arrays can be converted to Python scalars
Solution
You don't have to calculate it yourself, you can use it directly from scipy scipy.special.gamma. Example:
import numpy as np
import matplotlib.pyplot as plt
from scipy.special import gamma
x = np.arange(1,10, step=0.1)
y = gamma(x)
plt.plot(x, y)
Answered By - Lucas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.