<jupyter_start><jupyter_text>Задача 2: аппроксимация функции<jupyter_code>from math import sin, exp
def func(x):
    return sin(x / 5.) * exp(x / 10.) + 5. * exp(-x / 2.)
import numpy as np
from scipy import linalg
arrCoordinates = np.arange(1., 15.1, 0.1)
arrFunction = np.array([func(coordinate) for coordinate in arrCoordinates])<jupyter_output><empty_output><jupyter_text>1. Сформировать СЛАУ для многочлена первой степени, который должен совпадать с функцией в точках 1 и 15.<jupyter_code>#многочлен первой степени
arrCoord1 = np.array([1, 15])
N = 2
arrA1 = np.empty((0, N))
for i in xrange(N):
    arrA1Line = list()
    for j in xrange(N):
        arrA1Line.append(arrCoord1[i] ** j)
    arrA1 = np.append(arrA1, np.array([arrA1Line]), axis = 0)

arrB1 = np.array([func(coordinate) for coordinate in arrCoord1])

print arrCoord1
print arrA1
print arrB1
arrX1 = linalg.solve(arrA1, arrB1)
print arrX1
def func1(x): return arrX1[0] + arrX1[1] * x
arrFunc1 = np.array([func1(coordinate) for coordinate in arrCoordinates])
%matplotlib inline
import matplotlib.pylab as plt
plt.plot(arrCoordinates, arrFunction, arrCoordinates, arrFunc1)
plt.show()<jupyter_output><empty_output><jupyter_text>2. Многочлен второй степени в точка 1, 8, 15.<jupyter_code>#многочлен второй степени
arrCoord2 = np.array([1, 8, 15])
N = 3
arrA2 = np.empty((0, N))
for i in xrange(N):
    arrA2Line = list()
    for j in xrange(N):
        arrA2Line.append(arrCoord2[i] ** j)
    arrA2 = np.append(arrA2, np.array([arrA2Line]), axis = 0)
    
arrB2 = np.array([func(coordinate) for coordinate in arrCoord2])

print arrCoord2
print arrA2
print arrB2
arrX2 = linalg.solve(arrA2, arrB2)
print arrX2
def func2(x): return arrX2[0] + arrX2[1] * x + arrX2[2] * (x ** 2)
arrFunc2 = np.array([func2(coordinate) for coordinate in arrCoordinates])
plt.plot(arrCoordinates, arrFunction, arrCoordinates, arrFunc1, arrCoordinates, arrFunc2)
plt.show()<jupyter_output><empty_output><jupyter_text>3. Многочлен третьей степени в точка 1, 4, 10, 15.<jupyter_code>#многочлен третьей степени
arrCoord3 = np.array([1, 4, 10, 15])
N = 4
arrA3 = np.empty((0, N))
for i in xrange(N):
    arrA3Line = list()
    for j in xrange(N):
        arrA3Line.append(arrCoord3[i] ** j)
    arrA3 = np.append(arrA3, np.array([arrA3Line]), axis = 0)
    
arrB3 = np.array([func(coordinate) for coordinate in arrCoord3])

print arrCoord3
print arrA3
print arrB3
arrX3 = linalg.solve(arrA3, arrB3)
print arrX3
def func3(x): return arrX3[0] + arrX3[1] * x + arrX3[2] * (x ** 2) + arrX3[3] * (x ** 3)
arrFunc3 = np.array([func3(coordinate) for coordinate in arrCoordinates])
plt.plot(arrCoordinates, arrFunction, arrCoordinates, arrFunc1, arrCoordinates, arrFunc2, arrCoordinates, arrFunc3)
plt.show()
with open('answer2.txt', 'w') as fileAnswer:
    for item in arrX3:
        fileAnswer.write(str(item) + ' ')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13442167419242793, 'fraction_numerical': 0.04828065300451546, 'mean_word_length': 4.950413223140496, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '26401', 'license': 'MIT', 'n_tokens_mistral': 1215, 'n_tokens_neox': 1184, 'n_words': 287, 'path': '1 Mathematics and Python/Homework/1 linear algebra and texts similarity and function approximation/Task 2.ipynb', 'repo_name': 'MaxSu77/ML-DA-Coursera-Yandex-MIPT', 'size': '2879'}