-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolarization_ex2.py
170 lines (130 loc) · 4.36 KB
/
polarization_ex2.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import numpy as np
from numpy import arange
from pandas import read_table
from scipy.optimize import curve_fit
from matplotlib import pyplot as plt
import seaborn as sns
sns.set_theme()
# https://machinelearningmastery.com/curve-fitting-with-python/
def sine2x(x, a, b, c):
return a * np.square(x) + b * x + c
def sinesq2x(x, a, b):
return a * x + b
def load_data(path1, path2, objective):
'''
Load data in the *.txt file as x and y arrays
'''
dataframe1 = read_table(path1, header=0, index_col=False)
data1 = dataframe1.to_numpy()
dataframe2 = read_table(path2, header=0, index_col=False)
data2 = dataframe2.to_numpy()
data2[:, 0] = data2[:, 0] + data1[-1, 0]
x, y = np.concatenate((data1[:, 0], data2[:, 0])), np.concatenate((data1[:, 1], data2[:, 1]))
if objective == sine2x:
x = np.sin(2*x)
if objective == sinesq2x:
x = np.square(np.sin(2*x))
return x, y
def fit(objective, path1, path2):
'''
Fit the data to the objective function
Return the parameters of the fit
'''
x, y = load_data(path1, path2, objective)
popt, _ = curve_fit(objective, x, y)
if objective == sine2x:
a, b, c = popt
if objective == sinesq2x:
a, b, = popt
c = None
return x, y, a, b, c
def plot(x, y, a, b, c, objective):
'''
Plot the data and the fit
'''
red_chi_sq, residuals, uncertainty = get_residuals(x, y, a, b, c, objective)
print('Reduced chi-squared:', red_chi_sq)
x_line = arange(min(x), max(x), 0.01)
if objective == sine2x:
function = 'sine2x'
y_line = objective(x_line, a, b, c)
if objective == sinesq2x:
function = 'sinesq2x'
y_line = objective(x_line, a, b)
f, (ax1, ax2) = plt.subplots(1, 2)
f.set_size_inches(20*0.6, 8.5*0.6)
# uncertaintyx = 0.00005 * np.ones(len(x))
if objective == sine2x:
uncertaintyx = abs(2*np.cos(2*x) * 0.00005)
if objective == sinesq2x:
uncertaintyx = abs(2*np.sin(4*x) * 0.00005)
n = 25
ax1.errorbar(x[::n], y[::n], xerr=uncertaintyx[::n], yerr=uncertainty[::n], fmt='o', capsize=1, elinewidth=1, markersize=3)
ax1.plot(x_line, y_line, '--', color='red', linewidth=1)
ax1.set_title('Light Intensity vs. Sensor Position (3 Polarizers)')
ax1.set_ylabel('Light Intensity (volts)')
ax2.scatter(x[::n], residuals[::n], color='red', s=10)
ax2.set_title('Residuals for Light Intensity vs. Sensor Position (3 Polarizers)')
ax2.set_ylabel('Light Intensity (volts)')
ax2.legend(['Reduced Chi-Squared: '+str(round(red_chi_sq, 2))])
if objective == sine2x:
a = round(a, 3)
b = round(b, 3)
c = round(c, 3)
legend = "$f(x) = $"+str(a)+"$x^2 + $"+str(b)+"$x + $"+str(c)
ax1.legend([legend, "Intensity (V)"])
xlabel = 'Sensor Position [$sin(2x)$]'
ax1.set_xlabel(xlabel)
ax2.set_xlabel(xlabel)
if objective == sinesq2x:
a = round(a, 3)
b = round(b, 3)
legend = "$f(x) = $"+str(a)+"$x + $"+str(b)
ax1.legend([legend, "Intensity (V)"])
xlabel = 'Sensor Position [$sin^2(2x)$]'
ax1.set_xlabel(xlabel)
ax2.set_xlabel(xlabel)
plt.savefig('Figures\ex2' + function + '.png')
plt.show()
def get_parameters(a, b, c, objective):
'''
Print the parameters of the fit
'''
a = round(a, 2)
b = round(b, 2)
if objective == sine2x:
c = round(c, 2)
print("f(x) = "+str(a)+"x^2 + "+str(b)+"x + "+str(c))
if objective == sinesq2x:
print("f(x) = "+str(a)+"x + "+str(b))
def get_residuals(x, y, a, b, c, objective):
'''
Residuals = abs(actual_y - fit_y)
'''
if objective == sine2x:
fit_y = objective(x, a, b, c)
if objective == sinesq2x:
fit_y = objective(x, a, b)
residuals = abs(y-fit_y)
uncertainty = 0.03 * np.ones(len(y)) # OR 0.03 uncertainty (further away from 1)
red_chi_sq = chi_squared(y, fit_y, uncertainty, objective)
return red_chi_sq, residuals, uncertainty
def chi_squared(y, fit_y, uncertainty, objective):
'''
Reduced chi-squared value
'''
if objective == sine2x:
df = len(y) - 3
if objective == sinesq2x:
df = len(y) - 2
chi_sq = sum(((y-fit_y)**2)/(uncertainty**2))
return chi_sq / df
def fit_and_plot(objective, path1, path2):
x, y, a, b, c = fit(objective, path1, path2)
plot(x, y, a, b, c, objective)
get_parameters(a, b, c, objective)
if __name__ == '__main__':
path1 = 'Ex2\ex2trial4_1.txt'
path2 = 'Ex2\ex2trial4_2.txt'
fit_and_plot(sine2x, path1, path2)
fit_and_plot(sinesq2x, path1, path2)