関数の最小値

モジュールのインポート

import numpy as np
import scipy as sp
from scipy.optimize import minimize
import matplotlib.pyplot as plt
import scienceplots
#Warning : As of version 2.0.0, you need to add import scienceplots before setting the style (plt.style.use('science')).
plt.style.use(['science', 'notebook'])

関数のプロット

関数 $f(x)$ が最小となる $x$ を求める前に,$f(x)$ をプロットしよう.
def func(x):
    return (x-1)*(x-2)*(x-2.5)*(x-4)
x = np.linspace(0.5,4.5,101)
x
array([0.5 , 0.54, 0.58, 0.62, 0.66, 0.7 , 0.74, 0.78, 0.82, 0.86, 0.9 ,
       0.94, 0.98, 1.02, 1.06, 1.1 , 1.14, 1.18, 1.22, 1.26, 1.3 , 1.34,
       1.38, 1.42, 1.46, 1.5 , 1.54, 1.58, 1.62, 1.66, 1.7 , 1.74, 1.78,
       1.82, 1.86, 1.9 , 1.94, 1.98, 2.02, 2.06, 2.1 , 2.14, 2.18, 2.22,
       2.26, 2.3 , 2.34, 2.38, 2.42, 2.46, 2.5 , 2.54, 2.58, 2.62, 2.66,
       2.7 , 2.74, 2.78, 2.82, 2.86, 2.9 , 2.94, 2.98, 3.02, 3.06, 3.1 ,
       3.14, 3.18, 3.22, 3.26, 3.3 , 3.34, 3.38, 3.42, 3.46, 3.5 , 3.54,
       3.58, 3.62, 3.66, 3.7 , 3.74, 3.78, 3.82, 3.86, 3.9 , 3.94, 3.98,
       4.02, 4.06, 4.1 , 4.14, 4.18, 4.22, 4.26, 4.3 , 4.34, 4.38, 4.42,
       4.46, 4.5 ])
y = func(x)
y
array([ 5.25      ,  4.55452256,  3.91620096,  3.33224736,  2.79993536,
        2.3166    ,  1.87963776,  1.48650656,  1.13472576,  0.82187616,
        0.5456    ,  0.30360096,  0.09364416, -0.08644384, -0.23877504,
       -0.3654    , -0.46830784, -0.54942624, -0.61062144, -0.65369824,
       -0.6804    , -0.69240864, -0.69134464, -0.67876704, -0.65617344,
       -0.625     , -0.58662144, -0.54235104, -0.49344064, -0.44108064,
       -0.3864    , -0.33046624, -0.27428544, -0.21880224, -0.16489984,
       -0.1134    , -0.06506304, -0.02058784,  0.01938816,  0.05428896,
        0.0836    ,  0.10686816,  0.12370176,  0.13377056,  0.13680576,
        0.1326    ,  0.12100736,  0.10194336,  0.07538496,  0.04137056,
       -0.        , -0.04856544, -0.10410304, -0.16632864, -0.23489664,
       -0.3094    , -0.38937024, -0.47427744, -0.56353024, -0.65647584,
       -0.7524    , -0.85052704, -0.95001984, -1.04997984, -1.14944704,
       -1.2474    , -1.34275584, -1.43437024, -1.52103744, -1.60149024,
       -1.6744    , -1.73837664, -1.79196864, -1.83366304, -1.86188544,
       -1.875     , -1.87130944, -1.84905504, -1.80641664, -1.74151264,
       -1.6524    , -1.53707424, -1.39346944, -1.21945824, -1.01285184,
       -0.7714    , -0.49279104, -0.17465184,  0.18545216,  0.59001696,
        1.0416    ,  1.54282016,  2.09635776,  2.70495456,  3.37141376,
        4.0986    ,  4.88943936,  5.74691936,  6.67408896,  7.67405856,
        8.75      ])
fig = plt.figure() # グラフ領域の作成
plt.plot(x,y)
fig.savefig('p1_scipy_s1_1.pdf')

関数 minimize

https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html
初期値を3として,
res = minimize(func, 3)
res
fun: -1.8757366055407292
hess_inv: array([[0.09240358]])
     jac: array([0.])
 message: 'Optimization terminated successfully.'
    nfev: 16
     nit: 5
    njev: 8
  status: 0
 success: True
       x: array([3.51172731])
よって,最小値は,
res.x
array([3.51172731])