coding utf8 import math import Numeric import string import copy class

  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
# -*- coding: utf8 -*-
import math
import Numeric
import string
import copy
class Equation:
def __init__(self, f, t0, x0, cnst, values = None):
self.f = f
self.t0 = t0
self.x0 = x0
self.cnst = {}
if values == None:
for el in cnst:
self.cnst[el] = 1
else:
for i in xrange(len(cnst)):
self.cnst[cnst[i]] = values[i]
def __call__(self, t, x):
cnst = copy.deepcopy(self.cnst)
cnst['t'] = t
for i in xrange(len(self.f)):
str = 'x%d' % (i + 1)
cnst[str] = x[i]
cnst['math'] = math
res = []
for i in xrange(len(self.f)):
res.append(eval(self.f[i], cnst))
return res
def set_cnst(self, cnst):
self.cnst = cnst
def get_cnst(self):
return self.cnst
def check_parameters(self):
for el in self.x0:
if el < 0: return 0 # if there are negative x0[i]
return 1
class RungeKutta:
def __init__(self, eq, h, t1):
self.eq = eq
self.h = h
self.t1 = t1
def run(self): # returns [... [ti, xi] ...]
tn = string.atof(self.eq.t0)
xn = []
for i in xrange(len(self.eq.x0)):
xn.append(string.atof(self.eq.x0[i]))
x = []
x.append([tn, copy.deepcopy(xn)])
self.h = string.atof(self.h)
while(tn + self.h <= self.t1):
k1 = self.eq(tn, xn)
ktmp = []
for i in xrange(len(k1)):
ktmp.append(xn[i] + k1[i] * self.h / 2)
k2 = self.eq(tn + self.h / 2, ktmp)
ktmp = []
for i in xrange(len(k2)):
ktmp.append(xn[i] + k2[i] * self.h / 2)
k3 = self.eq(tn + self.h / 2, ktmp)
ktmp = []
for i in xrange(len(k3)):
ktmp.append(xn[i] + k3[i] * self.h)
k4 = self.eq(tn + self.h, ktmp)
for i in xrange(len(self.eq.x0)):
xn[i] += self.h / 6 * (k1[i] + 2 * k2[i] + 2 * k3[i] + k4[i])
tn += self.h
x.append([tn, copy.deepcopy(xn)])
return x
class MNKO:
def __init__(self, X, y):
self.X = copy.deepcopy(X)
self.y = copy.deepcopy(y)
self.tetha = Numeric.zeros((self.X.shape[1], 1), 'f')
def run(self):
H_s = Numeric.zeros((self.X.shape[1], self.X.shape[1]), 'f')
a_s = Numeric.zeros((1, self.X.shape[1]), 'f')
h_s = Numeric.zeros((1, self.X.shape[1]), 'f')
s = 0
b_s = Numeric.dot(Numeric.transpose(self.X[:,0:1]), self.X[:,0:1])[0][0]
gamma_s = Numeric.dot(Numeric.transpose(self.X[:,0:1]), self.y)[0][0]
t_s = 1. / b_s * gamma_s
self.tetha[0, 0] = t_s
print self.tetha
for s in xrange(1, self.X.shape[1]):
if s != 1:
H_s[0:s-1, 0:s-1] += Numeric.multiply(Numeric.dot(a_s[:, 0:s-1], Numeric.transpose(a_s[:, 0:s-1])), 1. / b_s)
H_s[0:s-1, s-1:s] -= Numeric.multiply(Numeric.transpose(a_s[:, 0:s-1]), 1. / b_s)
H_s[s-1:s, 0:s-1] -= Numeric.product(a_s[:, 0:s-1], 1. / b_s)
H_s[s-1, s-1] = 1. / b_s
h_s = Numeric.dot(Numeric.transpose(self.X[:,0:s]), self.X[:,s:s+1])
a_s[:, 0:s] = Numeric.dot(H_s[0:s, 0:s], h_s[:, 0:s])
b_s = Numeric.dot(Numeric.transpose(self.X[:,s:s+1]), self.X[:,s:s+1])[0][0] - Numeric.dot(Numeric.transpose(h_s[:,0:s]), a_s[:,0:s])[0][0]
gamma_s = Numeric.dot(Numeric.transpose(self.X[:,s:s+1]), self.y)[0][0]
t_s = 1. / b_s * (gamma_s - Numeric.dot(Numeric.transpose(h_s[:,0:s]), self.tetha[0:s,:])[0][0])
self.tetha[0:s,:] -= Numeric.multiply(a_s[:,0:s], t_s).astype('f')
self.tetha[s,0] = t_s
print Numeric.dot(self.X, self.tetha)
def main():
newton = Equation(['-tau * (x1 - Tc)'], 0, [0], ['Tc', 'tau'], [10, 1])
rk = RungeKutta(newton, 0.1, 15)
result = rk.run()
res = []
for i in xrange(11):
res.append(result[i*10][1][0])
del result
X = []
for el in res[:-1]:
X.append([el, 1])
X = Numeric.array(X, 'f')
y = []
for el in res[1:]:
y.append([el])
y = Numeric.array(y, 'f')
del res
print X
print y
mnko = MNKO(X, y)
del X, y
mnko.run()
if __name__ == '__main__':
main()