__author__ ars import numpy import math input data lambda math math 10

 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
__author__ = 'ars'
import numpy
import math
#input data
f = lambda x: 2 * (math.e ** x)
y = [1, math.e]
n = 10
h = 1. / n
x = list(numpy.arange(0, 1 + h, h))
p = lambda x: 3
q = lambda x: -2
#eval a, b, c
matrix_a, matrix_b, matrix_c, matrix_d = {}, {}, {}, {}
for i in range(n):
matrix_a[i] = 1 - p(x[i + 1]) * h / 2
matrix_b[i] = q(x[i]) * h ** 2 - 2
matrix_c[i] = 1 + p(x[i]) * h / 2
matrix_d[i] = f(x[i]) * h ** 2
alpha, beta, y = {}, {}, {}
alpha[1] = - matrix_c[1] / matrix_b[1]
beta[1] = matrix_d[1] / matrix_b[2]
for i in range(2, n):
alpha[i] = - (matrix_c[i] / (matrix_a[i - 1] * alpha[i - 1] + matrix_b[i]))
beta[i] = (matrix_d[i] - matrix_a[i - 1] * beta[i - 1]) / (matrix_a[i - 1] * alpha[i - 1] + matrix_b[i])
y[n - 1] = beta[n - 1]
print [x for x in reversed(range(n - 1))]
for i in reversed(range(1, n - 1)):
y[i] = alpha[i] * y[i + 1] + beta[i]
print '\n'.join([str(y[x]) for x in y])