0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

sympyで「Heaviside step function(ヘヴィサイドの階段関数)」を使ってみた

Last updated at Posted at 2024-01-30

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

Figure_1.png

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

参考

参考文献

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?