Mephistopheles_

My first optimization

Sep 2nd, 2021
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import pandas as pd
  4. import re
  5. from scipy.spatial.distance import cosine as cos
  6. import collections
  7.  
  8.  
  9. def f(x):
  10.     return np.sin(x / 5) * np.exp(x / 10) + 5 * np.exp(-x / 2)
  11.  
  12.  
  13. A = np.array([[1, 1, 1, 1], [64, 16, 4, 1], [1000, 100, 10, 1], [3375, 225, 15, 1]])
  14. b = np.array([f(1), f(4), f(10), f(15)])
  15. res = np.linalg.solve(A, b)
  16. print(res[::-1])
  17.  
  18.  
  19. def g(x):
  20.     global res
  21.     return res[0] * x * x * x + res[1] * x * x + res[2] * x + res[3]
  22.  
  23.  
  24. x = np.arange(0, 17, 0.5)
  25. y = np.array([f(i) for i in x])
  26. plt.plot(x, y)
  27. y = np.array([g(i) for i in x])
  28. plt.plot(x, y)
  29. plt.show()
  30.  
Advertisement
Add Comment
Please, Sign In to add comment