0
0

2次関数の応用問題「20年 中央大 経済 2」をsympyと作図をやってみたい。

Last updated at Posted at 2024-02-21

パイソニスタの方へ
・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()

作図

0000Figure_1.png

いつもの? sympyの実行環境と 参考のおすすめです。

(テンプレート)

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