import numpy as np
import matplotlib.pyplot as plt
temps = [5, 50, 500]
ks = [j for j in range(101)]
listzs = [[], [], []]
for i, temp in enumerate(temps):
for k in ks:
listzs[i].append(np.exp(-(k ** 2) / temp))
print(f"Temp = {temp}, listzs[i][100] = {listzs[i][100]}, ratio = {listzs[i][100] / listzs[i][1]}")
plt.plot(ks, listzs[i], 'o', label=f"{temp}K")
plt.ylabel(r'exp(-k^2/T)')
plt.xlabel('k')
plt.legend()
plt.show()
Run to view results
sums = []
for i, temp in enumerate(temps):
sums.append(np.sum(listzs[i]))
print(f"Temp = {temp}, sum = {sums[i]}")
Run to view results
analyticals = []
for i, temp in enumerate(temps):
analyticals.append(np.sqrt(np.pi * temp) / 2)
print(f"analytical solution = {analyticals[i]}")
#The analytical solution is a good approximation to the numerical evaluation for all values of T.
Run to view results
for i in range(len(temps)):
print(f"relative error is {(analyticals[i] - sums[i]) / sums[i]}")
Run to view results
def sum_vs_int(k, T):
'''k 1d array denoting the index of points to plot (assumes consecutive integers)
T - parameter for sum'''
#expfour = exponent(k, T)
expfour = np.exp(-k*k/T)
plt.plot(k, expfour, 'o')
plt.bar(k, expfour, width=1, align='edge', alpha=0.4)
plt.plot([k[0], k[0]], [0,expfour[0]], '--', color='tab:blue')
for i in range(len(k)):
plt.plot([k[i]+1,k[i]+1], [0,expfour[i]], '--', color='tab:blue')
plt.plot([k[i],k[i]+1], [expfour[i],expfour[i]], '--', color='tab:blue')
x = np.linspace(k[0],k[-1]+1,100)
y = np.exp(-x*x/T)
plt.plot(x, y, color='black')
title = 'T = {}'.format(T)
plt.title(title)
plt.show()
Run to view results
firstTen = np.array([i for i in range(10)])
for temp in temps:
sum_vs_int(firstTen, temp)
Run to view results