-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalfit.py
More file actions
37 lines (31 loc) · 977 Bytes
/
normalfit.py
File metadata and controls
37 lines (31 loc) · 977 Bytes
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
# Author: Alex Gezerlis
# Numerical Methods in Physics with Python (CUP, 2020)
from gauelim_pivot import gauelim_pivot
import numpy as np
def generatedata(N):
np.random.seed(45379)
dataxs = np.linspace(0,9,N)
datays = 2 + 5*np.sin(dataxs) + 0.3*np.random.randn(N)
datasigs = 0.2*np.abs(np.random.randn(N))
return dataxs, datays, datasigs
def phi(n,k,x):
if n==5:
val = x**k
elif n==2:
val = 1. if k==0 else np.sin(x)
return val
def normalfit(dataxs,datays,datasigs,n):
N = dataxs.size
A = np.zeros((N,n))
for k in range(n):
A[:,k] = phi(n,k,dataxs)/datasigs
bs = datays/datasigs
cs = gauelim_pivot(A.T@A, A.T@bs)
chisq = np.sum((bs - A@cs)**2)
return cs, chisq
if __name__ == '__main__':
dataxs, datays, datasigs = generatedata(8)
for n in (5, 2):
cs, chisq = normalfit(dataxs, datays, datasigs, n)
print(cs)
print(chisq/(dataxs.size-cs.size)); print("")