1

I am trying to apply an exponential fit to my data to determine the point at which the value drops by 1/e. When plotted, the fit seems to favor smaller values and does not portray the true relationship.

example image!!

import numpy as np 
import matplotlib
matplotlib.use("TkAgg")  # need to set the TkAgg backend explicitly otherwise it introduced a low-level error
from matplotlib import pyplot as plt
import scipy as sc


def autoCorrelation(sample, longTime, temp, plotTau = False ):
# compute empirical autocovariance with lag tau averaged over time longTime

    sample.takeTimeStep(timesteps=1500)  # 1500 timesteps to let sample reach equilibrium
    M = np.zeros(longTime)
    for tau in range(longTime):
        M[tau] = sample.calcMagnetisation()
        sample.takeTimeStep()

    M_ave = np.average(M) #time - average
    M = (M - M_ave)

    autocorrelation = np.correlate(M, M, mode='full')
    autocorrelation /= autocorrelation.max() # normalise such that max autocorrelation is 1
    autocorrelationArray = autocorrelation[int(len(autocorrelation)/2):]
    x = np.arange(0, len(autocorrelationArray), 1)

    # apply exponential fit
    def exponenial(x, a, b):
        return a * np.exp(-b * x)
    popt, pcov = curve_fit(exponenial, x, np.absolute(autocorrelationArray)) # array, 2d array
    yy = exponenial(x, *popt)
    plt.plot(x, np.absolute(autocorrelationArray), 'o', x, yy)
    plt.title('Exponential Fit of Magnetisation Autocorrelation against Time for Temperature = ' + str(T) + ' J/k')
    plt.xlabel('Time / Number of Iterations ')
    plt.ylabel('Magnetisation Autocorrelation')
    plt.show()

    # prints tau_e value b from exponential a * np.exp(-b * x)
    print('tau_e is ' + str(1/popt[1])) # units converted to time steps by taking reciprocal






if __name__ == '__main__':

#plot autocorrelation against time
    longTime = 100
    temp = [1, 2, 2.3, 2.6, 3, 4]
    for T in temp:
        magnet = Ising(30, T)  # (N, temp)
        autoCorrelation(magnet, longTime, temp)

Note: Ising is a class in another .py file containing the functions takeTimeStep and calcMagnetisation.

Expect greater values of tau_e

CAPSLOCK
  • 6,243
  • 3
  • 33
  • 56
  • what is the question here? Are you having trouble with the graphing or the values? If your values are different from expected, this is not a programming question for others to solve, please clarify your issue – Rachel Gallen Apr 10 '19 at 11:25
  • if you simply need to change the sizing of the graph, look at [this answer](https://stackoverflow.com/a/47018826/1675954) – Rachel Gallen Apr 10 '19 at 11:35
  • @RachelGallen sorry for not being clear. The values are correct. My issue is the exponential line fitted through the data (orange) is not very representative the data. I am trying to find the point at which my data (blue) drop to 1/e of the initial value, and the value obtained from the orange fit line does not represent he true value. –  Apr 10 '19 at 13:56
  • @RachelGallen that being the orange line drops to zero prematurely –  Apr 10 '19 at 14:18
  • Hi Only saw your messages now. It would seem to me that your issue is caused by your 'longtime' variable being set to 100. At a glance, this is what is defining the end value of your axis, and preventing it from extending beyond that. You can still use this value as the longtime value, but you can also [set the x-axis limits](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.xlim.html) using matplot (simple) functions. Hope this helps. Let me know how you get on. – Rachel Gallen Apr 10 '19 at 15:43

0 Answers0