裏 RjpWiki

Julia ときどき R, Python によるコンピュータプログラム,コンピュータ・サイエンス,統計学

算額(その109)

2023年01月25日 | Julia

算額(その109)

東京都府中市宮町 大國魂神社 明治18年
大國魂神社の算額(下段第5問)についての考察
http://www2.ttcn.ne.jp/~nagai/waseda/wasan/ok1.pdf

外円内に三角形,甲円,乙円,丙円が入っている。甲円,乙円,丙円の径をそれぞれ 49 寸,28 寸,17 寸のとき,外円の径を求めよ。

図のように記号を定め,方程式を立てる。
三角形の頂点 A, B, C の座標をそれぞれ (-x0, 98 - R), (x0, 98 - R), (x4, y4) とする。
甲円,乙円,丙円の中心座標と半径をそれぞれ (0, 49 -R; R), (x2, 41, 28), (x3, y3, 17) とする。


方程式は solve() では解けないので,nlsolve() で数値解を求める。

using SymPy

function distance(x1, y1, x2, y2, x0, y0)
p1, p2 = sympy.Point(x1, y1), sympy.Point(x2, y2)
l = sympy.Line(p1, p2)
l.distance(sympy.Point(x0, y0))^2  # 二乗距離を返す!!
end;

@syms R, x2, x3, y3, x0, x4, y4;

eq1 = x3^2 + y3^2 - (R - 17)^2 |> expand;
eq2 = R^2 - (98 - R)^2 - x0^2 |> expand;
eq3 = distance(x0, 98 - R, -x0, 98 - R, x2, 41) - 28^2 |> expand;
eq4 = x4^2 + y4^2 - R^2 |> expand;
eq5 = distance(-x0, 98 - R, x4, y4, x2, 41) - 28^2;
eq6 = distance( x0, 98 - R, x4, y4, x2, 41) - 28^2;
eq7 = distance(-x0, 98 - R, x4, y4, x3, y3) - 17^2;

eq1 |> println
eq2 |> println
eq3 |> println
eq4 |> println
eq5 |> println
eq6 |> println
eq7 |> println

   -R^2 + 34*R + x3^2 + y3^2 - 289
   196*R - x0^2 - 9604
   R^2 - 114*R + 2465
   -R^2 + x4^2 + y4^2
   (41 - (41*R^2 + 82*R*y4 - 8036*R + 41*x0^2 + 82*x0*x4 + 41*x4^2 + 41*y4^2 - 8036*y4 + (x0 + x4)*(R*x2 - R*x4 + x0*y4 - 41*x0 + x2*y4 - 98*x2 + 57*x4) + 393764)/(R^2 + 2*R*y4 - 196*R + x0^2 + 2*x0*x4 + x4^2 + y4^2 - 196*y4 + 9604))^2 + (x2 - (x2*(R^2 + 2*R*y4 - 196*R + x0^2 + 2*x0*x4 + x4^2 + y4^2 - 196*y4 + 9604) - (R + y4 - 98)*(R*x2 - R*x4 + x0*y4 - 41*x0 + x2*y4 - 98*x2 + 57*x4))/(R^2 + 2*R*y4 - 196*R + x0^2 + 2*x0*x4 + x4^2 + y4^2 - 196*y4 + 9604))^2 - 784
   (x2 - (R^2*x4 + R*x0*y4 - 41*R*x0 + R*x4*y4 - 155*R*x4 + x0^2*x2 - 2*x0*x2*x4 + x0*y4^2 - 139*x0*y4 + 4018*x0 + x2*x4^2 - 57*x4*y4 + 5586*x4)/(R^2 + 2*R*y4 - 196*R + x0^2 - 2*x0*x4 + x4^2 + y4^2 - 196*y4 + 9604))^2 + (-((98 - R)*(R^2 + 2*R*y4 - 196*R + x0^2 - 2*x0*x4 + x4^2 + y4^2 - 196*y4 + 9604) + (R + y4 - 98)*(R^2 + R*y4 - 155*R + x0^2 - x0*x2 - x0*x4 + x2*x4 - 57*y4 + 5586))/(R^2 + 2*R*y4 - 196*R + x0^2 - 2*x0*x4 + x4^2 + y4^2 - 196*y4 + 9604) + 41)^2 - 784
   (x3 - (x3*(R^2 + 2*R*y4 - 196*R + x0^2 + 2*x0*x4 + x4^2 + y4^2 - 196*y4 + 9604) - (R + y4 - 98)*(R*x3 - R*x4 - x0*y3 + x0*y4 + x3*y4 - 98*x3 - x4*y3 + 98*x4))/(R^2 + 2*R*y4 - 196*R + x0^2 + 2*x0*x4 + x4^2 + y4^2 - 196*y4 + 9604))^2 + (y3 - (y3*(R^2 + 2*R*y4 - 196*R + x0^2 + 2*x0*x4 + x4^2 + y4^2 - 196*y4 + 9604) + (x0 + x4)*(R*x3 - R*x4 - x0*y3 + x0*y4 + x3*y4 - 98*x3 - x4*y3 + 98*x4))/(R^2 + 2*R*y4 - 196*R + x0^2 + 2*x0*x4 + x4^2 + y4^2 - 196*y4 + 9604))^2 - 289

# solve([eq1, eq2, eq3, eq4, eq5, eq6, eq7, eq8, eq9, eq10])

using NLsolve

function nls(func, params...; ini = [0.0])
if typeof(ini) <: Number
r = nlsolve((vout, vin) -> vout[1] = func(vin[1], params..., [ini]), ftol=1e-14)
v = r.zero[1]
else
r = nlsolve((vout, vin)->vout .= func(vin, params...), ini, ftol=1e-14)
v = r.zero
end
return v, r.f_converged
end;

function H(u)
   (R, x2, x3, y3, x0, x4, y4) = u
   return [
       -R^2 + 34*R + x3^2 + y3^2 - 289,
       196*R - x0^2 - 9604,
       R^2 - 114*R + 2465,
       -R^2 + x4^2 + y4^2,
       (41 - (41*R^2 + 82*R*y4 - 8036*R + 41*x0^2 + 82*x0*x4 + 41*x4^2 + 41*y4^2 - 8036*y4 + (x0 + x4)*(R*x2 - R*x4 + x0*y4 - 41*x0 + x2*y4 - 98*x2 + 57*x4) + 393764)/(R^2 + 2*R*y4 - 196*R + x0^2 + 2*x0*x4 + x4^2 + y4^2 - 196*y4 + 9604))^2 + (x2 - (x2*(R^2 + 2*R*y4 - 196*R + x0^2 + 2*x0*x4 + x4^2 + y4^2 - 196*y4 + 9604) - (R + y4 - 98)*(R*x2 - R*x4 + x0*y4 - 41*x0 + x2*y4 - 98*x2 + 57*x4))/(R^2 + 2*R*y4 - 196*R + x0^2 + 2*x0*x4 + x4^2 + y4^2 - 196*y4 + 9604))^2 - 784,
       (41 - (41*R^2 + 82*R*y4 - 8036*R + 41*x0^2 - 82*x0*x4 + 41*x4^2 + 41*y4^2 - 8036*y4 - (x0 - x4)*(R*x2 - R*x4 - x0*y4 + 41*x0 + x2*y4 - 98*x2 + 57*x4) + 393764)/(R^2 + 2*R*y4 - 196*R + x0^2 - 2*x0*x4 + x4^2 + y4^2 - 196*y4 + 9604))^2 + (x2 - (x2*(R^2 + 2*R*y4 - 196*R + x0^2 - 2*x0*x4 + x4^2 + y4^2 - 196*y4 + 9604) - (R + y4 - 98)*(R*x2 - R*x4 - x0*y4 + 41*x0 + x2*y4 - 98*x2 + 57*x4))/(R^2 + 2*R*y4 - 196*R + x0^2 - 2*x0*x4 + x4^2 + y4^2 - 196*y4 + 9604))^2 - 784,
       (x3 - (-x0*(R^2 + 2*R*y4 - 196*R + x0^2 + 2*x0*x4 + x4^2 + y4^2 - 196*y4 + 9604) + (x0 + x4)*(R^2 + R*y3 + R*y4 - 196*R + x0^2 + x0*x3 + x0*x4 + x3*x4 + y3*y4 - 98*y3 - 98*y4 + 9604))/(R^2 + 2*R*y4 - 196*R + x0^2 + 2*x0*x4 + x4^2 + y4^2 - 196*y4 + 9604))^2 + (y3 - ((98 - R)*(R^2 + 2*R*y4 - 196*R + x0^2 + 2*x0*x4 + x4^2 + y4^2 - 196*y4 + 9604) + (R + y4 - 98)*(R^2 + R*y3 + R*y4 - 196*R + x0^2 + x0*x3 + x0*x4 + x3*x4 + y3*y4 - 98*y3 - 98*y4 + 9604))/(R^2 + 2*R*y4 - 196*R + x0^2 + 2*x0*x4 + x4^2 + y4^2 - 196*y4 + 9604))^2 - 289,
   ]
end;

iniv = [big"80.0", 30, -30, 55, 80, 30, 75]
res = nls(H, ini=iniv)
println(res)

   (BigFloat[85.0, 28.00000000000000000000000000000000000000000000000000000000000000000000000000055, -31.99999993545069734104462106203685900316672771867706686845499772119474317529376, 60.00000003442629475144286876691367519831107855003889767015735701621077260821287, 84.0, 35.99999999999999999999999999999999999999999999999999999999999999999999999999945, 77.0], true)

外円の半径は 85.0 である。元の単位での径(直径)は 85 寸である。

using Plots
using Printf

function circle(ox, oy, r, color=:red; beginangle=0, endangle=360, fill=false)
  θ = beginangle:0.1:endangle
  x = r.*cosd.(θ)
  y = r.*sind.(θ)
   if fill
       plot!(ox .+ x, oy .+ y, color=color, linewidth=0.5, seriestype=:shape, fillcolor=color)
   else
       plot!(ox .+ x, oy .+ y, color=color, linewidth=0.5)
   end
end;

function point(x, y, string="", color=:green, position=:left, vertical=:top; mark=true)
   mark && scatter!([x], [y], color=color, markerstrokewidth=0)
   annotate!(x, y, text(string, 10, position, color, vertical))
end;

function segment(x1, y1, x2, y2, color=:black; linestyle=:solid, linewidth=0.5)
   plot!([x1, x2], [y1, y2], color=color, linestyle=linestyle, linewidth=linewidth)
end;

function draw(more=false)
    pyplot(size=(500, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
   (R, x2, x3, y3, x0, x4, y4) = res[1]
   println("R = $R")
   plot()
   circle(0, 0, R)
   circle(0, 49-R, 49, :blue)
   circle(x2, 41, 28, :magenta)
   circle(x3, y3, 17, :green)
   plot!([x0, x4, -x0, x0], [98 - R, y4, 98 - R, 98 - R], color=:brown, lw=0.5)
    if more
       @printf("x0 = %.3f, x2 = %.3f, x3 = %.3f, y3 = %.3f, x4 = %.3f, y4 = %.3f", x0, x2, x3, y3, x4, y4)
       point(0, 49 - R, " 甲(0,49-R;49)", :blue, :left)
       point(x2, 41, "乙(x2,41;28)", :magenta, :top)
       point(x3, y3, "丙(x3,y3;17)", :green, :top)
       point(0, 0, " O")
       point(-x0, 98 - R, " A(-x0,98-R)", :brown)
       point(x0, 98 - R, "B(x0,98-R) ", :brown, :right)
       point(x4, y4, " C(x4,y4)", :brown, :left, :bottom)
       vline!([0], color=:black, lw=0.5)
       hline!([0], color=:black, lw=0.5)
    end
end;

 

 


コメント    この記事についてブログを書く
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする
« 算額(その108) | トップ | 算額(その110) »
最新の画像もっと見る

コメントを投稿

Julia」カテゴリの最新記事