パイソニスタの方へ
・Piecewise関数にしました。(ver0.2)
Represents an expression, condition pair.
https://docs.sympy.org/latest/modules/functions/elementary.html#piecewise
・Heaviside関数の正しい?使い方を教えて下さい。(ver0.1)
オリジナル
数学入試問題 様
大学入試数学問題集成 様> 未登録
sympyで
・Piecewise関数で
・ver0.2
# ver0.2
from sympy import *
x=symbols('x',real=True)
var('SR,BS,BC,RC,PB',real=True)
rep={SR:x,BS:x/tan(60*pi/180),RC:x/tan(60*pi/180),BC:1}
sol=solve(Eq(BC,BS+SR+RC).subs(rep),x)[0]
y1=x**2
rep={PB:1-x}
y2=(x*PB*sin(60*pi/180)).subs(rep).factor()
y =Piecewise( (y1,x<sol),(y2,True))
print("# (1)",y)
print("# (2)",maximum(y,x,Interval(0,1)) )
# (1) Piecewise((x**2, x < -3 + 2*sqrt(3)), (-sqrt(3)*x*(x - 1)/2, True))
# (2) sqrt(3)/8
・ver0.1
・オリジナル 様を参考に
# ver0.1
from sympy import *
x=symbols('x',real=True)
SR=x
BS=RC=x/tan(60*pi/180)
BC=1
eq=Eq(BC,BS+SR+RC)
sol=solve(eq,x)[0]
y1=x**2
PB=1-x
y2=(x*PB*sin(60*pi/180)).factor()
print("# (1)",y1,sol)
print("# (1)",y2)
# y =y1-Heaviside(x-sol,H0=0)*y1+Heaviside(x-sol,H0=0)*y2
# print("# (2)",maximum(y,x,Interval(0,1)))
print("# (2)",max( maximum(y1,x,Interval(0,sol)),maximum(y2,x,Interval(sol,1)) ))
# (1) x**2 -3 + 2*sqrt(3)
# (1) -sqrt(3)*x*(x - 1)/2
# (2) sqrt(3)/8
# 作図(グラフ)----------------------------------------------------------------------
from sympy.plotting import plot
p0=plot(0,sqrt(3)/8,(x,0,1),aspect_ratio=(1.0,1.0),show=False)
p0=plot(0 ,(x,0,1),aspect_ratio=(1.0,1.0),show=False)
p1=plot(y1,(x,0.0,sol),show=False)
p2=plot(y2,(x,sol,1.0),show=False)
p0.extend(p1)
p0.extend(p2)
p0.show()