from sympy import *
init_printing()
x1 = Matrix([1,1,1])
x2 = Matrix([0,1,0])
x3 = Matrix([-1,1,0])
GramSchmidt([x1,x2,x3])
a1 = x1/x1.norm()
a1
y2 = x2-x2.dot(a1)*a1
a2 = y2/y2.norm()
a2
y3 = x3 - x3.dot(a1)*a1-x3.dot(a2)*a2
y3
a3 = y3/y3.norm()
a3
x1,x2,x3,a,b=symbols('x1,x2,x3,a,b')
eq1=2*x1-x2+x3
eq2=x1-3*x2+x3
solve({eq1,eq2},{x1,x2,x3})
eq3 = expand(5/a*(-Rational(2,5)*a*x1+Rational(1,5)*a*x2+a*x3))
solve(eq3.subs({x2:a,x3:b}),x1)
a*Matrix([Rational(1,2),1,0]) + b*Matrix([Rational(5,2),0,1])
from sympy import *
init_session()
t = symbols('t')
v = exp(-t)+1.0
IPython console for SymPy 1.0 (Python 3.6.1-64-bit) (ground types: python) These commands were executed: >>> from __future__ import division >>> from sympy import * >>> x, y, z, t = symbols('x y z t') >>> k, m, n = symbols('k m n', integer=True) >>> f, g, h = symbols('f g h', cls=Function) >>> init_printing() Documentation can be found at http://docs.sympy.org/1.0/
vs = v.series(t,0,6)
vs
%matplotlib inline
vsO = vs.removeO()
p = plot(v, vsO, (t,0,2), ylim=[1,2], show=False)
p[0].line_color = 'b'
p[1].line_color = 'r'
p.show()
i_v = integrate(v,(t,0,2))
pprint(i_v)
i_v.evalf()
-2 - 1.0⋅ℯ + 3.0
i_vs = integrate(vsO,(t,0,2))
pprint(i_vs)
i_vs.evalf()
2.84444444444444
誤差をわかりやすくするには,下のようにまとめれば良い.そうすると必要な次数は7(8)次であることがわかる.
vs = v.series(t,0,10)
pprint(vs)
vsO = vs.removeO()
i_vs = integrate(vsO,(t,0,2))
pprint(i_vs)
i_vs.evalf()-i_v.evalf()
2 3 4 5 6 7 8 9 t t t t t t t t ⎛ 10⎞ 2.0 - t + ── - ── + ── - ─── + ─── - ──── + ───── - ────── + O⎝t ⎠ 2 6 24 120 720 5040 40320 362880 2.86462081128748
2点Q($x_1,y_1$)とR($x_2,y_2$)を通る直線の方程式は $$ y-y_1 = \frac{y_1-y_2}{x_1-x_2}(x-x_1) $$ で求められる.
from sympy import *
init_printing()
b, x, t = symbols('b,x,t')
y_c = 1-x**2
plot(y_c,(x,-2,2))
<sympy.plotting.plot.Plot at 0x112dec438>
s_c = solve(y_c,x)
s_c
x1=s_c[0]
y1=0
x2=s_c[1]-b
y2=y_c.subs({x:x2})
m = (y1-y2)/(x1-x2)
simplify(m)
y_m = expand(simplify(m)*(x+1))
y_m
plot(y_c,y_m.subs({b:0.7}),(x,-2,2))
<sympy.plotting.plot.Plot at 0x11cee22e8>
s1=expand(integrate(y_c-y_m,(x,-1,1-b)))
s1
s2=expand(integrate(y_m-y_c,(x,1-b,b)))
s2
expand(s1+s2)
plot(s1+s2,(b,0.5,1))
<sympy.plotting.plot.Plot at 0x119885438>
solve(diff(s1+s2,b),b)
放物線$C$の方程式を$y=1-0.5x^2$として問題を解く. 放物線$C$上の2点Q($-\sqrt{2},0$)とR($\sqrt{2}-b,1-(\sqrt{2}-b)^2$)と読み替える.また,$S_2$を求めるときの範囲は$\sqrt{2}-b \leq x \leq b$と読み替える.
from sympy import *
init_printing()
b, x, t = symbols('b,x,t')
# y_c = 1-0.5*x**2
y_c = 1-Rational(1,2)*x**2
plot(y_c,(x,-2,2))
<sympy.plotting.plot.Plot at 0x115d64c88>
s_c = solve(y_c,x)
s_c
x1=s_c[0]
y1=0
x2=s_c[1]-b
y2=y_c.subs({x:x2})
y_m = simplify((y1-y2)/(x1-x2)*(x-x1)+y1)
y_m
s1= expand(integrate(y_c-y_m,(x,x1,x2)))
simplify(s1)
s2=expand(integrate(y_m-y_c,(x,x2,b)))
s2
plot(s1+s2,(b,1/2,s_c[1]))
<sympy.plotting.plot.Plot at 0x10da6cef0>
solve(diff(s1+s2,b),b)
solve(diff(s1+s2,b),b)[1].evalf()
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-15-3fb892d5818b> in <module>() ----> 1 solve(diff(s1+s2,b),b)[1].evalf() IndexError: list index out of range
solve(simplify(diff(s1+s2,b)),b)
solve(simplify(diff(s1+s2,b)),b)[1].evalf()
与関数を $$y=1-\frac{1}{2}x^2$$ つまり
y=1-Rational(1,2)*x**2
とRationalを明示的に使えば.答えは, $$ \left [ - \sqrt{14} - 2 \sqrt{2}, \quad - 2 \sqrt{2} + \sqrt{14}\right ] $$ と解析的に求められる.後ろ側が求めた数値解と一致する. その場合,
solve(simplify(diff(s1+s2,b)),b)
としないと求められない.
2015 年度大学入試センター試験 追試 数学 II・B 第 2 問 (1)の解答例を参考に示しておく(苦労して解いたんで).
from sympy import *
init_printing()
a, x, t = symbols('a,x,t')
y_c = 1-x**2
plot(y_c,(x,-2,2))
<sympy.plotting.plot.Plot at 0x10a3d8748>
m = diff(y_c,x)
m
y_l=collect(expand(m.subs({x: a})*(x-a)+y_c.subs({x: a})),x)
y_l
plot(y_l.subs({a:1}),y_c,(x,-2,2))
<sympy.plotting.plot.Plot at 0x114b0f0b8>
点($x_0,y_0$)と直線($c_ax+c_by+c_c=0$)の距離($h$)の公式 $$ h = \frac{|c_ax_0+c_by_0+c_c|}{\sqrt{c_a^2+c_b^2}} $$
c_a=y_l.coeff(x)
c_c=y_l.coeff(x,0)
c_b=-1
h = (c_a*0+c_b*0+c_c)/sqrt(c_a**2+c_b**2)
h
a_2 = solve(sqrt(4*a**2+1)**2-t**2,a**2)[0]
simplify((a_2+1)/t)
plot(h,(a,-1,1))
<sympy.plotting.plot.Plot at 0x114b64160>
s1 = solve(diff(h,a),a)
s1
h.subs({a:s1[1]})