Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from MMP import TMA
- from math import log
- class Problem:
- """Computational problem using splines"""
- def __init__(self, xList, yList, d1, d2):
- super(Problem, self).__init__()
- self.n = len(yList) - 1;
- self.xList = xList
- self.yList = yList
- self.heights = self.__getHeights()
- self.lambdas = self.__getLambdas()
- self.mus = self.__getMus()
- self.frees = [d1] + self.__getFrees() + [d2]
- self.slopes = self.__getSlopes()
- self.a = self.__getAs();
- self.b = self.__getBs();
- def __getHeights(self):
- heights = []
- for x in range(0, self.n):
- heights.append(self.xList[x + 1] - self.xList[x])
- return heights
- def __getLambdas(self):
- lambdas = []
- for i in range(1, self.n):
- lambdas.append(self.heights[i - 1]/(self.heights[i - 1] + self.heights[i]))
- return [0] + lambdas # Нуль из краевых условий
- def __getMus(self):
- mus = []
- for i in range(1, self.n):
- mus.append(self.heights[i]/(self.heights[i - 1] + self.heights[i]))
- return mus + [0] # Нуль из краевых условий
- def __getFrees(self):
- frees = []
- for i in range(1, self.n):
- frees.append(3*(self.lambdas[i - 1]*(self.yList[i + 1] - self.yList[i])/self.heights[i]
- + self.mus[i - 1]*(self.yList[i] - self.yList[i - 1])/self.heights[i - 1]))
- # print(frees)
- return frees
- def __getSlopes(self):
- m = TMA([0] + self.mus, [1] + ([2] * (self.n - 1)) + [1], self.lambdas + [0], self.frees);
- return m.solve()
- def __getAs(self):
- a = []
- for i in range(0, self.n):
- a.append(6/self.heights[i]*
- (-(2*self.slopes[i] + self.slopes[i + 1])/3 +
- (self.yList[i + 1] - self.yList[i])/self.heights[i]))
- return a
- def __getBs(self):
- b = [];
- for i in range(0, self.n):
- b.append(12/(self.heights[i]**2)*((self.slopes[i] + self.slopes[i + 1])/2 -
- (self.yList[i] - self.yList[i])/self.heights[i]))
- return b
- def solve(self, x):
- # print("mus|lambdas|heights|a|b\n",len(self.mus), len(self.lambdas), len(self.heights), len(self.a), len(self.b))
- # print("slopes|frees\n", len(self.slopes), len(self.frees))
- i = 0;
- x1 = xList[i];
- while x1 + self.heights[i] < x:
- x1 += self.heights[i]
- i += 1
- diff = x - self.xList[i];
- # print(i)
- return self.yList[i] + self.slopes[i]*diff + self.a[i]*diff**2/2 + self.b[i]*diff**3/6
- def printSlopes(self):
- for i in range(0, self.n + 1):
- print(self.slopes[i])
- xList = [
- 0.5,
- 0.55,
- 0.6,
- 0.65,
- 0.7,
- 0.75,
- 0.8,
- 0.85,
- 0.9,
- 0.95,
- 1
- ]
- yList = [
- -0.1479400,
- -0.1040401,
- -0.0549733,
- -0.0007458,
- 0.0586362,
- 0.1231673,
- 0.1928419,
- 0.2676551,
- 0.3476020,
- 0.4326779,
- 0.5228787,
- ]
- d1 = 0.8262822
- d2 = 1.8552351
- p = Problem(xList, yList, d1, d2)
- # x = float(input())
- def func(x):
- return p.solve(x);
- a = 0.5
- b = 1.0
- EPS = 1e-5
- n = 10
- def tabulate(a, b, n):
- if (a > b): swap(a, b)
- t = (b - a)/n
- res = []
- for x in range(0, n + 1):
- res.append(a + x*t)
- return res
- def integral(func, table):
- # print(table)
- summ = 0
- for i in range(1, n + 1):
- summ += func(table[i])*(table[i] - table[i - 1])
- return summ
- I = integral(func, tabulate(a, b, n))
- n *= 2
- I2 = integral(func, tabulate(a, b, n))
- c = 1
- while abs(I - I2) > EPS:
- c += 1
- I = I2
- n *= 2
- I2 = integral(func, tabulate(a, b, n))
- print (I2, c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement