plot (2024/02/15 追加)
from sympy.plotting import plot
var('x',real=True)
y0=x*2-Heaviside(x-3,H0=0)*x*2+Heaviside(x-3,H0=0)*(6+(x-3)*3)
y1=x*2-Heaviside(x-3,H0=1)*x*2+Heaviside(x-3,H0=1)*(6+(x-3)*3)
print('#',y0)
print('#',y1)
plot(y0,y1,(x,0,6))
# -2*x*Heaviside(x - 3, 0) + 2*x + (3*x - 3)*Heaviside(x - 3, 0)
# -2*x*Heaviside(x - 3, 1) + 2*x + (3*x - 3)*Heaviside(x - 3, 1)
print("#",y0.subs({x:3}))
print("#",y1.subs({x:3}))
# 6
# 6
Doc
Heaviside step function.
wikipedia
sympyで
参考
数学入試問題 様
>2次関数の応用問題
・点Qが点Cから点Oへ移動します。
相互リンクです。
・ver0.2
# ver0.2
from sympy import *
var('Px,Qy,t,x',real=True)
OC =6
Qsp=2
t3 =Rational(OC,Qsp)
Qy_CO=OC -Qsp*t*Heaviside(t- 0,H0=0) #;print("# Qy_CO=",Qy_CO)
Qy_OC=Qsp*(t-t3) *Heaviside(t-t3,H0=0) #;print("# Qy_OC=",Qy_OC)
Qy =Qy_CO+Qy_OC*2 ;print("# Qy =",Qy )
Qy_0 =Qy.subs({t:0}) ;print("# Qy_0 =",Qy_0)
Qy_1 =Qy.subs({t:1}) ;print("# Qy_1 =",Qy_1)
Qy_2 =Qy.subs({t:2}) ;print("# Qy_2 =",Qy_2)
Qy_3 =Qy.subs({t:3}) ;print("# Qy_3 =",Qy_3)
Qy_4 =Qy.subs({t:4}) ;print("# Qy_4 =",Qy_4)
Qy_5 =Qy.subs({t:5}) ;print("# Qy_5 =",Qy_5)
Qy_6 =Qy.subs({t:6}) ;print("# Qy_6 =",Qy_6)
#
# Qy = -2*t*Heaviside(t, 0) + 2*(2*t - 6)*Heaviside(t - 3, 0) + 6
# Qy_0 = 6
# Qy_1 = 4
# Qy_2 = 2
# Qy_3 = 0
# Qy_4 = 2
# Qy_5 = 4
# Qy_6 = 6
・ver0.1
# ver0.1
from sympy import *
var('Px,Qy,t,x',real=True)
Qy_CO=6-2*t*Heaviside(t-0,H0=0) ;print("# Qy_CO=",Qy_CO)
Qy_0 =Qy_CO.subs({t:0}) ;print("# Qy_0 =",Qy_0)
Qy_1 =Qy_CO.subs({t:1}) ;print("# Qy_1 =",Qy_1)
Qy_2 =Qy_CO.subs({t:2}) ;print("# Qy_2 =",Qy_2)
Qy_3 =Qy_CO.subs({t:3}) ;print("# Qy_3 =",Qy_3)
#
Qy_OC=2*(t-3)*Heaviside(t-3,H0=0) ;print("# Qy_OC=",Qy_OC)
Qy_4 =Qy_OC.subs({t:4}) ;print("# Qy_4 =",Qy_4)
Qy_5 =Qy_OC.subs({t:5}) ;print("# Qy_5 =",Qy_5)
Qy_6 =Qy_OC.subs({t:6}) ;print("# Qy_6 =",Qy_6)
# Qy_CO= -2*t*Heaviside(t, 0) + 6
# Qy_0 = 6
# Qy_1 = 4
# Qy_2 = 2
# Qy_3 = 0
# Qy_OC= (2*t - 6)*Heaviside(t - 3, 0)
# Qy_4 = 2
# Qy_5 = 4
# Qy_6 = 6
参考