-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriginterp.py
More file actions
40 lines (33 loc) · 1017 Bytes
/
triginterp.py
File metadata and controls
40 lines (33 loc) · 1017 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
38
39
40
# Author: Alex Gezerlis
# Numerical Methods in Physics with Python (CUP, 2020)
from math import pi
import numpy as np
def f(x):
return np.exp(np.sin(2*x))
def generatedata(n,f):
dataxs = 2*pi*np.arange(n)/n
datays = f(dataxs)
return dataxs, datays
def computeparams(dataxs,datays):
n = dataxs.size
m = n//2
aparams = np.zeros(m+1)
bparams = np.zeros(m-1)
for k in range(m+1):
aparams[k] = [email protected](k*dataxs)/m
for k in range(1,m):
bparams[k-1] = [email protected](k*dataxs)/m
return aparams, bparams
def triginterp(aparams,bparams,x):
n = aparams.size + bparams.size
m = n//2
val = 0.5*(aparams[0] + aparams[-1]*np.cos(m*x))
for k in range(1,m):
val += aparams[k]*np.cos(k*x)
val += bparams[k-1]*np.sin(k*x)
return val
if __name__ == '__main__':
dataxs, datays = generatedata(8, f)
aparams, bparams = computeparams(dataxs, datays)
x = 0.3; pofx = triginterp(aparams, bparams, x)
print(x,pofx)