長文です。 YouTube様内のコメント文の1行で済まそうと思いましたができませんでした。
・ChatGPT先生は、以前だとこんなに提案しなかったように思いました。
オリジナル
Youtube
数学を数楽に 様 (0:00〜5:35) いつもお世話になっております。
2通りで解説
知っていれば一瞬!絶対値の入った2次方程式
x^2-4|x|-12=0
sympyのweb上での実行方法
matplotlibは調査中。matplotlibなしのコードは省略しました。
SymPy Live Shellで。FreeCADのマクロは、以下で実行できません。
https://qiita.com/mrrclb48z/items/00dd08b0317069be9342#web%E4%B8%8A%E3%81%AE%E5%AE%9F%E8%A1%8C%E6%96%B9%E6%B3%95sympy-live-shell%E3%81%A7
まずは、自力?WolframAlpha で
・根のプロットがでます。
・プロットがでます。
・「知っていれば一瞬!」の因数分解を期待しましたが残念でした。
?有限体上の因数分解 がでます。
・プロットがでます。連立方程式?でした。Youtubeのコメント様より
(本日の)ChatGPT先生へ
・以下、かなり省略しています。
x2-4|x|-12=0
オリジナル 様の最初の方法と同じ。場合分け法でした。
x2-4|x|-12=0 を5通りで解いて下さい。
良い問題ですね。5通りの方法で解いてみましょう。
【方法①:置換法(基本)】
【方法②:場合分け法】
【方法③:グラフ法】
【方法④:数値的アプローチ】
【方法⑤:対称性利用】
(省略)
ご希望があれば「グラフ描画」や「SymPyによる確認コード」も示せます。どちらをご希望ですか?
SymPyによる確認コードで
もちろんです。...
(省略)
必要であれば、この方程式のグラフ表示コード(plot)付きも示せます。
希望されますか?
希望します。
以下のコードでは、3本のグラフを 同じ図上に 描いて、...
from sympy import symbols, Abs, Eq, solve
import matplotlib.pyplot as plt
import numpy as np
# 変数定義
x = symbols('x', real=True)
# 各関数
y1 = x**2 - 12 # 左辺の一部(x^2 - 12)
y2 = 4*Abs(x) # 右辺(4|x|)
f = y1 - y2 # 方程式左辺 f(x) = x^2 - 4|x| - 12
# 解
solutions = solve(Eq(f, 0), x)
print("解:", solutions)
# 数値化
y1_func = lambda t: t**2 - 12
y2_func = lambda t: 4*abs(t)
f_func = lambda t: t**2 - 4*abs(t) - 12
# 描画範囲
X = np.linspace(-10, 10, 400)
Y1 = [y1_func(t) for t in X]
Y2 = [y2_func(t) for t in X]
F = [f_func(t) for t in X]
# グラフ描画
plt.figure(figsize=(7,5))
plt.plot(X, Y1, label=r'$y_1 = x^2 - 12$', color='blue')
plt.plot(X, Y2, label=r'$y_2 = 4|x|$', color='orange')
plt.plot(X, F, label=r'$f(x) = x^2 - 4|x| - 12$', color='green', linestyle='--')
plt.axhline(0, color='gray', lw=1)
# 解の表示(赤点)
for s in solutions:
plt.plot(float(s), 0, 'ro')
plt.text(float(s)+0.2, 1, f"x={s}", color='red')
plt.title("方程式 x² - 4|x| - 12 = 0 のグラフ的確認")
plt.xlabel("x")
plt.ylabel("y")
plt.legend()
plt.grid(True)
plt.show()
ご希望があれば、このグラフを FreeCAD の Draft モジュールで描くスクリプト形式 にも変換できます。
FreeCADバージョンも出しますか?
お願いします。
ご希望があれば、Draftオブジェクトに数式ラベル(例:"y=x²−12"など)をテキストで表示する版もお作りできます。
テキスト表示付きにしますか?
お願いします。
すばらしい 👍
では、FreeCAD Draftで「関数名をテキストで図中に表示する」完全版を示します。
🧮 FreeCAD Draftスクリプト(テキスト付き 完全版)
(省略)
...x=6 の距離や座標関係を明示する完全版をお示しします。
import FreeCAD, Draft
import math
doc = FreeCAD.ActiveDocument
if doc is None:
doc = FreeCAD.newDocument("Graph_Abs_Full")
# ========= パラメータ =========
xmin, xmax = -10, 10
step = 0.2
# 曲線生成関数
def curve_points(func):
return [FreeCAD.Vector(x, func(x), 0) for x in [xmin + i*step for i in range(int((xmax-xmin)/step)+1)]]
# 関数定義
y1 = lambda x: x**2 - 12
y2 = lambda x: 4*abs(x)
f = lambda x: x**2 - 4*abs(x) - 12
# ========= 曲線描画 =========
p1 = curve_points(y1)
p2 = curve_points(y2)
p3 = curve_points(f)
# y1 (x^2 - 12)
wire1 = Draft.makeWire(p1, closed=False, face=False)
wire1.Label = "y1 = x^2 - 12"
wire1.ViewObject.LineColor = (0.0, 0.0, 1.0) # 青
# y2 (4|x|)
wire2 = Draft.makeWire(p2, closed=False, face=False)
wire2.Label = "y2 = 4|x|"
wire2.ViewObject.LineColor = (1.0, 0.5, 0.0) # オレンジ
# f(x) (x^2 - 4|x| - 12)
wire3 = Draft.makeWire(p3, closed=False, face=False)
wire3.Label = "f(x) = x^2 - 4|x| - 12"
wire3.ViewObject.LineColor = (0.0, 0.8, 0.0) # 緑
wire3.ViewObject.LinePattern = "Dashdot"
# x軸
xaxis = Draft.makeLine(FreeCAD.Vector(xmin,0,0), FreeCAD.Vector(xmax,0,0))
xaxis.Label = "x-axis"
xaxis.ViewObject.LineColor = (0.5, 0.5, 0.5)
# ========= 解の赤点 =========
for x0 in [-6, 6]:
pt = Draft.makePoint(x0, 0, 0)
pt.Label = f"x={x0}"
pt.ViewObject.PointColor = (1.0, 0.0, 0.0)
pt.ViewObject.PointSize = 6
# ========= 寸法線の追加 =========
# 解 x=-6 と x=6 の間の水平寸法
p1 = FreeCAD.Vector(-6, 0, 0)
p2 = FreeCAD.Vector(6, 0, 0)
dim = Draft.makeDimension(p1, p2, FreeCAD.Vector(0, -5, 0)) # 下にオフセット
dim.Label = "Distance between roots"
dim.ViewObject.FontSize = 4
dim.ViewObject.ArrowSize = 3
# x=-6 の縦寸法 (y=0 から y=24 まで)
p3 = FreeCAD.Vector(-6, 0, 0)
p4 = FreeCAD.Vector(-6, 24, 0)
dimL = Draft.makeDimension(p3, p4, FreeCAD.Vector(-7, 12, 0))
dimL.Label = "Left vertex height"
# x=6 の縦寸法
p5 = FreeCAD.Vector(6, 0, 0)
p6 = FreeCAD.Vector(6, 24, 0)
dimR = Draft.makeDimension(p5, p6, FreeCAD.Vector(7, 12, 0))
dimR.Label = "Right vertex height"
# ========= テキストラベル =========
Draft.makeText(["y1 = x² - 12"], point=FreeCAD.Vector(2, 35, 0))
Draft.makeText(["y2 = 4|x|"], point=FreeCAD.Vector(5, 25, 0))
Draft.makeText(["f(x) = x² - 4|x| - 12"], point=FreeCAD.Vector(-9, -10, 0))
Draft.makeText(["x軸 (y=0)"], point=FreeCAD.Vector(8, 2, 0))
Draft.makeText(["解: x = ±6"], point=FreeCAD.Vector(-2, 5, 0))
Draft.makeText(["寸法表示例: 距離12, 高さ24"], point=FreeCAD.Vector(-9, -16, 0))
doc.recompute()
次はどちらの方向(SymPyかFreeCAD)を進めましょうか?
???

