This repository has been archived on 2021-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
scientificComputing/simulations/lecture/staticnonlinearity.py

56 lines
1.8 KiB
Python

import numpy as np
import scipy.stats as st
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from plotstyle import colors, cm_size, show_spines
def boltzmann(x, x0, k):
return 1.0/(1.0+np.exp(-k*(x-x0)))
if __name__ == "__main__":
n = 50
xmin = -18.0
xmax = 18.0
x0 = 2.0
k = 0.25
sigma = 0.08
rng = np.random.RandomState(15281)
x = np.linspace(xmin, xmax, n)
y = boltzmann(x, x0, k) + sigma*rng.randn(len(x))
xx = np.linspace(xmin, xmax, 200)
yy = boltzmann(xx, x0, k)
fig = plt.figure(figsize=cm_size(16.0, 6.0))
spec = gridspec.GridSpec(nrows=1, ncols=2,
left=0.10, bottom=0.23, right=0.97, top=0.96, wspace=0.4)
ax1 = fig.add_subplot(spec[0, 0])
show_spines(ax1, 'lb')
ax1.plot(xx, yy, colors['red'], lw=2)
ax1.scatter(x, y, c=colors['blue'], edgecolor='white', s=50)
ax1.set_xlabel('Hair deflection / nm')
ax1.set_ylabel('Open probability')
ax1.set_xlim(-20, 20)
ax1.set_ylim(-0.2, 1.17)
ax1.set_xticks(np.arange(-20.0, 21.0, 10.0))
ax1.set_yticks(np.arange(-0.2, 1.1, 0.2))
ax2 = fig.add_subplot(spec[0, 1])
show_spines(ax2, 'lb')
xg = np.linspace(-1.0, 1.01, 200)
yg = st.norm.pdf(xg, 0.0, sigma)
ax2.plot(xg, yg, colors['red'], lw=2)
bw = 0.05
h, b = np.histogram(y-boltzmann(x, x0, k), np.arange(-1.0, 1.01, bw))
ax2.bar(b[:-1], h/np.sum(h)/(b[1]-b[0]), fc=colors['yellow'], width=0.9*bw, align='edge')
ax2.set_xlabel('residuals')
ax2.set_ylabel('pdf')
ax2.set_xlim(-0.3, 0.3)
#ax2.set_ylim(0, 370)
#ax2.set_xticks([0, 0.005, 0.01])
#ax2.set_xticklabels(['0', '0.005', '0.01'])
#ax2.set_yticks(np.arange(0, 351, 100))
#ax2.set_yticklabels([])
fig.savefig("staticnonlinearity.pdf")
plt.close()