-
Notifications
You must be signed in to change notification settings - Fork 4
/
parameterEstimztion_functions.py
102 lines (76 loc) · 2.17 KB
/
parameterEstimztion_functions.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
import os, sys
import torch
import numpy as np
import scipy as sp
import scipy.sparse as sparse
import matplotlib.pyplot as plt
import math
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import grad
from torch.autograd.functional import jacobian
from torch.autograd.functional import jvp
import torch.optim as optim
from scipy.sparse.linalg import spsolve
import torchvision
from torch.utils.data.dataloader import DataLoader
import matplotlib.pyplot as plt
import torch.autograd.functional as taf
#device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
device = 'cpu'
def yp_fun(p, X):
#x' = sigma*(y-x)
#y' = x*(rho-z)-y
#z' = x*y - beta*z
#sigma = 10, rho = 28 beta=8/3,
xp1 = p[0]*(X[:,1] - X[:,0])
xp2 = X[:,0]*(p[1] - X[:,2]) - X[:,1]
xp3 = X[:,0]*X[:,1] - p[2]*X[:,2]
yp = torch.cat((xp1[:,None], xp2[:,None], xp3[:,None]), dim=1)
return yp
def dynamical_system(p, x0, yp_fun, nt, dt):
X = torch.zeros(x0.shape[0], 3, nt+1)
X[:, :, 0] = x0
for i in range(nt):
Xi = X[:,:,i].clone()
k1 = yp_fun(p, Xi)
k2 = yp_fun(p, Xi + dt/2*k1)
k3 = yp_fun(p, Xi + dt/2*k2)
k4 = yp_fun(p, Xi + dt*k3)
Xp = Xi + dt/6*(k1+2*k2+2*k3+k4)
X[:,:,i+1] = Xp
return X
def phaseDiagram(X):
# Data for a three-dimensional line
x = X[0,0,:].detach().cpu()
y = X[0,1,:].detach().cpu()
z = X[0,2,:].detach().cpu()
plt.figure(1)
plt.subplot(3,1,1)
plt.plot(x)
plt.subplot(3,1,2)
plt.plot(y)
plt.subplot(3,1,3)
plt.plot(z)
plt.figure(2)
ax = plt.axes(projection='3d')
ax.plot3D(x, y, z)
return ax
Nt = 50
dt = 1e-2
p = torch.tensor([10, 28, 8/3])
#p.requires_grad = True
x0 = torch.randn(1,3)
X = dynamical_system(p, x0, yp_fun, Nt, dt)
def dynamical_system_wrapper(p):
Nt = 50
dt = 1e-2
x0 = torch.ones(1,3)
X = dynamical_system(p, x0, yp_fun, Nt, dt)
return X
#J = jacobian(dynamical_system_wrapper, p)
# Computing J^TJv for any v
# J^T ( Jv)
v = torch.randn_like(p)
Jv = jvp(dynamical_system_wrapper,p, v)
print('h')