-
Notifications
You must be signed in to change notification settings - Fork 0
/
tanh_simple.py
58 lines (42 loc) · 1.26 KB
/
tanh_simple.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from __future__ import division,print_function
from scipy import constants as sc
from numpy import loadtxt,array,dot,sqrt,linspace,log
from numpy import cos,sin,tanh
import numpy as np
from math import pi
import matplotlib.pyplot as plt
# Create area of x and tanh values (toy model of magnetization)
x=np.linspace(0,5,100,endpoint=True)
y=np.tanh(x)
h=0.000001
#Simple forward difference derivative
def fdif_der(f,x,h):
#Compute forward difference
s=f(x+h)-f(x-h)
#Divide by stepsize
s/=(2*h)
return s
# Enter interactive mode
plt.ion()
#plt.figure(1)
#Set up latex fonts
#plt.rc('text',usetex=True)
#plt.rc('font',**{'family':'serif','serif':['Times New Roman']})
#Multiple panels to visualize function, derivative, and error
plt.subplot(211)
plt.plot(x,y,'b',linewidth=3)
plt.ylim(0,1.3)
#plt.ylabel('Magnetization'r'$M(T)$',fontsize=18)
#Plot Derivative
plt.subplot(212)
plt.plot(x,fdif_der(np.tanh,x,h),color='k',linewidth=4)
plt.plot(x,np.power(np.cosh(x),-2),'r*',markersize=1,linewidth=4)
plt.yscale('log')
plt.ylim([0,10])
plt.xlabel(r'$1/T~({\rm eV}^{-1})$',fontsize=18)
plt.ylabel(r'$\frac{dM}{dT}$')
plt.ylim([0.00001,10])
#plt.xlabel(r'$1/T~({\rm eV}^{-1})$',fontsize=18)
#plt.ylabel(r'$\frac{dM}{dT}')
plt.savefig("pleaswork.pdf",format="pdf")
plt.show()