算額(その131)
新潟県長岡市与板町本与板 与板八幡宮 文化5年(1808)
http://www.wasan.jp/niigata/yoitahatiman3.html
短辺(平)の長さが 595 寸の長方形の中に,甲円,乙円,丙円,丁円,戊円が入っている。甲円の径はいかほどか。
長辺(長)の長さを w, 甲円,乙円,丙円,丁円,戊円の半径を r1, r2, r3, r4, r5,戊円の中心の x 座標を x5 として以下のように記号を定め,方程式を立てて,解く。
using SymPy
@syms r1::positive, r2::positive, r3::positive, r4::positive, r5::positive, x5::positive, w::positive;
eq1 = (w - r1 - r2)^2 + (595 - r1 - r2)^2 - (r1 + r2)^2
eq2 = (w - r1 - r3)^2 + (r1 - r3)^2 - (r1 + r3)^2
eq3 = (r4 - r1)^2 + (595 - r1 - r4)^2 - (r1 + r4)^2
eq4 = (w - r1 - x5)^2 + (595 - r1 - r5)^2 - (r1 + r5)^2
eq5 = (r2 - r3)^2 + (r2 - 595 + r3)^2 - (r2 + r3)^2
eq6 = (r2 - x5)^2 + (r2 - r5)^2 - (r2 + r5)^2
eq7 = (x5 - w + r4)^2 + (r4 - r5)^2 - (r4 + r5)^2;
# res = solve([eq1, eq2, eq3, eq4, eq5, eq6, eq7], (r1, r2, r3, r4, r5, x5, w))
これも solve() では解けないので,nlsolve() で解く。
println(eq1, ",")
println(eq2, ",")
println(eq3, ",")
println(eq4, ",")
println(eq5, ",")
println(eq6, ",")
println(eq7, ",")
-(r1 + r2)^2 + (-r1 - r2 + 595)^2 + (-r1 - r2 + w)^2,
(r1 - r3)^2 - (r1 + r3)^2 + (-r1 - r3 + w)^2,
(-r1 + r4)^2 - (r1 + r4)^2 + (-r1 - r4 + 595)^2,
-(r1 + r5)^2 + (-r1 - r5 + 595)^2 + (-r1 + w - x5)^2,
(r2 - r3)^2 - (r2 + r3)^2 + (r2 + r3 - 595)^2,
(r2 - r5)^2 - (r2 + r5)^2 + (r2 - x5)^2,
(r4 - r5)^2 - (r4 + r5)^2 + (r4 - w + x5)^2,
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)
(r1, r2, r3, r4, r5, x5, w) = u
return [
-(r1 + r2)^2 + (-r1 - r2 + 595)^2 + (-r1 - r2 + w)^2,
(r1 - r3)^2 - (r1 + r3)^2 + (-r1 - r3 + w)^2,
(-r1 + r4)^2 - (r1 + r4)^2 + (-r1 - r4 + 595)^2,
-(r1 + r5)^2 + (-r1 - r5 + 595)^2 + (-r1 + w - x5)^2,
(r2 - r3)^2 - (r2 + r3)^2 + (r2 + r3 - 595)^2,
(r2 - r5)^2 - (r2 + r5)^2 + (r2 - x5)^2,
(r4 - r5)^2 - (r4 + r5)^2 + (r4 - w + x5)^2,
]
end;
iniv = [200.0, 165, 135, 95, 90, 400, 700]
res = nls(H, ini=iniv)
println(res)
([213.00034502265584, 164.1726947717987, 134.08788967981687, 96.00257736188509, 88.286549252726, 404.9567532384933, 685.0868546118494], false)
甲円の半径 = 213.000, 乙円の半径 = 164.173, 丙円の半径 = 134.088, 丁円の半径 = 96.003, 戊円の半径 = 88.287
長 = 685.087, x = 404.957
となるので,算額の問である「甲円の径]は 213*2 = 426寸である。
using Plots
using Printf
function circle(ox, oy, r, color=:red; beginangle=0, endangle=360)
θ = beginangle:0.1:endangle
x = r.*cosd.(θ)
y = r.*sind.(θ)
plot!(ox .+ x, oy .+ y, color=color, linewidth=0.5)
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 draw(more)
pyplot(size=(500, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
(r1, r2, r3, r4, r5, x5, w) = res[1]
@printf("甲円の半径 = %.3f, 乙円の半径 = %.3f, 丙円の半径 = %.3f, 丁円の半径 = %.3f, 戊円の半径 = %.3f\n", r1, r2, r3, r4, r5)
@printf("長 = %.3f, x = %.3f\n", w, x5)
plot([0, w, w, 0, 0], [0, 0, 595, 595, 0])
circle(w - r1, 595 - r1, r1)
circle(r2, r2, r2)
circle(r3, 595 - r3, r3)
circle(w - r4, r4, r4)
circle(x5, r5, r5)
if more == true
hline!([0], color=:black, lw=0.5)
vline!([0], color=:black, lw=0.5)
else
plot!(showaxis=false)
end
end;
甲円の半径 = 213.000, 乙円の半径 = 164.173, 丙円の半径 = 134.088, 丁円の半径 = 96.003, 戊円の半径 = 88.287
長 = 685.087, x = 404.957