-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise2.py
More file actions
52 lines (43 loc) · 1.3 KB
/
exercise2.py
File metadata and controls
52 lines (43 loc) · 1.3 KB
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
# coding: utf-8
# In[46]:
import math
import numpy
import matplotlib.pyplot as plt
def F(x): #наша функция
return math.sin(x/5)*math.exp(x/10)+5*math.exp(-x/2)
def matrix(arr): #создаем матрицу размерности nxn
matrixCoef = numpy.zeros(shape=(len(arr), len(arr)))
for i in range(len(arr)):
for j in range(len(arr)):
matrixCoef[i][j] = arr[i]**j
return matrixCoef
def sumOfEle(arr, x): #подсчитываем сумму прогрессии нужной нам размерности
out = numpy.zeros(shape=(len(x)))
for i in range(len(x)):
for j in range(len(arr)):
out[i] += arr[j] * (x[i]**j)
return out
x = numpy.arange(1, 15, .1) #наш набор точек от 1 до 15
in1 = [1, 15]
in2 = [1, 8, 15]
in3 = [1, 4, 10, 15]
m1 = matrix(in1)
m2 = matrix(in2)
m3 = matrix(in3)
#лист решений функции F(x)
l1 = list(map(F, in1))
l2 = list(map(F, in2))
l3 = list(map(F, in3))
answ1 = numpy.linalg.solve(m1, l1)
answ2 = numpy.linalg.solve(m2, l2)
answ3 = numpy.linalg.solve(m3, l3)
#подсчитываем Y
y = list(map(F, x))
y1 = sumOfEle(answ1, x)
y2 = sumOfEle(answ2, x)
y3 = sumOfEle(answ3, x)
#рисуем графики
plt.plot(x, y)
plt.plot(x, y1)
plt.plot(x, y2)
plt.plot(x, y3)