以下の問題をpythonで解き,LUNAへ提出せよ.LUNAへはipynbとpdf形式の2種類を提出すること.
from sympy import *
x, y = symbols('x y')
f = 1/(1+exp(-x))
f
df = f.diff(x)
df
df2 = f.diff(x,x)
df2
%matplotlib inline
plot(f,df,df2,(x,-5,5))
<sympy.plotting.plot.Plot at 0x7fd634e6db50>
solve(df,x)
[]
solve(df2,x)
[0]
x | $-\infty$ | $\cdots$ | 0 | $\cdots$ | $\infty$ |
---|---|---|---|---|---|
f(x) | 0 | $\nearrow$ | 0.5 | $\nearrow$ | 0 |
f'(x) | 0 | + | + | + | 0 |
f''(x) | 0 | + | 0 | - | 0 |
3変数のシグモイド関数で,1変数を固定すると次のような関数となる.
import numpy as np
def softmax(x,y):
return np.exp(-x)/(np.exp(-x)+np.exp(-y)+np.exp(-1))
この関数を
x = np.arange(-4, 4, 0.5)
y = np.arange(-4, 4, 0.5)
で3次元プロットせよ.
import numpy as np
def softmax(x,y):
return np.exp(-x)/(np.exp(-x)+np.exp(-y)+np.exp(-1))
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
%matplotlib inline
x = np.arange(-3, 3, 0.25)
y = np.arange(-3, 3, 0.25)
X, Y = np.meshgrid(x, y)
Z1 = softmax(X,Y)
fig = plt.figure()
plot3d = Axes3D(fig)
plot3d.plot_surface(X,Y,Z1)
plt.show()
from sympy import *
w0,w1,w2 = symbols('w0,w1,w2')
x0,x1,x2 = symbols('x0,x1,x2')
ww = Matrix([w0,w1,w2])
xx = Matrix([x0,x1,x2])
ww
ww.T*xx
ww*xx.T
xdata=np.array([1,2,3,4])
ydata=np.array([0,5,15,24])
を対象データとして,(5-53)にしたがって,N=4, n=3で $$ y=a_0 + a_1\,x +a_2\, x^2 $$ に対するfittingを行う.得られたデザイン行列$X$は $$X=\left( \begin{array}{@{\,}ccc@{\,}} 1 & 1 & 1\\ 1 & 2 & 4\\ 1 & 3 & 9\\ 1 & 4 & 16\\\end{array} \right)$$ となる.(5-59)式の左辺の$X^TX$が3x3行列になることを確認せよ.
ヒント:https://nbviewer.jupyter.org/github/daddygongon/jupyter_num_calc/blob/master/numerical_calc/least_square_fit.ipynb の「正規方程式(Normal Equations)による解」の「python codeによる具体例」を参照せよ.
import numpy as np
from pprint import pprint
import scipy.linalg as linalg
X = Matrix([[ 1., 1., 1.],
[ 1., 2., 4.],
[ 1., 3., 9.],
[ 1., 4., 16.]])
np.dot(X.T,X).shape
(3, 3)
(2018大学入試センター試験 追試験 数学II・B 第2問)
$a$ を正の実数とし, 放物線$y=3x^2$ を$C_1$, 放物線$y=2x^2+a^2$ を$C_2$ とする. $C_1$ と$C_2$ の二つの共有点を $x$ 座標の小さい順にA,Bとする. また,$C_1$ と$C_2$ の両方に第1象限で接する直線を$l$ とする.
(1) Bの座標を$a$ を用いて表すと $(\fbox{ ア }\,, \fbox{ イ }\,a^{ \fbox{ ウ }})$ である
from sympy import *
a, s, t, x = symbols('a, s, t, x')
yC1 = 3*x**2
yC1
yC2 = 2*x**2+a**2
yC2
eq = yC1 - yC2
xB = solve(eq,x)[1]
xB
yC2.subs({x:xB})
直線$l$ と二つの放物線$C_1, C_2$ の接点の$x$ 座標をそれぞれ$s,t$ とおく. $l$ は$x=s$ で$C_1$ と接するので,$l$ の方程式は \begin{equation*} y = \fbox{ エ }\,sx - \fbox{ オ }\,s^{ \fbox{ カ }} \end{equation*} と表せる. 同様に,$l$ は$x=t$ で$C_2$ と接するので,$l$ の方程式は \begin{equation*} y = \fbox{ キ }\,tx - \fbox{ ク }\,t^{ \fbox{ カ }} + a^2 \end{equation*} とも表せる.これらにより,$s,t$ は \begin{equation*} s = \frac{\sqrt{\fbox{ ケ }}}{\fbox{ コ }}a , \,\,\,\, t= \frac{\sqrt{\fbox{ ケ }}}{\fbox{ サ }}a \end{equation*} である.
m1 = diff(yC1)
#y - y0 = m*(x-x0)
yl1 = m1.subs({x:s})*(x-s)+yC1.subs({x:s})
yl1.expand()
m2 = diff(yC2,x)
yl2 = m2.subs({x:t})*(x-t)+yC2.subs({x:t})
yl2.expand()
t0=solve(m1.subs({x:s})-m2.subs({x:t}),t)[0]
t0
s0=solve((yl1-yl2).subs({x:0}).subs({t:t0}),s)[1]
s0
t0.subs({s:s0})
放物線$C_1$ の$s \leqq x \leqq {\fbox{ ア }}$ の部分 放物線$C_2$ の${\fbox{ ア }} \leqq x \leqq t$ の部分, $x$ 軸, および2直線$x=s, x=t$で囲まれた図形の面積は \begin{equation*} \frac{\fbox{ シ }\sqrt{\fbox{ ス }}-\fbox{ セ }}{\fbox{ ソ }}a^{\fbox{ タ }} \end{equation*} である.
SS=integrate(yC1,(x,s,a))+integrate(yC2,(x,a,t))
together(SS.subs({t:t0}).subs({s:s0}))
問3.において,放物線$C_1$ が $$ y = 2.9 x^2 $$ である場合について解きなさい. ただし,係数が浮動小数点数に変わったので,$\fbox{ ア }\,, \fbox{ イ }$などには浮動小数点数が入る.最後の図形の面積は,$1.284186\ldots a^3$ となる.(30点)
from sympy import *
a, s, t, x = symbols('a, s, t, x')
yC1 = 2.9*x**2
yC1
yC2 = 2*x**2+a**2
yC2
eq = yC1 - yC2
xB = solve(eq,x)[1]
xB
yC2.subs({x:xB})
m1 = diff(yC1)
#y - y0 = m*(x-x0)
yl1 = m1.subs({x:s})*(x-s)+yC1.subs({x:s})
yl1.expand()
m2 = diff(yC2,x)
yl2 = m2.subs({x:t})*(x-t)+yC2.subs({x:t})
yl2.expand()
t0=solve(m1.subs({x:s})-m2.subs({x:t}),t)[0]
t0
s0=solve((yl1-yl2).subs({x:0}).subs({t:t0}),s)[1]
s0
t0.subs({s:s0})
SS=integrate(yC1,(x,s,a))+integrate(yC2,(x,a,t))
together(SS.subs({t:t0}).subs({s:s0}))
(-6+7*sqrt(6.0))/9