Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: one
Views: 239
Kernel: Python 2 (system-wide)
import pandas as pd import numpy as np from scipy import optimize, linalg import scipy from scipy import spatial import re import math import pandas as pd from numpy import zeros, dot, savetxt import matplotlib from matplotlib import pylab as plt %matplotlib inline
plt.style.use('ggplot') print(plt.style.available)
[u'seaborn-darkgrid', u'Solarize_Light2', u'seaborn-notebook', u'classic', u'seaborn-ticks', u'grayscale', u'bmh', u'seaborn-talk', u'dark_background', u'ggplot', u'fivethirtyeight', u'_classic_test', u'seaborn-colorblind', u'seaborn-deep', u'seaborn-whitegrid', u'seaborn-bright', u'seaborn-poster', u'seaborn-muted', u'seaborn-paper', u'seaborn-white', u'fast', u'seaborn-pastel', u'seaborn-dark', u'seaborn', u'seaborn-dark-palette']
x = [0.24, 0.26, 0.27, 0.29, 0.30, 0.32] y = [1/2711, 1.297, 1.310, 1.336, 1.350, 1.377]
def Lin_func(vector): znach_v = [] for znach in vector: znach_v += [math.sin(znach / 5) * math.exp(znach / 10) + 5 * math.exp(-znach / 2)] return znach_v def EDin_func(znach): return math.sin(znach / 5) * math.exp(znach / 10) + 5 * math.exp(-znach / 2)
a = np.linspace(1, 15, num = 100) #a, len(a) #print Lin_func(a), b = Lin_func(a)
b = [EDin_func(1.), EDin_func(15.)] b
[3.252216865271419, 0.6352214195786656]
izn3 = [1., 4., 10., 15.] #ввод точек fzn3 = Lin_func(izn3)
koeff = [] def koeff_search(izn3 = izn3, fzn3 = fzn3): work_m = izn3 znach_func = fzn3 for i in range(len(work_m)): #из списка в матрицу work_m[i] = [work_m[i]] for i in range(len(work_m)): #умножения количества элементов work_m[i] = work_m[i] * (len(work_m)) N = 0 for i in range(len(work_m)): #создание матрицы квадратов for j in range(len(work_m)): work_m[i][j] = work_m[i][j] ** N N += 1 N = 0 koeff = np.linalg.solve(work_m, znach_func) #Находим решение системы print koeff return koeff
def Polin_func1(vector, koeff = koeff): znach_f = [] for znach in vector: znach_f += [koeff[0] + ((koeff[1]) * znach) + (koeff[2] * (znach ** 2)) + ((koeff[3]) * (znach ** 3))] return znach_f
def Polin_func2(vector, koeff = koeff): #функция нахождения полинома znach_v = [] shab = [] for znach in vector: for i in range(len(koeff)): shab += [koeff[i] * (znach ** i)] znach_v += [float(sum(shab))] shab = [] return znach_v
dis = np.linspace(1, 30, num = 200) var = Lin_func(dis) for i in range(len(var)): var[i] = (var[i])
print(koeff)
[]
print(Polin_func2(dis, koeff=c))
[18.000000000263807, 19.388014987838822, 19.342195328299884, 18.137311459303127, 16.12720435913431, 13.67469559277156, 11.10561857515768, 8.68206959273242, 6.590556745789898, 4.941273353330649, 3.7752394986730877, 3.0765412883515815, 2.7873520485411483, 2.8238430926616545, 3.0914838605580997, 3.498592154331533, 3.967324877698303, 4.441598125486962, 4.89169266688657, 5.315536821104956, 5.736863435677606, 6.200611148403937, 6.766082340652929, 7.498481174676499, 8.459534850746877, 9.697949719360622, 11.24047114161067, 13.084302006341204, 15.191589584690519, 17.486613933935587, 19.856203350234864, 22.153763414375135, 24.20713597838403, 25.830303002639994, 26.838717468424875, 27.067779670411255, 26.393682020163396, 24.755519094193005, 22.177201995655196, 18.78732721050619, 14.834730007321923, 10.697001040389296, 6.878762204985833, 3.9959839230577927, 2.7420809270406608, 3.8309472879045643, 7.911483787524048, 15.447531974699814, 26.55645910097519, 40.79893685062416, 56.9117242192151, 72.47450108104385, 83.5010039123008, 83.94388893526047, 65.10189031262416, 14.916952305706218, -82.85190567583777, -249.59218357899226, -512.9541295336094, -908.1357917110436, -1479.3541267537512, -2281.5204745964147, -3382.139817019459, -4863.454375889152, -6824.853277096525, -9385.571207303554, -12687.700223124586, -16899.54013605602, -22219.314191252925, -28879.278084699064, -37150.25172036979, -47346.60449790768, -59831.72634124383, -75024.01812935248, -93403.43667332269, -115518.63089700975, -141994.70742345415, -173541.66534611955, -210963.5415697433, -255168.30974625424, -307178.57749915123, -368143.12833205983, -439349.3563488871, -522236.6436769888, -618410.7322788388, -729659.1436647475, -857967.7018759102, -1005538.2169947773, -1174807.388361819, -1368466.988625832, -1589485.391739808, -1841130.510026157, -2126994.207480192, -2451018.258558184, -2817521.9238004535, -3231231.2157821953, -3697309.931051582, -4221392.525916129, -4809618.9161723405, -5468671.283135235, -6205812.970618248, -7028929.559844017, -7946572.2116175, -8968003.367487997, -10103244.904041111, -11363128.836914748, -12759350.67361468, -14304525.516722381, -16012247.021623015, -17897149.315469205, -19974971.986694276, -22262628.25703132, -24778276.45066446, -27541394.877833128, -30572860.252954423, -33895029.77007741, -37531826.96129143, -41508831.46652508, -45853372.84603584, -50594628.569779515, -55763726.320765495, -61393850.75244379, -67520354.84318864, -74180875.99390078, -81415457.0178467, -89266672.17490053, -97779758.40546441, -107002751.92249227, -116986630.32320213, -127785460.38525891, -139456551.71545672, -152060616.42218304, -165661934.98622704, -180328528.50786567, -196132337.51145196, -213149407.49218702, -231460081.39311647, -251149199.20388794, -272306304.8762617, -295025860.7548952, -319407469.7254424, -345556105.28562737, -373582349.748507, -403602640.79082155, -435739526.5629673, -470121929.58084106, -506885419.6235509, -546172495.8647175, -588132878.4689126, -632923809.8886027, -680710366.1008053, -731665778.0265827, -785971763.3803835, -843818869.2002144, -905406825.3136206, -970944908.998414, -1040652321.1011963, -1114758573.8807487, -1193503890.8475113, -1277139618.8744392, -1365928652.85884, -1460145873.2187548, -1560078596.5119734, -1666027039.4697742, -1778304796.7419376, -1897239332.6538467, -2023172487.2808323, -2156460997.1493587, -2297477030.879032, -2446608740.083805, -2604260825.855385, -2770855121.1562576, -2946831189.4542236, -3132646939.93507, -3328779259.634535, -3535724662.835335, -3753999958.0796833, -3984142933.152542, -4226713058.395462, -4482292208.715668, -4751485404.659779, -5034921572.926628, -5333254326.698044, -5647162766.171917, -5977352299.686169, -6324555485.827873, -6689532896.92606, -7073074004.332233, -7475998085.89772, -7899155156.061447, -8343426918.967663, -8809727745.037498, -9299005671.424149, -9812243426.785961, -10350459480.817482, -10914709118.983505, -11506085542.906525, -12125720996.863388, -12774787920.852089, -13454500130.695366]
plt.plot(dis, var, dis, Polin_func2(dis, koeff=c),'-b') plt.show
<function matplotlib.pyplot.show>
Image in a Jupyter notebook
% time res = scipy.optimize.minimize(EDin_func, 2 ) print res,'\n',res.x,'\n', res.message,'\n', res.hess_inv %time res = scipy.optimize.minimize(EDin_func, 30, method='BFGS' ) print res,'\n',res.x,'\n', res.message,'\n', res.hess_inv %time res = scipy.optimize.differential_evolution(EDin_func, [(0, 30)]) print res.x, res.fun %time res = scipy.optimize.minimize(Int_EDin_func, 30, method='BFGS' ) print res,'\n',res.x,'\n', res.message,'\n', res.hess_inv
CPU times: user 2.93 ms, sys: 0 ns, total: 2.93 ms Wall time: 2.38 ms fun: 1.7452682903449388 hess_inv: array([[ 5.98752437]]) jac: array([ -2.07126141e-06]) message: 'Optimization terminated successfully.' nfev: 21 nit: 6 njev: 7 status: 0 success: True x: array([ 4.13627618]) [ 4.13627618] Optimization terminated successfully. [[ 5.98752437]] CPU times: user 1.19 ms, sys: 0 ns, total: 1.19 ms Wall time: 877 µs fun: -11.898894665981285 hess_inv: array([[ 1.67932484]]) jac: array([ 2.38418579e-07]) message: 'Optimization terminated successfully.' nfev: 21 nit: 6 njev: 7 status: 0 success: True x: array([ 25.88019339]) [ 25.88019339] Optimization terminated successfully. [[ 1.67932484]] CPU times: user 2.87 ms, sys: 0 ns, total: 2.87 ms Wall time: 2.85 ms [ 25.88019288] -11.898894666 CPU times: user 190 µs, sys: 0 ns, total: 190 µs Wall time: 159 µs fun: -5 hess_inv: array([[1]]) jac: array([ 0.]) message: 'Optimization terminated successfully.' nfev: 3 nit: 0 njev: 1 status: 0 success: True x: array([ 30.]) [ 30.] Optimization terminated successfully. [[1]]
def Int_EDin_func(znach): return int(math.sin(znach / 5) * math.exp(znach / 10) + 5 * math.exp(-znach / 2))
koeff
[]
c = koeff_search()
[ 4.36264154 -1.29552587 0.19333685 -0.00823565]
import random a = [] b = range(10) for i in range(10): a += [random.randint(1, 30)]
print a, len(a), '\n', b, len(b)
[12, 27, 7, 19, 23, 24, 30, 11, 2, 5] 10 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 10
c = koeff_search(b, a)
[ 1.20000000e+01 1.30229365e+02 -1.90641270e+02 8.07185626e+01 3.64652777e+00 -1.23655093e+01 3.95277778e+00 -5.81216931e-01 4.19642857e-02 -1.20149912e-03]
hu =