1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

センター数学2B(2019年)をPythonで解いてみた

Last updated at Posted at 2019-02-27
  • 実行環境
  • VirtualBox 5.2.2
  • Cent 6.9
  • Python 3.5.4

分数や根号が計算されるので、無理やり分解して表示している箇所があります。

第1問

(1)

2倍角の定理は明示とする。
θの値は出力から判断にしましたが、やり方次第で最大値のみ出力にできると思います。

プログラム

# -*- coding: utf-8 -*-

from sympy import *
import math

# シータの代わり
a = symbols('a')
# パイの代わり
b = symbols('b')
sin = symbols('sin')
sin2 = symbols('sin2')
cos = symbols('cos')
cos2 = symbols('cos2')

f = 3*sin**2+4*sin*cos-cos**2
print("f(0) =", f.subs(sin, math.sin(math.radians(0))).subs(cos, math.cos(math.radians(0))))
#print("f(pi/3) =", f.subs(sin, math.sin(math.pi/3)).subs(cos, math.cos(math.pi/3)))
print("f(pi/3) =", f.subs(sin, sqrt(3)/2).subs(cos, 1/2))

ff = f.subs(sin**2, (1-cos2)/2).subs(cos**2, (cos2+1)/2).subs(sin*cos, sin2/2)
print(ff)
coeffSin2 = ff.coeff(sin2, 1)
coeffCos2 = ff.coeff(cos2, 1)
x = round(coeffSin2/(math.sqrt(coeffSin2**2+coeffCos2**2)), 2)
y = round(coeffCos2/(math.sqrt(coeffSin2**2+coeffCos2**2)), 2)

radList = (-5*b/6, -3*b/4, -2*b/3, -1*b/2, -b/3, -b/4, -b/6, 0*b, b/6, b/4, b/3, b/2, 2*b/3, 3*b/4, 5*b/6, 1*b)
num = 0
for radian in radList:
    if x == round(math.cos(radian.subs(b, math.pi)),2) and y == round(math.sin(radian.subs(b, math.pi)),2):
        num = radian

z = sqrt(coeffSin2**2+coeffCos2**2)
fff = z*sin*(2*a+num)+ff.subs(sin2, 0).subs(cos2, 0)
print(fff)

sinList = (0, 1/2, 1/sqrt(2), sqrt(3)/2, 1, sqrt(3)/2, 1/sqrt(2), 1/2, 0)
radList = ( 0*b, b/6, b/4, b/3, b/2, 2*b/3, 3*b/4, 5*b/6, 1*b)

cnt = 0
for sinNum in sinList :
    print("f(%s) = %s" % (solve(2*a+num-radList[cnt], a)[0], fff.subs(sin*(2*a+num), sinNum)))
    cnt += 1

出力

f(0) = -1.00000000000000
f(pi/3) = 1.0*sqrt(3) + 2.0
-2*cos2 + 2*sin2 + 1
2*sqrt(2)*sin*(2*a - b/4) + 1
f(b/8) = 1
f(5*b/24) = 1 + 1.0*sqrt(2)
f(b/4) = 3
f(7*b/24) = 1 + sqrt(6)
f(3*b/8) = 1 + 2*sqrt(2)
f(11*b/24) = 1 + sqrt(6)
f(b/2) = 3
f(13*b/24) = 1 + 1.0*sqrt(2)
f(5*b/8) = 1

解答
ア:-、イ:1、ウ:2、エ:3、オ:1
カ:2、キ:2、ク:2、ケ:1、コ:2
サ:2、シ:4、ス:3、セ:4、ソ:2

(2)

真数条件や指数関数が0より大きいことは明示としています。

プログラム

#import matplotlib.pyplot as plt

x = symbols('x')
y = symbols('y')
# 1/3のかわり
a = symbols('a')
t = symbols('t')

f = log(x+2, 2)-2*log(y+3, 4)+1
g = a**y-11*a**(x+1)+6

print(reduce_rational_inequalities([[x+2 > 0]], x).args[0])
print(reduce_rational_inequalities([[y+3 > 0]], y).args[0])

subFormula = log(y+3, 2)/log(4, 2)
print(subFormula)
ff = f.subs(log(y+3, 4), subFormula)
y_value = solve(ff, y)[0]
print("y =", y_value)

gg = g.subs(y, y_value).expand().subs(a**x, t).subs(a**(2*x), t**2).subs(a, 1/3)*3
print(gg)
num = int((1/3)**solve(x+2, x)[0])
print("%d < t < %s" % (0, num))
for ans in solve(gg, t) :
    if ans > 0 and ans < num :
        t_value = ans

print("t =", t_value)
print("x = log3_1/", int(t_value))
tmpNum=1/int(t_value)**y_value.coeff(x, 1)*3*y_value.subs(x, 0)
print("y = log3_", tmpNum)

出力

-2 < x
-3 < y
log(y + 3)/(2*log(2))
y = 2*x + 1
t**2 - 11.0*t + 18
0 < t < 9
t = 2.00000000000000
x = log3_1/ 2
y = log3_ 3/4

解答
タ:2、チ:2、ツ:2、テ:1、ト:1
ナ:1、ニ:1、ヌ:8、ネ:0、ノ:9
ハ:2、ヒ:1、フ:2、ヘ:3、ホ:4

第2問

微分した関数は極値と取る時のxで0になることは明示とします。

プログラム

# -*- coding: utf-8 -*-

from sympy import *
import math
import numpy as np

x = symbols('x')
p = symbols('p')
q = symbols('q')
k = symbols('k')
a = symbols('a')
b = symbols('b')

f = x**3+p*x**2+q*x
g = -k*x**2

ff = diff(f, x)

p_value = solve([ff.subs(x, -1), f.subs(x, -1)-2], [p, q]).get(p)
q_value = solve([ff.subs(x, -1), f.subs(x, -1)-2], [p, q]).get(q)
print("p =", p_value)
print("q =", q_value)

# pとqを代入
f = f.subs(p, p_value).subs(q, q_value)
ff = ff.subs(p, p_value).subs(q, q_value)

for num in solve(ff, x) :
    if num != -1 :
        min_value_x = num
        min_value = f.subs(x, num)

print("x =", min_value_x)
print("極小値 =", min_value)

gg = diff(g, x)
l = (gg.subs(x, a)*(x-a)+(-k*a**2)).expand()
print("l:y=" ,l)
l_x_intercept = solve(l, x)[0]
print("lとx軸の交点 =", l_x_intercept)
d_x_a = 1/2*k*a**2*l_x_intercept
print("Dとx軸および直線x=aで囲まれた図形の面積 =", integrate(k*x**2, (x, 0, a)))
tmp_num = int(np.reciprocal(d_x_a.subs(a, 1).subs(k, 1)) * np.reciprocal(integrate(-g, (x, 0, a))).subs(a, 1).subs(k, 1))
tmp_S = integrate(-g, (x, 0, a))*tmp_num - d_x_a*tmp_num
S = integrate(-g, (x, 0, a)) - d_x_a
print("S =%s/%s" % (tmp_S, tmp_num))
k_a_value = solve(f.subs(x, a)-(-k*a**2), k)[0]
print("k =", k_a_value)
l_c = (ff.subs(x, b)*(x-b)+f.subs(x, b)).expand()
print(l_c.factor(x))
f_g = (f - l_c).factor()
print(f_g)
for num in solve(f_g, x) :
    if num != b :
        b_a_value = solve(num-a, b)[0]

for num in solve(l.coeff(x, 1).subs(k, k_a_value) - l_c.coeff(x, 1).subs(b, b_a_value), a) :
    if num > 0 :
        a_value = num

print("a^2 =", a_value**2)
print("S =", S.subs(k, k_a_value).subs(a, a_value))

出力

p = 0
q = -3
x = 1
極小値 = -2
l:y= a**2*k - 2*a*k*x
lとx軸の交点 = a/2
Dとx軸および直線x=aで囲まれた図形の面積 = a**3*k/3
S =1.0*a**3*k/12
k = -a + 3/a
-2*b**3 + x*(3*b**2 - 3)
(-b + x)**2*(2*b + x)
a^2 = 12/5
S = 0.120000000000000

解答
ア:0、イ:0、ウ:-、エ:3、オ:1
カ:-、キ:2、ク:-、ケ:2、コ:2
サ:a、シ:2、ス:3、セ:3、ソ:1
タ:2、チ:3、ツ:a、テ:a、ト:3
ナ:1、ニ:2、ヌ:b、ネ:2、ノ:1
ハ:2、ヒ:5、フ:3、ヘ:2、ホ:5

第3問

総和を求める際に出力を一回確認しています。
全項の次数を確認すればプログラム内でできそうな気がします。
Tnの漸化式の係数が整数なので、classで比較しています。
数式の問題は全体的に出力を確認してからでないと、自分の腕では厳しいです。

プログラム

# -*- coding: utf-8 -*-

from sympy import *
import math
import numpy as np

n = symbols('n')
T_n = symbols('T_n')
an = symbols('an')
bn = symbols('bn')
x = symbols('x')

Sn = (3*(4**n-1))/(4-1)
S2 = Sn.subs(n, 2)
print("S2 =", S2)
T1 = -1
T2 = Sn.subs(n, 1) + T1
print("T2 =", T2)
print("Sn =", Sn)
# Snを確認後無理やり計算
Tn = T1 + (4*(4**(n-1)-1))/(4-1)-(n-1)
Tn = Tn.expand()
print("Tn =", Tn)

a1 = -3
b1 = (a1+2*Tn/n).subs(n, 1)
print("b1 =", b1)
num = 1
# 整数比較用
check_integer = (n+1).subs(n, 1)
while True :
    tmp_formula = (Tn.subs(n, n+1)-num*Tn).expand()
    if tmp_formula.coeff(n, 1) > 0 and tmp_formula.coeff(n, 1).__class__ == check_integer.__class__ and tmp_formula.coeff(n, 0) > 0 and tmp_formula.coeff(n, 0).__class__ == check_integer.__class__ :
        break
    num += 1

Tn_1 = num*T_n+tmp_formula
print("Tn+1 =", Tn_1)
"""
bn+1 = (a(n+1)+2T(n+1))/(n+1)
a(n+1) = 4(n+1)an+8Tn
T(n+1)を代入して分母を計算するためにn(n+1)を掛けた値から考える
"""
bn_1 = (4*(n+1)*an+8*T_n)+2*n*Tn_1
bn_1 = (bn_1.expand().factor()/(n+1)/n).expand()
coeff_bn_0 = (bn_1.coeff(T_n, 0).coeff(an, 0))
bn_1 = bn_1 - coeff_bn_0
tmp_num = bn_1.factor()/((an+2*T_n)/n)
bn_1 = tmp_num*bn+coeff_bn_0
print("bn+1 =", bn_1)

diff_num = solve(bn_1.subs(bn, x)-x, x)[0]
b_n = (b1-diff_num)*tmp_num**(n-1)+diff_num
print("bn =", b_n)
a_n = n*b_n-2*Tn
print("an =", a_n.expand())

出力

S2 = 15
T2 = 2
Sn = 4**n - 1
Tn = 4**n/3 - n - 4/3
b1 = -5
Tn+1 = 4*T_n + 3*n + 3
bn+1 = 4*bn + 6
bn = -3*4**(n - 1) - 2
an = -3*4**n*n/4 - 2*4**n/3 + 8/3

解答
ア:1、イ:5、ウ:2、エ:4、オ:1
カ:1、キ:4、ク:1、ケ:3、コ:4
サ:3、シ:-、ス:5、セ:4、ソ:3
タ:3、チ:4、ツ:6、テ:-、ト:3
ナ:0、ニ:2、ヌ:-、ネ:9、ノ:8
ハ:8、ヒ:3

第4問

今更ですが、三角比はmathを使うより、自分で関数用意したほうが
値が決まっていいかもです。
mathを使うと誤差が出るので。
内積が0の場合垂直に交わるのは明示とします。
BHが三角錐BOACの高さであることから、BHとaの内積が0になるのも明示とします。

プログラム

# -*- coding: utf-8 -*-

from sympy import *
import math
import numpy as np

a = symbols('a')
b = symbols('b')
c = symbols('c')

s = symbols('s')
t = symbols('t')

a_size = 1
b_size = sqrt(3)
c_size= sqrt(5)
a_b = 1
b_c = 3
c_a = 0

def function_calc(formula) :
    return formula.expand().subs(a**2, a_size**2).subs(b**2, b_size**2).subs(c**2, c_size**2).subs(a*b, a_b).subs(b*c, b_c).subs(a*c, c_a)


area_OAC = 1/2*a_size*c_size
print("OACの面積 =", area_OAC)
ba_bc = function_calc((a-b)*(c-b))
print("BA*BC =", ba_bc)
ba = sqrt(function_calc((a-b)**2))
print("BA =", ba)
bc = sqrt(function_calc((c-b)**2))
print("BC =", bc)
cosABC = ba_bc/(ba*bc)
sin_list = {30 : 1/2, 45 : 1/sqrt(2), 60 : sqrt(3)/2, 90 : 1, 120 : sqrt(3)/2, 135 : 1/sqrt(2), 150 : 1/2}
cos_list = {30 : sqrt(3)/2, 45 : 1/sqrt(2), 60 : 1/2, 90 : 0, 120 : -1/2, 135 : -1/sqrt(2), 150 : -sqrt(3)/2}
for key in cos_list.keys() :
    if cosABC == cos_list[key] :
        angle_ABC = key

print("角ABC =", angle_ABC)
angle_BAD = 180 - angle_ABC
angle_ADC = angle_BAD
print("角BAD = 角ADC =", angle_BAD)
ad = ba*cos_list[angle_BAD]+bc+ba*cos_list[angle_BAD]
tmp_num = int(ad/bc)
print("AD = %sBC" % tmp_num)
d = (a+tmp_num*(c-b)).expand()
print("OD =", d)
area_ABCD = 1/2*(bc+ad)*ba*sin_list[angle_BAD]
print("ABCDの面積 =", area_ABCD)

bh = s*a+t*c-b
bh_a = function_calc(bh*a)
bh_c = function_calc(bh*c)
s_value = solve([bh_a, bh_c], [s,t])[s]
t_value = solve([bh_a, bh_c], [s,t])[t]
print("s =", s_value)
print("t =", t_value)
bh = bh.subs(s, s_value).subs(t, t_value)
bh_size = sqrt(function_calc(bh**2))
print("BH =", bh_size)
V = 1/3*area_OAC*bh_size
print("V = 1/6 *", int(2*area_OAC*bh_size))
num = int(area_ABCD/(1/2*bc*ba*sin_list[angle_BAD]))
print("OABCD = %dV" % num)
height = num*V*3/area_ABCD
print(height)

出力

OACの面積 = 0.5*sqrt(5)
BA*BC = -1
BA = sqrt(2)
BC = sqrt(2)
角ABC = 120
角BAD = 角ADC = 60
AD = 2BC
OD = a - 2*b + 2*c
ABCDの面積 = 1.5*sqrt(3)
s = 1
t = 3/5
BH = sqrt(5)/5
V = 1/6 * 1
OABCD = 3V
0.333333333333333*sqrt(3)

解答
ア:9、イ:0、ウ:5、エ:2、オ:-
カ:1、キ:2、ク:2、ケ:1、コ:2
サ:0、シ:6、ス:0、セ:2、ソ:2
タ:2、チ:3、ツ:2、テ:2、ト:0
ナ:1、ニ:3、ヌ:5、ネ:5、ノ:5
ハ:1、ヒ:6、フ:3、ヘ:3、ホ:3

第5問

解説が見当たらなかったので考え方に誤りがあるかもしれません。
また、正規分布表から値を取得する部分は面倒なので、ハードコーディングしています。

プログラム

# -*- coding: utf-8 -*-

from sympy import *
import math
import numpy as np
from statistics import mean, pvariance

e_x = -7
sigma_x = 5

e_x2 = e_x**2+sigma_x**2
print("E(X^2) =", e_x2)
e_w = e_x * 1000
print("E(W) =-7*10^3")
v_w = 1000**2*sigma_x**2
print("V(X) =%d^2*10^6" % sigma_x)
print(7/5)

# 正規分布表から取得
p_z = round(0.5 - 0.4192, 2)
print("クケ:", p_z)
e_m = 50 * p_z
print("E(M) =", e_m)
sigma_m = round(50*p_z*(1-p_z), 1)
print("Mの標準偏差の二乗", sigma_m)

sigma_y = 6/sqrt(100)
print("セソ:", sigma_y)
print("タチ:", round(0.4495*2, 2))
print("%f <= m <= %f" % (round(-10.2-1.64*sigma_y, 1), round(-10.2+1.64*sigma_y, 1)))

出力

E(X^2) = 74
E(W) =-7*10^3
V(X) =5^2*10^6
1.4
クケ: 0.08
E(M) = 4.0
Mの標準偏差の二乗 3.7
セソ: 3/5
タチ: 0.9
-11.200000 <= m <= -9.200000

解答
ア:7、イ:4、ウ:3、エ:2、オ:6
カ:1、キ:4、ク:0、ケ:8、コ:4
サ:0、シ:3、ス:7、セ:0、ソ:6
タ:9、チ:0、ツ:2

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?