(正三角形)「1995年お茶の水女子大付の数学入試問題」をsympyとwolframalphaとVBAソルバーでやってみたい。(つづき)

前頁

https://qiita.com/mrrclb48z/items/c2d2ff6d430693ffaf18

## 以下ヘロンの公式と余弦定理でチャレンジしました。

# wolframalphaで


##### ヘロンの公式で
「入力が最大文字数を超えています．」
最大文字数を調査中です。

sqrt((-a/2 + b/2 + x/2)*(a/2 - b/2 + x/2)*(a/2 + b/2 - x/2)*(a/2 + b/2 + x/2)) + sqrt((-a/2 + c/2 + x/2)*(a/2 - c/2 + x/2)*(a/2 + c/2 - x/2)*(a/2 + c/2 + x/2)) + sqrt((-b/2 + c/2 + x/2)*(b/2 - c/2 + x/2)*(b/2 + c/2 - x/2)*(b/2 + c/2 + x/2)) - sqrt(3)*sqrt(x**4)/4=0



##### 余弦定理で

https://www.wolframalpha.com/input/?i=acos%28%28x*x-3*3-4*4%29%2F%28-2*3*4%29%29%2Bacos%28%28x*x-4*4-5*5%29%2F%28-2*4*5%29%29%2Bacos%28%28x*x-5*5-3*3%29%2F%28-2*5*3%29%29%3D2*pi&lang=ja

解
x = sqrt(25 + 12 sqrt(3))
数値解
x ≈ ± 6.76643256752231...

https://ja.wolframalpha.com/input/?i=1%E8%BE%BA%E3%81%AE%E9%95%B7%E3%81%95%E3%81%8Csqrt%2825+%2B+12+sqrt%283%29%29%E3%81%A7%E3%81%82%E3%82%8B%E6%AD%A3%E4%B8%89%E8%A7%92%E5%BD%A2%E3%81%AE%E9%9D%A2%E7%A9%8D

解
9 + (25 sqrt(3))/4≈19.8253


#PycharmとSymPyで
ヘロンの公式を使って、できましたが。
余弦定理を使って、できませんでした。

##### ヘロンの公式で
```python
from sympy import *
var('a b c x')
def herons(a, b, c):
    s = (a+b+c) / 2
    return sqrt(s*(s-a)*(s-b)*(s-c))
Eq1=herons(a,b,x)+herons(b,c,x)+herons(c,a,x)-herons(x,x,x)
mySubs1={a:3,b:4,c:5}
print("#x =",solve(Eq1,x)[4].subs(mySubs1))
print("#x =",float(solve(Eq1,x)[4].subs(mySubs1)))
#x = sqrt(12*sqrt(3) + 25)
#x = 6.766432567522307
```


##### 余弦定理で
```python
from sympy import *
var('a b c x')
Eq2 =acos((x**2-a**2-b**2)/(-2*a*b))+acos((x**2-b**2-c**2)/(-2*b*c))+acos((x**2-c**2-a**2)/(-2*c*a))
mySubs1={a:3.0,b:4.0,c:5.0}
mySubs2={x:sqrt(12*sqrt(3.0) + 25.0)}
print("#y =",(Eq2.subs(mySubs1)-2*pi.evalf()).subs(mySubs2))
ans=solve(Eq2.subs(mySubs1)-2*pi.evalf(),x)
print("#x =",ans)
#-------------------------------------------------
#y = 6.66133814775094e-16
# Traceback (most recent call last):
#   File "C:/Users/xxx/PycharmProjects/pythonProject/main.py", line 18, in <module>
#     ans=solve(Eq2.subs(mySubs1)-2*pi.evalf(),x)
#   File "C:\Users\xxx\Anaconda3\lib\site-packages\sympy\solvers\solvers.py", line 1094, in solve
#     solution = _solve(f[0], *symbols, **flags)
#   File "C:\Users\xxx\Anaconda3\lib\site-packages\sympy\solvers\solvers.py", line 1694, in _solve
#     raise NotImplementedError('\n'.join([msg, not_impl_msg % f]))
# NotImplementedError: multiple generators [acos(17/15 - x**2/30), acos(25/24 - x**2/24), acos(41/40 - x**2/40)]
# # No algorithms are implemented to solve equation acos(41/40 - x**2/40) + acos(25/24 - x**2/24) + acos(17/15 - x**2/30) - 628318530717959/100000000000000
```
# ソルバー関連の VBA 関数で


##### ヘロンの公式でソルバー

```VB
Sub aaa_main()
    Range("A1") = "3"
    Range("A2") = "4"
    Range("A3") = "5"
    Range("A4") = "6.5"
    Range("A5").Formula = "=(A1+A2+A$4)/2": Range("B5").Formula = "=SQRT(A5*(A5-A1)*(A5-A2)*(A5-A$4))"
    Range("A6").Formula = "=(A2+A3+A$4)/2": Range("B6").Formula = "=SQRT(A6*(A6-A2)*(A6-A3)*(A6-A$4))"
    Range("A7").Formula = "=(A3+A1+A$4)/2": Range("B7").Formula = "=SQRT(A7*(A7-A3)*(A7-A1)*(A7-A$4))"
    Range("A8").Formula = "=(A4+A4+A$4)/2": Range("B8").Formula = "=SQRT(A8*(A8-A4)*(A8-A4)*(A8-A$4))"
    Range("B10").Formula = "=SUM(B5:B7)-B8"
MsgBox "ヘロンの公式で計算します。"
    Dim ws As Worksheet: Set ws = ActiveSheet
    SolverReset
    SolverOk setCell:=ws.Range("B10"), _
                   MaxMinVal:=3, _
                   ByChange:=ws.Range("A4"), _
                   EngineDesc:="GRG Nonlinear"
    SolverSolve UserFinish:=True
End Sub
'6.76643256771842
```

##### 余弦定理でソルバー
```VB
Sub bbb_main()
    Range("A1") = "3"
    Range("A2") = "4"
    Range("A3") = "5"
    Range("A4") = "6.5"
    Range("B5").Formula = "=180/PI()*ACOS((A$4^2-A1^2-A2^2)/(-2*A1*A2))"
    Range("B6").Formula = "=180/PI()*ACOS((A$4^2-A2^2-A3^2)/(-2*A2*A3))"
    Range("B7").Formula = "=180/PI()*ACOS((A$4^2-A3^2-A1^2)/(-2*A3*A1))"
    Range("B8").Formula = "=360"
    Range("B10").Formula = "=SUM(B5:B7)-B8"
MsgBox "余弦定理で計算します。"
    Dim ws As Worksheet: Set ws = ActiveSheet
    SolverReset
    SolverOk setCell:=ws.Range("B10"), _
                   MaxMinVal:=3, _
                   ByChange:=ws.Range("A4"), _
                   EngineDesc:="GRG Nonlinear"
    SolverSolve UserFinish:=True
End Sub
'6.76643256936964
```


# 参考

https://docs.microsoft.com/ja-jp/office/vba/excel/concepts/functions/using-the-solver-vba-functions