SharedWORK2_POLINOM_V.ipynbOpen in CoCalc
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'seaborn-notebook', u'classic', u'seaborn-ticks', u'grayscale', u'bmh', u'seaborn-talk', u'dark_background', u'ggplot', u'fivethirtyeight', 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'seaborn-pastel', u'seaborn-dark', 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))
[3.252216865271419, 3.119726238415282, 2.994245015909533, 2.8756202708283363, 2.76369907624586, 2.65832850523627, 2.559355630873733, 2.466627526232415, 2.3799912643864833, 2.299293918410103, 2.2243825613774426, 2.1551042663626676, 2.0913061064399434, 2.032835154683438, 1.9795384841673171, 1.9312631679657484, 1.8878562791528966, 1.8491648908029288, 1.8150360759900115, 1.7853169077883122, 1.7598544592719958, 1.7384958035152307, 1.7210880135921813, 1.707478162577015, 1.6975133235438986, 1.6910405695669979, 1.6879069737204804, 1.6879596090785105, 1.6910455487152567, 1.6970118657048852, 1.7057056331215632, 1.7169739240394555, 1.7306638115327286, 1.74662236867555, 1.764696668542085, 1.7847337842065023, 1.806580788742965, 1.8300847552256432, 1.8550927567287014, 1.881451866326307, 1.909009157092624, 1.9376117021018233, 1.9671065744280685, 1.9973408471455256, 2.028161593328362, 2.059415886050742, 2.090950798386837, 2.122613403410809, 2.1542507741968278, 2.1857099838190583, 2.2168381053516653, 2.2474822118688182, 2.2774893764446835, 2.306706672153422, 2.3349811720692095, 2.3621599492662044, 2.388090076818578, 2.4126186278004935, 2.4355926752861183, 2.4568592923496206, 2.4762655520651684, 2.4936585275069243, 2.5088852917490545, 2.521792917865728, 2.53222847893111, 2.5400390480193664, 2.5450716982046693, 2.5471735025611757, 2.546191534163057, 2.541972866084482, 2.5343645713996175, 2.5232137231826197, 2.5083673945076708, 2.4896726584489173, 2.4669765880805503, 2.440126256476722, 2.408968736711591, 2.3733511018593436, 2.333120424994128, 2.2881237791901228, 2.2382082375214907, 2.183220873062396, 2.123008758887007, 2.0574189680694914, 1.9862985736840137, 1.9094946488047349, 1.826854266505837, 1.7382244998614702, 1.6434524219458133, 1.5423851058330236, 1.434869624597269, 1.320753051312721, 1.1998824590535442, 1.0721049208939135, 0.9372675099079792, 0.7952172991699094, 0.6458013617538789, 0.48886677073404883, 0.32426059918458705, 0.15182992017966868, -0.02857819320654542, -0.21711666789989792, -0.41393843082622794, -0.6191964089113355, -0.8330435290810883, -1.0556327182613003, -1.2871169033778074, -1.527649011356445, -1.7773819691230486, -2.036468703603454, -2.3050621417234964, -2.5833152104090047, -2.8713808365858213, -3.1694119471797535, -3.477561469116658, -3.795982329322378, -4.124827454722713, -4.4642497722435195, -4.814402208810641, -5.175437691349877, -5.5475091467870925, -5.930769502048108, -6.325371684058766, -6.7314686197448665, -7.149213236032288, -7.57875845984686, -8.020257218114388, -8.473862437760708, -8.939727045711678, -9.418003968893096, -9.908846134230842, -10.412406468650708, -10.928837899078573, -11.458293352440208, -12.000925755661484, -12.556888035668266, -13.126333119386295, -13.709413933741516, -14.306283405659691, -14.917094462066657, -15.542000029888285, -16.181153036050333, -16.834706407478734, -17.50281307109927, -18.185625953837786, -18.88329798262008, -19.595982084372025, -20.32383118601946, -21.06699821448818, -21.82563609670403, -22.599897759592892, -23.389936130080528, -24.195904135092817, -25.01795470155558, -25.85624075639467, -26.71091522653589, -27.58213103890506, -28.470041120428036, -29.37479839803069, -30.296555798638806, -31.235466249178216, -32.19168267657477, -33.165358007754335, -34.15664516964266, -35.16569708916566, -36.19266669324912, -37.23770690881892, -38.30097066280079, -39.38261088212069, -40.48278049370441, -41.601632424477785, -42.73931960136659, -43.89599495129676, -45.07181140119401, -46.26692187798429, -47.48147930859335, -48.71563661994708, -49.9695467389713, -51.24336259259178, -52.53723710773443, -53.85132321132507, -55.18577383028952, -56.54074189155358, -57.91638032204317, -59.312842048684104, -60.7302799984021, -62.16884709812311, -63.62869627477298, -65.10998045527742, -66.61285256656234, -68.13746553555363, -69.68397228917706, -71.25252575435846, -72.84327885802364, -74.45638452709858, -76.09199568850889, -77.75026526918052, -79.43134619603933, -81.1353913960111, -82.86255379602176]
plt.plot(dis, var, dis, Polin_func2(dis, koeff=c),'-b')
plt.show
<function matplotlib.pyplot.show>
% 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.65 ms, sys: 0 ns, total: 2.65 ms Wall time: 14.2 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.39 ms, sys: 566 µs, total: 1.96 ms Wall time: 1.9 ms 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 7.59 ms, sys: 0 ns, total: 7.59 ms Wall time: 10.5 ms [ 25.88020657] -11.8988946659 CPU times: user 577 µs, sys: 0 ns, total: 577 µs Wall time: 531 µ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)
[29, 15, 27, 10, 3, 24, 29, 24, 29, 3] 10 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 10
c =  koeff_search(b, a)
[ 2.90000000e+01 4.23968254e+01 -2.29717063e+02 3.17045392e+02 -1.99559028e+02 6.75873843e+01 -1.31361111e+01 1.46822090e+00 -8.77976190e-02 2.17702822e-03]
hu =