1
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?

2次関数の最大値(その1/2)「 数学チャレンジ問題 長方形DBFEの面積の最大値は?」をとsympyとFreeCADとChatGPTとWolframAlphaでやってみたい。

Last updated at Posted at 2024-06-29

・問題文は2次元ですが、3次元FreeCADのマクロで、XY平面上に作図しました。
・(その2/2)に続きます。
https://qiita.com/mrrclb48z/items/5dc3518f948cc3d53f82

オリジナル

・YUUU0123 様 (0:00〜3:36)

sympyで(オリジナル 様の方法を参考に)

0.png

Ver.0.1 平方完成で

「平方完成(へいほうかんせい、英: completing the square) 」を調べた。ChatGPT以後を追加
https://qiita.com/mrrclb48z/items/38e849f41f46bf0c8f9c

# Ver.0.1 平方完成で
from sympy import *
var('x,h,k',real=True,nonnegative=True)
def complete_square(expr):
    a, b, c = Poly(expr, x).all_coeffs()
    
    h = -b / (2 * a)
    k = expr.subs(x, h)
    
    return h, k
y=(2-x)*2*x
h,k=complete_square(y.expand())
print("#",h,k)
plot(y,(x,0,2))
# 12

Ver.0.2 sympyのmaximumで

sympy.calculus.util.maximum(f, symbol, domain=Reals)
https://docs.sympy.org/latest/modules/calculus/index.html#sympy.calculus.util.maximum

# Ver.0.2 sympyのmaximumで
from sympy import *
var('x',real=True,nonnegative=True)
y=(2-x)*2*x
y_max=maximum(y,x,Interval(0,2))
x_max=solve(Eq(y,y_max),x)[0]
print("#",x_max,y_max)
# plot(y,(x,0,2))
# # 1 2

Ver.0.3 微分で

>強いて、微分で
To take derivatives, use the diff function.
https://docs.sympy.org/latest/tutorials/intro-tutorial/calculus.html#derivatives

# Ver.0.3 微分で
from sympy import *
var('x',real=True,nonnegative=True)
y=(2-x)*2*x
x_max=solve(Eq(diff(y),0),x)[0]
y_max=y.subs({x:x_max})
print("#",x_max,y_max)
# # 1 2 

Ver.0.4 ベクトルで

・点Eの座標より。

# Ver.0.4 ベクトルで
from sympy import *
var('t',real=True,nonnegative=True)
A=Point(0,2)
B=Point(0,0)
C=Point(4,0)
E=C+t*(A-C)
area=E.x*E.y
area_max=maximum(area,t,Interval(0,1))
area_t  =solve(Eq(area,area_max),t)[0]
E=E.subs({t:area_t})
print("#",E,area_max)
plot(area,(t,0,1))
# Point2D(2, 1) 2

0.png

FreeCADのマクロで作図

・計算部分は、Ver.0.4の コピー貼り付けです。

import FreeCAD
import Part
import DraftTools
import Draft
import Mesh
############################################################################
# Ver.0.4 ベクトルで
from sympy import *
var('t',real=True,nonnegative=True)
A=Point(0,2)
B=Point(0,0)
C=Point(4,0)
E=C+t*(A-C)
area=E.x*E.y
area_max=maximum(area,t,Interval(0,1))
area_t  =solve(Eq(area,area_max),t)[0]
E=E.subs({t:area_t})
print("#",E,area_max)
# plot(area,(t,0,1))
# Point2D(2, 1) 2
############################################################################
# 作図用
D=Point(0,E.y)
F=Point(E.x,0)
############################################################################
# 3D作図 z=0 XY平面に作図しました。
############################################################################
def myXYZ2Txt_2D(A):
    return '(' + str(A.x) + ',' + str(A.y) +  ')'
    # return ""
def myTxtXYZ_2D(A,myWedgei):
    P5x=float(A.x)
    P5y=float(A.y)
    P5z=0.0
    p5 = FreeCAD.Vector(P5x, P5y, P5z)
    myText = Draft.makeText(myWedgei, p5)
    myText.Label = myWedgei
    # FreeCADGui.ActiveDocument.ActiveObject.FontSize = '0.1 mm'
    FreeCADGui.ActiveDocument.ActiveObject.FontSize = '0.3 mm'
    return
def myTxtXYZ_S_2D(*xy_tx):
    for i in range(1,int(len(xy_tx)/2)+1):
        myTxtXYZ_2D(xy_tx[2*i-2],xy_tx[2*i-1]+myXYZ2Txt_2D(xy_tx[2*i-2]) )
    return
def myLine_2D(A,B):
    Ax,Ay,Az=float(A.x),float(A.y),0.0
    Bx,By,Bz=float(B.x),float(B.y),0.0
    pl = FreeCAD.Placement()
    pl.Rotation.Q = (0.4247081540122249, 0.17592004639554645, 0.33985110062924484, 0.8204732460821097)
    pl.Base = FreeCAD.Vector(-3.9166066876399563, -2.1670824762243774, 1.7495260956243028)
    points = [FreeCAD.Vector(Ax,Ay,Az), FreeCAD.Vector(Bx,By,Bz)]
    line = Draft.make_wire(points, placement=pl, closed=False, face=True, support=None)
    Draft.autogroup(line)
    return
def myLine_S_2D(*args):
    for i in range(1,len(args)):
        myLine_2D(args[i-1],args[i])
    return
def myLine_C_2D(*args):
    for i in range(1,len(args)):
        myLine_2D(args[i-1],args[i])
    myLine_2D(args[i],args[0])
    return
#######################################################################################
myLine_C_2D  (A,D,B,F,C,E)
myLine_S_2D  (D,E,F)
myTxtXYZ_S_2D(A,"A",D,"D",B,"B", F,"F",C,"C",E,"E", )
#######################################################################################
doc = App.activeDocument()
App.ActiveDocument.addObject("App::Origin", "Origin")
# App.ActiveDocument.getObject('Origin').Visibility = True
App.ActiveDocument.recompute()
Gui.activeDocument().activeView().viewAxonometric()
Gui.SendMsgToActiveView("ViewFit")

isometric方向?です。

0.png

拡大図

1.png

WolframAlphaで

最大値
x = 1 のとき, max{(2 - x) 2 x} = 2

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

(テンプレート)

・以下ができたら、助かります。指定と全部です

いつもと違うおすすめです。

・(省略) 
 
 

・(その2/2)に続きます。
https://qiita.com/mrrclb48z/items/5dc3518f948cc3d53f82

1
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
1
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?