The gamma function with Python
#!/usr/bin/env python3
from scipy.special import gamma
a,b=.1,2
n=25
p=(5**.5-1)/2
c=b-p*(b-a)
d=a+p*(b-a)
fc,fd=gamma(c),gamma(d)
print(f'{"Iter":>4} {"x":>12} {"gamma(x)":>12}')
for i in range(1,n+1):
if fc<fd:
b,d,fd=d,c,fc
c=b-p*(b-a)
fc=gamma(c)
x,f=c,fc
else:
a,c,fc=c,d,fd
d=a+p*(b-a)
fd=gamma(d)
x,f=d,fd
print(f"{i:4d} {x:12.8f} {f:12.8f}")
print(f"\nLocal minimum approx: x = {x:.10f}, gamma(x) = {f:.10f}")\
#!/usr/bin/env python3
import numpy as n,matplotlib.pyplot as p
from scipy.special import gamma
x=n.linspace(.1,2,500)
y=gamma(x)
m=1.461632
p.plot(x,y,label='gamma(x)')
p.axvline(m,color='r',ls='--',label=f'min near x={m:.6f}')
p.scatter(m,gamma(m),color='r')
p.title('Gamma function near its first local minimum')
p.xlabel('x')
p.ylabel('gamma(x)')
p.grid()
p.legend()
p.tight_layout()
p.show()
