裏 RjpWiki

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

算額(その217)

2023年04月26日 | Julia

算額(その217)

中村信弥「改訂増補 長野県の算額」
http://www.wasan.jp/zoho/zoho.html
県内の算額2(145)
長野県下伊那郡阿智村伍加向関 向関天満宮 天保9年(1838)

円内に水平な弦を引き,弦の上に甲円と2個の丙円,下に乙円と2個の丙円を置く。甲円と丙円,乙円と丙円は外接し,それぞれの円は弦にも接している。甲円と乙円の径が 6寸4分,1寸6分 としたとき,丙円の径はいかほどか。

外円,甲円,乙円,丙円の半径をそれぞれ R, r1, r2, r3 とおく。最も右側の丙円と右から2番めの丙円の中心座標を (x3, R - 2r1 + r3), (x2, R - 2r1 - r3) とする。甲円,乙円の中心は y 軸上にあり,それぞれの座標位置は R - r1, R - 2r1 - r2 である。
弦が円と交わる点の座標を (x, y) = (x, R - 2r1) とすると,x = sqrt(R^2 - y^2) である。

以下の連立方程式を解き,R, r3, x2, x3 を求める。これらは r1, r2, により表される。

using SymPy

@syms R::positive, r3::positive, x2::positive, x3::positive, r1::positive, r2::positive;

eq1 = x3^2 + (r1 - r3)^2 - (r1 + r3)^2
eq2 = x3^2 + (R - 2r1 + r3)^2 - (R - r3)^2
eq3 = x2^2 + (R - 2r1 - r3)^2 - (R - r3)^2
eq4 = x2^2 + (r3 - r2)^2 - (r2 + r3)^2

res = solve([eq1, eq2, eq3, eq4], (R, r3, x2, x3))

   1-element Vector{NTuple{4, Sym}}:
    (r1 + r2, r1*r2/(r1 + r2), 2*sqrt(r1)*r2/sqrt(r1 + r2), 2*r1*sqrt(r2)/sqrt(r1 + r2))

乙円の半径 r3 は r1*r2/(r1 + r2) で計算できる。すなわち,「甲円と乙円の半径の積を甲円と乙円の半径の和で割る」。当然のことであるが術と一致する。

   r1 = 6.40000;  r2 = 1.60000;  r3 = 1.28000;  R = 8.00000;  x2 = 2.86217;  x3 = 5.72433


任意の r1, r2 で図を描くことができるが,算額の図は r1 = 10, r2 = 4 ぐらいで描いたものと思われる。

   r1 = 10.00000;  r2 = 4.00000;  r3 = 2.85714;  R = 14.00000;  x2 = 6.76123;  x3 = 10.69045
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=:blue, 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(r1, r2, more)
    pyplot(size=(500, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
   (R, r3, x2, x3) = (r1 + r2, r1*r2/(r1 + r2), 2*sqrt(r1)*r2/sqrt(r1 + r2), 2*r1*sqrt(r2)/sqrt(r1 + r2))
   @printf("r1 = %.5f;  r2 = %.5f;  r3 = %.5f;  R = %.5f;  x2 = %.5f;  x3 = %.5f\n", r1, r2, r3, R, x2, x3)
   y = R - 2r1
   plot()
   circle(0, 0, R, :blue)
   circle(0, R - r1, r1, :green)
   circle(0, y - r2, r2, :magenta)
   circle(x3, y + r3, r3, :orange)
   circle(-x3, y + r3, r3, :orange)
   circle(x2, y - r3, r3, :orange)
   circle(-x2, y - r3, r3, :orange)
   x = sqrt(R^2 - y^2)
   segment(-x, y, x, y, 1)
   if more == true
       point(R, 0, "R ", :blue, :right, :bottom)
       point(0, R, "R ", :blue, :right, :bottom)
       point(0, R - r1, " R - r1")
       point(0, R - 2r1, " R-2r1")
       point(0, R - 2r1 - r2, " R-2r1-r2")
       point(x2, R - 2r1 - r3, "(x2,R-2r1-r3)")
       point(x3, R - 2r1 + r3, "(x2,R-2r1+r3)", :blue, :center)
       hline!([0], color=:black, lw=0.5)
       vline!([0], color=:black, lw=0.5)
   else
       plot!(showaxis=false)
   end
end;

 

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

算額(その216)

2023年04月25日 | Julia

算額(その216)

中村信弥「改訂増補 長野県の算額」
http://www.wasan.jp/zoho/zoho.html
県内の算額2(134)
長野県長野市元善町 善光寺 天保3年(1832)

円内に正三角形と乙円 6 個,正三角形に 2 本の斜線を引き正三角形内に甲円 2 個を入れる。乙円の径を知って,甲円の径を求めよ。

外円,甲円,乙円の半径をそれぞれ R, r1, r2 とする。r2 が既知ということで,適当な数値を割り当てておく。甲円の径は乙円の径の倍数で表される。ここでは適当に,r2 = 23 とおいて解く。
また,上の甲円の中心座標を (0, y1),最も右の乙円の中心座標を (x2, y2) とおく。
また,線分の端点 (X,Y) を定め,甲円,乙円からそれぞれの直線までの距離に関して連立方程式を立てる。

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::positive, y1::positive, r1::positive, x2::positive, y2::positive, r2::positive, X::positive, Y::positive;
r2 = 23
sq3 = sqrt(Sym(3))
eq1 = x2^2 + y2^2 - (R - r2)^2
eq2 = distance(-sq3*R/2, -R/2, sq3*R/2, R/2, x2, y2) - r2^2 |> expand |> simplify
eq3 = distance(0, R, sq3*R/2, -R/2, x2, y2) - r2^2 |> expand |> simplify
eq4 = distance(-sq3*R/2, -R/2, X, Y, 0, y1) - r1^2 |> expand |> simplify
eq5 = distance(0, R, sq3*R/2, -R/2, 0, y1) - r1^2 |> expand |> simplify
eq6 = distance(-sq3*R/2, -R/2, X, Y, 0, r1-R/2) - r1^2 |> expand |> simplify
eq7 = sq3*(sq3*R/2 -X) - (Y + R/2);

println(eq1, ",")
println(eq2, ",")
println(eq3, ",")
println(eq4, ",")
println(eq5, ",")
println(eq6, ",")
println(eq7, ",")

   x2^2 + y2^2 - (R - 23)^2,
   (x2/4 - sqrt(3)*y2/4)^2 + (-sqrt(3)*x2/4 + 3*y2/4)^2 - 529,
   (-R/4 + sqrt(3)*x2/4 + y2/4)^2 + (-sqrt(3)*R/4 + 3*x2/4 + sqrt(3)*y2/4)^2 - 529,
   -r1^2 + (y1 - (-3*R^3*X/4 + 3*sqrt(3)*R^3*Y/4 + sqrt(3)*R^3*y1/4 - sqrt(3)*R^2*X^2 + 3*R^2*X*Y + R^2*X*y1/2 + sqrt(3)*R^2*Y*y1 - R*X^3 + sqrt(3)*R*X^2*Y + 2*R*X*Y*y1 + sqrt(3)*R*Y^2*y1 + 2*X*Y^2*y1)/(sqrt(3)*R^3 + 5*R^2*X + sqrt(3)*R^2*Y + 3*sqrt(3)*R*X^2 + 2*R*X*Y + sqrt(3)*R*Y^2 + 2*X^3 + 2*X*Y^2))^2 + (sqrt(3)*R^3*X/4 - 3*R^3*Y/4 + 3*R^3*y1/4 + R^2*X^2/2 + sqrt(3)*R^2*X*y1 - 3*R^2*Y^2/2 + 3*R^2*Y*y1/2 + R*X^2*Y + R*X^2*y1 - sqrt(3)*R*X*Y^2 + 2*sqrt(3)*R*X*Y*y1 + 2*X^2*Y*y1)^2/(sqrt(3)*R^3 + 5*R^2*X + sqrt(3)*R^2*Y + 3*sqrt(3)*R*X^2 + 2*R*X*Y + sqrt(3)*R*Y^2 + 2*X^3 + 2*X*Y^2)^2,
   -r1^2 + (-R/4 + y1/4)^2 + 3*(R - y1)^2/16,
   -r1^2 + (-R/2 + r1 - (-R*(sqrt(3)*R^3 + 5*R^2*X + sqrt(3)*R^2*Y + 3*sqrt(3)*R*X^2 + 2*R*X*Y + sqrt(3)*R*Y^2 + 2*X^3 + 2*X*Y^2)/2 + (R + 2*Y)*(3*sqrt(3)*R^3 + 12*R^2*X + 2*sqrt(3)*R^2*r1 + 4*sqrt(3)*R*X^2 + 4*R*X*r1 + 4*sqrt(3)*R*Y*r1 + 8*X*Y*r1)/8)/(sqrt(3)*R^3 + 5*R^2*X + sqrt(3)*R^2*Y + 3*sqrt(3)*R*X^2 + 2*R*X*Y + sqrt(3)*R*Y^2 + 2*X^3 + 2*X*Y^2))^2 + (-3*R^4/8 - sqrt(3)*R^3*X/4 - 3*R^3*Y/2 + 3*R^3*r1/4 - sqrt(3)*R^2*X*Y + sqrt(3)*R^2*X*r1 - 3*R^2*Y^2/2 + 3*R^2*Y*r1/2 + R*X^2*r1 - sqrt(3)*R*X*Y^2 + 2*sqrt(3)*R*X*Y*r1 + 2*X^2*Y*r1)^2/(sqrt(3)*R^3 + 5*R^2*X + sqrt(3)*R^2*Y + 3*sqrt(3)*R*X^2 + 2*R*X*Y + sqrt(3)*R*Y^2 + 2*X^3 + 2*X*Y^2)^2,
   -R/2 - Y + sqrt(3)*(sqrt(3)*R/2 - X),

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, y1, r1, x2, y2, X, Y) = u
   return [
       x2^2 + y2^2 - (R - 23)^2,
       (x2/4 - sqrt(3)*y2/4)^2 + (-sqrt(3)*x2/4 + 3*y2/4)^2 - 529,
       (-R/4 + sqrt(3)*x2/4 + y2/4)^2 + (-sqrt(3)*R/4 + 3*x2/4 + sqrt(3)*y2/4)^2 - 529,
       -r1^2 + (y1 - (-3*R^3*X/4 + 3*sqrt(3)*R^3*Y/4 + sqrt(3)*R^3*y1/4 - sqrt(3)*R^2*X^2 + 3*R^2*X*Y + R^2*X*y1/2 + sqrt(3)*R^2*Y*y1 - R*X^3 + sqrt(3)*R*X^2*Y + 2*R*X*Y*y1 + sqrt(3)*R*Y^2*y1 + 2*X*Y^2*y1)/(sqrt(3)*R^3 + 5*R^2*X + sqrt(3)*R^2*Y + 3*sqrt(3)*R*X^2 + 2*R*X*Y + sqrt(3)*R*Y^2 + 2*X^3 + 2*X*Y^2))^2 + (sqrt(3)*R^3*X/4 - 3*R^3*Y/4 + 3*R^3*y1/4 + R^2*X^2/2 + sqrt(3)*R^2*X*y1 - 3*R^2*Y^2/2 + 3*R^2*Y*y1/2 + R*X^2*Y + R*X^2*y1 - sqrt(3)*R*X*Y^2 + 2*sqrt(3)*R*X*Y*y1 + 2*X^2*Y*y1)^2/(sqrt(3)*R^3 + 5*R^2*X + sqrt(3)*R^2*Y + 3*sqrt(3)*R*X^2 + 2*R*X*Y + sqrt(3)*R*Y^2 + 2*X^3 + 2*X*Y^2)^2,
       -r1^2 + (-R/4 + y1/4)^2 + 3*(R - y1)^2/16,
       -r1^2 + (-R/2 + r1 - (-R*(sqrt(3)*R^3 + 5*R^2*X + sqrt(3)*R^2*Y + 3*sqrt(3)*R*X^2 + 2*R*X*Y + sqrt(3)*R*Y^2 + 2*X^3 + 2*X*Y^2)/2 + (R + 2*Y)*(3*sqrt(3)*R^3 + 12*R^2*X + 2*sqrt(3)*R^2*r1 + 4*sqrt(3)*R*X^2 + 4*R*X*r1 + 4*sqrt(3)*R*Y*r1 + 8*X*Y*r1)/8)/(sqrt(3)*R^3 + 5*R^2*X + sqrt(3)*R^2*Y + 3*sqrt(3)*R*X^2 + 2*R*X*Y + sqrt(3)*R*Y^2 + 2*X^3 + 2*X*Y^2))^2 + (-3*R^4/8 - sqrt(3)*R^3*X/4 - 3*R^3*Y/2 + 3*R^3*r1/4 - sqrt(3)*R^2*X*Y + sqrt(3)*R^2*X*r1 - 3*R^2*Y^2/2 + 3*R^2*Y*r1/2 + R*X^2*r1 - sqrt(3)*R*X*Y^2 + 2*sqrt(3)*R*X*Y*r1 + 2*X^2*Y*r1)^2/(sqrt(3)*R^3 + 5*R^2*X + sqrt(3)*R^2*Y + 3*sqrt(3)*R*X^2 + 2*R*X*Y + sqrt(3)*R*Y^2 + 2*X^3 + 2*X*Y^2)^2,
       -R/2 - Y + sqrt(3)*(sqrt(3)*R/2 - X),
      ]
end;

iniv = [big"100.0", 40, 29, 74, 17, 38, 35]
res = nls(H, ini=iniv);
(R, y1, r1, x2, y2, X, Y) = res[1]

   7-element Vector{BigFloat}:
    99.11622476544556957238989434845238340897947997475365391347542734808144883286993
    44.55172637545979555630210417618609080956802950433982356837680886832301306912858
    27.28224919499288700804389085331208997911328616254797832971744726175111068974111
    74.33716857408417737634750791824531083825876799588770930380613622091470494778482
    16.36047190431930186378547790223141187137407375182171202155806842641558229407336
    36.06913041500994321125272707530450704350748214455846748909600618940625778785056
    36.64265830182042962183937816811331567947926445575786571111108150485370011531358

   r2 = 23.00000;  R = 99.11622;  y1 = 44.55173;  r1 = 27.28225;  x2 = 74.33717;  y2 = 16.36047;  X = 36.06913;  Y = 36.64266

甲円の径は乙円の径の約 27.282249195... / 23 ≒ 1.1861847476083864 倍である。

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 circle4(x, y, r, color=:red)
   circle(x, y, r, color)
   circle(x, -y, r, color)
   circle(-x, y, r, color)
   circle(-x, -y, r, color)
end;

function circle42(x, y, r, color=:red)
   circle(x, y, r, color)
   circle(x, -y, r, color)
   circle(y, x, r, color)
   circle(-y, x, r, color)
end;

function point(x, y, string="", color=:blue, 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)
    pyplot(size=(500, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
   (R, y1, r1, x2, y2, X, Y) = res[1]
   r2 = 23
   @printf("r2 = %.5f;  R = %.5f;  y1 = %.5f;  r1 = %.5f;  x2 = %.5f;  y2 = %.5f;  X = %.5f;  Y = %.5f\n", r2, R, y1, r1, x2, y2, X, Y)
   l = R - r2
   θ = pi/6 + atan(r2 / sqrt(l^2 - r2^2))
   plot([√3R/2, 0, -√3R/2, √3R/2], [-R/2, R, -R/2, -R/2], color=:black, lw=0.5)
   circle(0, 0, R, :blue)
   circle(0, y1, r1, :magenta)
   circle(0, r1 - R/2, r1, :magenta)
   circle(x2, y2, r2, :red)
   circle(-x2, y2, r2, :red)
   circle(l*cos(θ), l*sin(θ), r2, :red)
   circle(-l*cos(θ), l*sin(θ), r2, :red)
   circle(r2, -r2 - R/2, r2, :red)
   circle(-r2, -r2 - R/2, r2, :red)
   segment(sqrt(3)*R/2, -R/2, -X, Y, :green)
   segment(-sqrt(3)*R/2, -R/2, X, Y, :green)
   if more == true
       point(0, y1, " y1 甲")
       point(0, y1 + r1, " y1+r1", :blue, :center)
       point(0, r1 - R/2, " r1-R/2")
       point(x2, y2, "(x2,y2) 乙")
       point(√3R/2, -R/2, "(√3R/2,-R/2)")
       point(0, R, " R")
       point(R, 0, " R")
       point(X, Y, " (X,Y)", :blue, :left, :bottom)
       hline!([0], color=:black, lw=0.5)
       vline!([0], color=:black, lw=0.5)
   else
       plot!(showaxis=false)
   end
end;

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

算額(その215)

2023年04月25日 | Julia

算額(その215)

中村信弥「改訂増補 長野県の算額」
http://www.wasan.jp/zoho/zoho.html
県内の算額2(134)
長野県長野市元善町 善光寺 天保3年(1832)

鉤股の中に正方形,全円,中円,小円が入っている。3 個の円の径の和の 3 乗に鉤股弦の3辺の和を加えると 175784 寸になるとき,鉤,股の長さを求めよ。

全円,中円,小円の半径を r1, r2, r3,正方形の一辺の長さを a とする。a = r1*(1 + 1/√2)
鉤,股,弦の長さを x, y, z とする。z^2 = x^2 + y^2 

using SymPy

@syms r1::positive, r2::positive, r3::positive, a::positive, x::positive, y::positive, z::positive;

鉤股弦に内接する円の直径に関する公式により
eq1 = (x + y - z) - 2r1

鉤股弦の定理により

eq2 = x^2 + y^2 - z^2eq3 = r1*(1 + 1/√Sym(2)) - a

中円が内接する直角三角形において,鉤 = a,股 = y - a,より弦を求め,更に中円の径を求める。

eq4 = a + (y - a) - sqrt(a^2 + (y - a)^2) - 2r2

小円が内接する直角三角形において,鉤 = x - a,股 = a,より弦を求め,更に小円の径を求める。

eq5 = (x - a) + a - sqrt((x - a)^2 + a^2) - 2r3
eq6 = sqrt(a^2 + (y - a)^2) + sqrt((x - a)^2 + a^2) - z


3 円の径の 3 乗と鉤股弦の長さの和が 175784 寸になること。

eq7 = (2r1 + 2r2 + 2r3)^3 + (x + y + z) - 175784

# res = solve([eq1, eq2, eq3, eq4, eq5, eq6, eq7], (r1, r2, r3, a, x, y, z))

println(eq1, ",")
println(eq2, ",")
println(eq3, ",")
println(eq4, ",")
println(eq5, ",")
println(eq6, ",")
println(eq7, ",")


   -2*r1 + x + y - z,
   x^2 + y^2 - z^2,
   -a + r1*(sqrt(2)/2 + 1),
   -2*r2 + y - sqrt(a^2 + (-a + y)^2),
   -2*r3 + x - sqrt(a^2 + (-a + x)^2),
   -z + sqrt(a^2 + (-a + x)^2) + sqrt(a^2 + (-a + y)^2),
   x + y + z + (2*r1 + 2*r2 + 2*r3)^3 - 175784,

nlsolve() に与える初期値として,以下では [14, 8, 6, 24, 42, 56, 70] を与えている。これが何であるかは,後ほど明らかにする。

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, a, x, y, z) = u
   return [
       -2*r1 + x + y - z,
       x^2 + y^2 - z^2,
       -a + r1*(sqrt(2)/2 + 1),
       -2*r2 + y - sqrt(a^2 + (-a + y)^2),
       -2*r3 + x - sqrt(a^2 + (-a + x)^2),
       -z + sqrt(a^2 + (-a + x)^2) + sqrt(a^2 + (-a + y)^2),
       x + y + z + (2*r1 + 2*r2 + 2*r3)^3 - 175784,
      ]
end;

iniv = [big"14.0", 8, 6, 24, 42, 56, 70]
res = nls(H, ini=iniv);

(r1, r2, r3, a, x, y, z) = res[1]

   7-element Vector{BigFloat}:
    14.00009199718881266997164505667248631437239191211784047768957946446283538592614
     7.530673092915759606828706615206250886849777022685165021155300562153373379538257
     6.469418904767690724808290956523842847260715462871629533555945388717614860294677
    23.89965205591648493671409981825858528719421105627886838770965718274749524331951
    44.47523941973287034607539732539475983162747384861150323437216124063871614521499
    51.79378645760826848096196046976934542743851611350476010640996323602390734022659
    68.26890380908048029730932373318754118070670892490859464615855881182802342371832

この結果は,条件式を満たすがその制度は低い。

residuals = [-2*r1 + x + y - z,
x^2 + y^2 - z^2,
-a + r1*(sqrt(2)/2 + 1),
-2*r2 + y - sqrt(a^2 + (-a + y)^2),
-2*r3 + x - sqrt(a^2 + (-a + x)^2),
-z + sqrt(a^2 + (-a + x)^2) + sqrt(a^2 + (-a + y)^2),
x + y + z + (2*r1 + 2*r2 + 2*r3)^3 - 175784];
residuals

   7-element Vector{BigFloat}:
    -6.192611696681021525605136840855038550278702801226075559326409107071012901001753e-05
     9.765402982411353192658777533349762286583446730354184844530409424450095677342936e-06
    -7.027994826461461300837844291342422836735678351587353901813809597787233631272473e-08
    -5.355063501058776092617519186570005715143107383625329191134445449929575796103538e-05
    -5.608181214456910119933111617475016179932686281639492614172558048509090495266509e-05
     4.7705340913023316164424824417060357246824030732233419126833127607968720315658e-05
    -4.706396192122615236089639278606438686047887003738046689427877440546358609517795e-08

得られたパラメータで図に描いてみれば,誤差は気にならない。
   r1 = 14.00009;  r2 = 7.53067;  r3 = 6.46942;  a = 23.89965;  x = 44.47524;  y = 51.79379;  z = 68.26890
   (2r1 + 2r2 + 2r3)^3 + (x + y + z) = 175784.00000

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=:blue, 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, a, x, y, z) = res[1]
   @printf("r1 = %.5f;  r2 = %.5f;  r3 = %.5f;  a = %.5f;  x = %.5f;  y = %.5f;  z = %.5f\n", r1, r2, r3, a, x, y, z)
   @printf("(2r1 + 2r2 + 2r3)^3 + (x + y + z) = %.5f\n",  (2r1 + 2r2 + 2r3)^3 + (x + y + z))
   plot([0, y, 0, 0], [0, 0, x, 0], color=:black, lw=0.5)
   plot!([0, a, a, 0, 0], [0, 0, a, a, 0], color=:orange, lw=0.5)
   circle(r1, r1, r1, :blue)
   circle(a + r2, r2, r2, :red)
   circle(r3, a + r3, r3, :magenta)
   if more == true
       point(r1, r1, "(r1,r1,r1) 全", :blue, :center)
       point(a + r2, r2, "(a+r2,r2,r2) 中", :red, :center)
       point(r3, a + r3, "(r3,a+r3,r3) 小", :magenta, :center)
       point(0, a, " a")
       point(0, x, " x", :blue, :left, :bottom)
       point(y, 0, " y", :blue, :left, :bottom)
       hline!([0], color=:black, lw=0.5)
       vline!([0], color=:black, lw=0.5)
   else
       plot!(showaxis=false)
   end
end;

draw(true)
savefig("fig1.png");

そもそも,算額の結果は整数解あるいはきれいな式で表されるのが普通なので,数値解を見てみると r1 が整数に近いこと,a は r1 ほどではないが整数値に近い。

r1 = 14 が算額における真値であると仮定する。鉤股弦に内接する円の径の方程式 eq1 = (x + y - z) - 2r1 より x + y - z = 28。
また当然 eq2 = x^2 + y^2 - z^2。

これを解くのに SymPy はちょっと力不足。

@syms x::positive, y::positive, z::positive;
eq1 = x + y - z - 28
eq2 = x^2 + y^2 - z^2;

1 ≦ x ≦ y ≦ 1000 で条件を満たすものはそんなに多くはない。

for x in 1:1000
   for y in x:1000
       z = round(Int, sqrt(x^2 + y^2))
       if x^2 + y^2 == z^2 && x + y - z == 28
           println("(x, y, z) = ($x, $y, $z)  GCD(x, y, z) = $(gcd(x, y, z))")
       end
   end
end

   (x, y, z) = (29, 420, 421)  GCD(x, y, z) = 1
   (x, y, z) = (30, 224, 226)  GCD(x, y, z) = 2
   (x, y, z) = (32, 126, 130)  GCD(x, y, z) = 2
   (x, y, z) = (35, 84, 91)  GCD(x, y, z) = 7
   (x, y, z) = (36, 77, 85)  GCD(x, y, z) = 1
   (x, y, z) = (42, 56, 70)  GCD(x, y, z) = 14

馴染みの深いのが (x, y, z) = (42, 56, 70) なので,これと当たりをつけて先に進む。

(x, y, z) = (42, 56, 70)
r1 = (x + y - z)/2  # 14
r1 |> println

   14.0

a = r1*(1 + 1/√2)  # eq3
a |> println

   23.899494936611664

これにより得られた a は数値解に近く,更に,ほぼ 24 であるのが好ましい。a = 24 にしよう。
更に,中円,小円の半径は (a + (y - a) - sqrt(a^2 + (y - a)^2))/2,((x - a) + a - sqrt((x - a)^2 + a^2) - 2r3)/2 である。

a = 24
r2 = (a + (y - a) - sqrt(a^2 + (y - a)^2)) / 2
r3 = ((x - a) + a - sqrt((x - a)^2 + a^2)) / 2
(r2, r3) |> println

   (8.0, 6.0)

(2r1 + 2r2 + 2r3)^3 + (x + y + z)  # 問に合う

   175784.0

図に描いても,a = 24 としたことによる誤差(斜辺と円接点が正方形の角になるべき)のはあまり目立たない。

function draw2()
    pyplot(size=(500, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
   (r1, r2, r3, a, x, y, z) = [14, 8, 6, 24, 42, 56, 70]
   @printf("r1 = %.5f;  r2 = %.5f;  r3 = %.5f;  a = %.5f;  x = %.5f;  y = %.5f;  z = %.5f\n", r1, r2, r3, a, x, y, z)
   @printf("(2r1 + 2r2 + 2r3)^3 + (x + y + z) = %.5f\n",  (2r1 + 2r2 + 2r3)^3 + (x + y + z))
   plot([0, y, 0, 0], [0, 0, x, 0], color=:black, lw=0.5)
   plot!([0, a, a, 0, 0], [0, 0, a, a, 0], color=:orange, lw=0.5)
   circle(r1, r1, r1, :blue)
   circle(a + r2, r2, r2, :red)
   circle(r3, a + r3, r3, :magenta)
   plot!(showaxis=false)
end;

draw2()
savefig("fig3.png");

   r1 = 14.00000;  r2 = 8.00000;  r3 = 6.00000;  a = 24.00000;  x = 42.00000;  y = 56.00000;  z = 70.00000
   (2r1 + 2r2 + 2r3)^3 + (x + y + z) = 175784.00000

 

 

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

算額(その214)

2023年04月25日 | Julia

算額(その214)

中村信弥「改訂増補 長野県の算額」
http://www.wasan.jp/zoho/zoho.html
県内の算額2(128)
長野県伊那市羽広 仲仙寺 天保2年(1831)

外円の中に大円を 5 個,中円,小円を 4 個ずつ入れる。小円の径が分かっているときに中円の径を求めよ。

外円,大円,中円,小円の半径を 2r1, r1, r2, r3 と置く。図中の中円,小円の中心座標を (x2, x2), (0, r1 - r3) と置く。

以下の連立方程式を解く。

using SymPy

@syms r1::positive, r2::positive, r3::positive, x2::positive;

eq1 = (r1 - x2)^2 + x2^2 - (r1 -r2)^2
eq2 = 2x2^2 - (r1 - r2)^2
eq3 = r1^2 + (r1 - r3)^2 - (r1 + r3)^2;

res = solve([eq1, eq2, eq3], (r1, r2, x2))

   2-element Vector{Tuple{Sym, Sym, Sym}}:
    (4*r3, 2*r3*(2 - sqrt(2)), 2*r3)
    (4*r3, 2*r3*(sqrt(2) + 2), 2*r3)

2 組目の解では中円の径が大円より大きくなるので不適切解である。

すなわち,中円の径 = 2*(2 - sqrt(2))×小円の径 である。術では「4から8の平方根を引いて小円の径を掛ける」である。
小円の径を 5 とすれば,以下のようになる。

2*(2 - sqrt(2))*5, (4 - sqrt(8))*5

   (5.857864376269049, 5.857864376269049)

   r1 = 20.00000;  r2 = 5.85786;  r3 = 5.00000;  x2 = 10.00000

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 circle4(x, y, r, color=:red)
   circle(x, y, r, color)
   circle(x, -y, r, color)
   circle(-x, y, r, color)
   circle(-x, -y, r, color)
end;

function circle42(x, y, r, color=:red)
   circle(x, y, r, color)
   circle(x, -y, r, color)
   circle(y, x, r, color)
   circle(-y, x, r, color)
end;

function point(x, y, string="", color=:blue, 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(r3, more)
    pyplot(size=(500, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
   (r1, r2, x2) = (4*r3, 2*r3*(2 - sqrt(2)), 2*r3)
   @printf("r1 = %.5f;  r2 = %.5f;  r3 = %.5f;  x2 = %.5f\n", r1, r2, r3, x2)
   plot()
   circle(0, 0, 2r1, :black)
   circle(0, 0, r1, :red)
   circle42(0, r1, r1, :red)
   circle42(0, r1 - r3, r3, :blue)
   circle4(x2, x2, r2, :green)
   if more == true
       point(r1-r3, 0, " r1-r3", :blue, :center)
       point(r1, 0, " r1 大円", :red)
       point(2r1, 0, " 2r1", :red)
       point(x2, x2, "(x2,x2,r2)", :green, :center)
       point(x2, x2, "中円", :green, :center, :bottom)
       point(0, r1-r3, "小円", :blue)
       hline!([0], color=:black, lw=0.5)
       vline!([0], color=:black, lw=0.5)
   else
       plot!(showaxis=false)
   end
end;

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

算額(その213)

2023年04月23日 | Julia

算額(その213)

中村信弥「改訂増補 長野県の算額」
http://www.wasan.jp/zoho/zoho.html
県内の算額2(127)
長野県伊那市羽広 仲仙寺 天保2年(1831)

外円の中に斜線 4 本,等円,甲円,乙円を4個ずつ入れる。外円の径が分かっているときに乙円の径を求めよ。

外円,等円,甲円,乙円の半径を R, r1, r2, r3 と置く。甲円,乙円の中心座標を (x2, R/2), (x3, R/2) と置く。第1象限にある斜線と円の交点を (x, y) と置く。

以下の連立方程式を解く。

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 r1::positive, r2::positive, r3::positive, x2::positive, x3::positive, x::positive, y::positive, R::positive;

eq1 = x2^2 + R^2//4 - (R - r2)^2
eq2 = distance(0, R/2, R, 0, 0, R - r1) - r1^2
eq3 = distance(0, R/2, R, 0, x2, R/2) - r2^2
eq4 = distance(0, R/2, R, 0, x3, R/2) - r3^2  
eq5 = x2 - r2 - r3 - x3
eq11 = x^2 + y^2 - R^2
eq12 = 2y - R - x

res = solve([eq1, eq2, eq3, eq4, eq5, eq11, eq12], (r1, r2, r3, x2, x3, x, y))

   1-element Vector{NTuple{7, Sym}}:
    (R*(-2 + sqrt(5)), R/4, R*(3 - sqrt(5))/8, sqrt(5)*R/4, R*(-5/8 + 3*sqrt(5)/8), 3*R/5, 4*R/5)

外円の径を R としたとき,乙円の径は R*(3 - sqrt(5))/8 である。
術では,「3 から 5 の平方根を引き 8 で割って外円径を掛ける」といっている。
R が 1 のとき,(3 - √5)/8 = 0.09549150281252627 である。
R = 1.00000;  r1 = 0.23607;  r2 = 0.25000;  r3 = 0.09549;  x2 = 0.55902;  x3 = 0.21353;  x = 0.60000;  y = 0.80000

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 circle4(x, y, r, color=:red)
   circle(x, y, r, color)
   circle(x, -y, r, color)
   circle(-x, y, r, color)
   circle(-x, -y, r, color)
end;

function point(x, y, string="", color=:blue, 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(R, more)
    pyplot(size=(500, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
   (r1, r2, r3, x2, x3, x, y) = (R*(-2 + sqrt(5)), R/4, R*(3 - sqrt(5))/8, sqrt(5)*R/4, R*(-5/8 + 3*sqrt(5)/8), 3*R/5, 4*R/5)
   @printf("R = %.5f;  r1 = %.5f;  r2 = %.5f;  r3 = %.5f;  x2 = %.5f;  x3 = %.5f;  x = %.5f;  y = %.5f\n", R, r1, r2, r3, x2, x3, x, y)
   plot()
   circle(0, 0, R, :red)
   circle(0, r1, r1, :blue)
   circle(0, R - r1, r1, :blue)
   circle(0, -r1, r1, :blue)
   circle(0, -R + r1, r1, :blue)
   circle4(x2, R/2, r2, :green)
   circle4(x3, R/2, r3, :orange)
   segment(-R, 0, x, y)
   segment(R, 0, -x, y)
   segment(-R, 0, x, -y)
   segment(R, 0, -x, -y)
   if more == true
       point(0, r1, " r1")
       point(0, r1, " 等円", :blue, :left, :bottom)
       point(0, R - r1, " R-r1")
       point(0, R, " R", :blue, :left, :bottom)
       point(R, 0, " R", :blue, :left, :bottom)
       point(x2, R/2, " (x2,R/2)", :green)
       point(x2, R/2, " 甲円 r2", :green, :left, :bottom)
       point(x3, R/2, " (x3,R/2)", :orange)
       point(x3, R/2, " 乙円 r3", :orange, :left, :bottom)
       point(x, y, " (x,y)", :blue, :left, :bottom)
       hline!([0], color=:black, lw=0.5)
       vline!([0], color=:black, lw=0.5)
   else
       plot!(showaxis=false)
   end
end;

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

算額(その212)

2023年04月22日 | Julia

算額(その212)

中村信弥「改訂増補 長野県の算額」
http://www.wasan.jp/zoho/zoho.html
県内の算額2(127)
長野県伊那市羽広 仲仙寺 天保2年(1831)

長方形の中に三角形を構成する小斜,中斜,大斜の 3 本の斜線を引き各領域に等円 3 個を入れる。小斜が 197 寸,大斜と中斜の差が 167 寸であるとき,等円の径を求めよ。

等円の半径を r1,大斜,中斜の長さをそれぞれ l, m とする。また,長方形の短辺,長辺を h, w,短辺上,長辺上の斜線との交点座標を (x, h), (w, y) とする。

以下の 7 連立方程式を解く。

追記:後半に,solve() で解く別解を示す。

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 w::positive, h::positive, x::positive, y::positive, r::positive, l::positive, m::positive;

eq1 = (w - x)^2 + (h - y)^2 - 197^2
eq2 = distance(x, h, w, y, w - r, h - r) - r^2
eq3 = w^2 + y^2 - l^2
eq4 = distance(0, 0, w, y, w - r, r) - r^2
eq5 = x^2 + h^2 - m^2
eq6 = distance(0, 0, x, h, r, h - r) - r^2
eq7 = l - m - 167;

# res = solve([eq1, eq2, eq3, eq4, eq5, eq6, eq7])

println(eq1, ",")
println(eq2, ",")
println(eq3, ",")
println(eq4, ",")
println(eq5, ",")
println(eq6, ",")
println(eq7, ",")

   (h - y)^2 + (w - x)^2 - 38809,
   -r^2 + (h - r - ((h - r)*(h^2 - 2*h*y + w^2 - 2*w*x + x^2 + y^2) + (w - x)*(h*r - h*w + h*x + r*w - r*x - r*y + w*y - x*y))/(h^2 - 2*h*y + w^2 - 2*w*x + x^2 + y^2))^2 + (-r + w - ((h - y)*(h*r - h*w + h*x + r*w - r*x - r*y + w*y - x*y) + (-r + w)*(h^2 - 2*h*y + w^2 - 2*w*x + x^2 + y^2))/(h^2 - 2*h*y + w^2 - 2*w*x + x^2 + y^2))^2,
   -l^2 + w^2 + y^2,
   -r^2 + (r - y*(-r*w + r*y + w^2)/(w^2 + y^2))^2 + (-r + w - w*(-r*w + r*y + w^2)/(w^2 + y^2))^2,
   h^2 - m^2 + x^2,
   -r^2 + (r - x*(h^2 - h*r + r*x)/(h^2 + x^2))^2 + (h - h*(h^2 - h*r + r*x)/(h^2 + x^2) - r)^2,
   l - m - 167,

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)
   (w, h, x, y, r, l, m) = u
   return [
(h - y)^2 + (w - x)^2 - 38809,
-r^2 + (h - r - (h*(h^2 - 2*h*y + w^2 - 2*w*x + x^2 + y^2) - (h - y)*(h*r - r*w + r*x - r*y + w^2 - 2*w*x + x^2))/(h^2 - 2*h*y + w^2 - 2*w*x + x^2 + y^2))^2 + (-r + w - (x*(h^2 - 2*h*y + w^2 - 2*w*x + x^2 + y^2) + (w - x)*(h*r - r*w + r*x - r*y + w^2 - 2*w*x + x^2))/(h^2 - 2*h*y + w^2 - 2*w*x + x^2 + y^2))^2,
-l^2 + w^2 + y^2,
-r^2 + (r - y*(-r*w + r*y + w^2)/(w^2 + y^2))^2 + (-r + w - w*(-r*w + r*y + w^2)/(w^2 + y^2))^2,
h^2 - m^2 + x^2,
-r^2 + (r - x*(h^2 - h*r + r*x)/(h^2 + x^2))^2 + (h - h*(h^2 - h*r + r*x)/(h^2 + x^2) - r)^2,
l - m - 167,
      ]
end;

iniv = [big"250.0", 125, 80, 60, 50, 260, 135]
res = nls(H, ini=iniv)
println(res)


   (BigFloat[232.4044469772863381238028027822743717781137151621708810104479568177241870837339, 55.63756161725137162803324021866819269065990883777636104258854899113324532126497, 37.40444697728633812380280375316414124163853497849054096364471888965060106357849, 27.63756161725137162803323070272197693210543500789238221320524683591722634272648, 13.00000000000000000000000453359762710515940900185361961353502389265049286768763, 234.042008594537709751836022952215178167849774200045164979268135278539486810265, 67.04200859453770975183602295221517816784977420004516497926813528879817944192434], true)

w = 232.40445;  h = 55.63756;  x = 37.40445;  y = 27.63756;  l = 234.04201;  m = 67.04201
等円半径 = r = 13.00000;  等円直径 = 26.00000;

よくあることであるが,算額の図のアスペクト比は実際の図とずいぶん異なる。

等円の直径は 26 寸である。

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=:blue, 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(a, b, more)
    pyplot(size=(500, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
   (w, h, x, y, r, l, m) = res[1]
   @printf("w = %.5f;  h = %.5f;  x = %.5f;  y = %.5f;  l = %.5f;  m = %.5f\n", w, h, x, y, l, m)
   @printf("等円半径 = r = %.5f;  等円直径 = %.5f;  \n", r, 2r)
   plot([0, w, w, 0, 0], [0, 0, h, h, 0], color=:black, lwd=0.5)
   circle(r, h - r, r, :blue)
   circle(w - r, r, r, :blue)
   circle(w - r, h - r, r, :blue)
   plot!([0, w, x, 0], [0, y, h, 0, 0], color=:orange, lwd=0.5)
   if more == true
       point(r, h-r, "(r,h-r)", :blue, :center)
       point(w - r, h - r, "(w-r,h-r)", :blue, :right)
       point(w - r, r, "(w-r,y)", :blue, :center)
       point(x, h, " (x,h)", :blue, :left, :bottom)
       point(w, y, " y", :blue, :left, :bottom)
       point(w, 0, " w", :blue, :left, :bottom)
       point(0, h, " h", :blue, :left, :bottom)
       hline!([0], color=:black, lw=0.5)
       vline!([0], color=:black, lw=0.5)
   else
       plot!(showaxis=false)
   end
end;

別解

鉤股弦に内接する円の径は,径 = 鉤 + 股 - 弦 で求めることができる。式が簡単なので,7 次元連立方程式になるが solve() で解くことができる。

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 w::positive, h::positive, x::positive, y::positive, r::positive, l::positive, m::positive;

eq1 = (w - x)^2 + (h - y)^2 - 197^2
eq2 = h - y + w - x - 197  - r
eq3 = w^2 + y^2 - l^2
eq4 = x + h - m - r
eq5 = x^2 + h^2 - m^2
eq6 = w + y - l - r
eq7 = l - m - 167;

res = solve([eq1, eq2, eq3, eq4, eq5, eq6, eq7], (w, h, x, y, r, l, m))

   1-element Vector{NTuple{7, Sym}}:
    (13*sqrt(13755)/14 + 247/2, 2*sqrt(13755)/15 + 40, -143/2 + 13*sqrt(13755)/14, 12 + 2*sqrt(13755)/15, 26, 219/2 + 223*sqrt(13755)/210, -115/2 + 223*sqrt(13755)/210)

using Printf
(w, h, x, y, r, l, m) = res[1]
@printf("w = %.5f;  h = %.5f;  x = %.5f;  y = %.5f;  l = %.5f;  m = %.5f\n", w, h, x, y, l, m)
@printf("等円半径 = r = %.5f;  等円直径 = %.5f;  \n", r, 2r)

   w = 232.40445;  h = 55.63756;  x = 37.40445;  y = 27.63756;  l = 234.04201;  m = 67.04201
   等円半径 = r = 26.00000;  等円直径 = 52.00000;  

 

 

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

算額(その211)

2023年04月22日 | Julia

算額(その211)

中村信弥「改訂増補 長野県の算額」
http://www.wasan.jp/zoho/zoho.html
県内の算額2(126)
長野県伊那市羽広 仲仙寺 天保2年(1831)

正方形の中に4本の斜線を引き各領域に等円 5 個,甲円,乙円をそれぞれ 4 個入れる。等円と乙円の径の差が与えられたとき,甲円の径を求めよ。

正方形の一辺の長さを a,等円,甲円,乙円の半径を r1,r2,r3 とする。
以下の 3 連立方程式を解くが,まず eq1 で r1 を a について解いて r1 とする(そうしないとあとの解が複雑になる)。以下 r2, r3 も a を含む式になる。

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 r1::positive, r2::positive, r3::positive, a::positive;

等円の半径 r1 を求める。

eq1 = 2(a - r1)^2 - (2r1)^2
res1 = solve(eq1, r1)[1]
r1 = res1
res1 |> println  # r1

   a*(-1 + sqrt(2))

甲円の半径 r2 を求める。

eq2 = distance(a, sqrt(Sym(2))r1 - a, sqrt(Sym(2))r1 - a, a, a - r2, 0) - r2^2
res2 = solve(eq2, r2)[1]
res2 |> println  # r2

   a*(3 - 2*sqrt(2))

乙円の半径を求める。

eq3 = distance(a, sqrt(Sym(2))r1 - a, sqrt(Sym(2))r1 - a, a, r1 + r3, 0) - r3^2
res3 = solve(eq3, r3)[1] |> sympy.sqrtdenest |> simplify
res3 |> println  # r3

   a*(-7 + 5*sqrt(2))

甲円の径と(等円の径 - 乙円の径)の比を求める。

res2 / (res1 - res3) |> simplify |> println  # r2 / (r1 - r3)

   1/2

上の式は,「甲円の径 / (等円の径 - 乙円の径) = 1 / 2」なので,「甲円の径 = (等円の径 - 乙円の径) ÷ 2」,つまり「等円と乙円の径の差の半分が甲円の径」である。

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=:blue, 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(a, b, more)
    pyplot(size=(500, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
   a = 10
   r1 = (√2-1)a
   r2 = (3 - 2*sqrt(2))a
   r3 = (5*sqrt(2) - 7)a
   plot([a, a, -a, -a, a], [-a, a, a, -a, -a], color=:black, lwd=0.5)
   circle(0, 0, r1, :blue)
   circle(a - r1, a - r1, r1, :blue)
   circle(a - r1, r1 - a, r1, :blue)
   circle(r1 - a, a - r1, r1, :blue)
   circle(r1 - a, r1 - a, r1, :blue)
   circle(a - r2, 0, r2)
   circle(r2 - a, 0, r2)
   circle(0, a - r2, r2)
   circle(0, r2 - a, r2)
   circle(r1 + r3, 0, r3, :green)
   circle(-r1 + -r3, 0, r3, :green)
   circle(0, r1 + r3, r3, :green)
   circle(0, -r1 + -r3, r3, :green)
   segment(a, √2r1 - a, √2r1 - a, a, :orange)
   segment(a, a - √2r1, √2r1-a, -a, :orange)
   segment(-a, √2r1-a, a - √2r1, a, :orange)
   segment(-a, a - √2r1, a - √2r1, -a, :orange)
   if more == true
       point(a - r1, a - r1, "等円: (a-r1,a-r1)", :blue, :top)
       point(0, a - r2, "甲円: a-r2", :red, :center)
       point(0, r1 + r3, " 乙円: r1+r3 ", :green)
       point(a, √2r1 - a, "√2r1-a ", :blue, :right)
       point(√2r1 - a, a, " √2r1-a", :blue, :left, :bottom)
       hline!([0], color=:black, lw=0.5)
       vline!([0], color=:black, lw=0.5)
   else
       plot!(showaxis=false)
   end
end;

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

算額(その210)

2023年04月20日 | Julia

算額(その210)

中村信弥「改訂増補 長野県の算額」
http://www.wasan.jp/zoho/zoho.html
県内の算額2(124)
長野県木曽郡植松町 臨川寺弁財天社 文政13年(1830)

外円内に,弦,斜,矢を引き,天円 1 個,地円 2 個を入れる。矢および斜が与えられたとき,天円の径を求めよ。

外円の半径を R,矢,斜の長さをそれぞれ a, b とする。
地円の半径 r1,中心座標を (r1, y1)
天円の半径 r2,中心座標を (0, R - r2)
弦と外円の交点座標を (x, a - R) 
として方程式を立てる。

y1 は負の値も取りうるので ::positive を指定しない。

using SymPy

@syms y1, r1::positive, R::positive, r2::positive, x::positive, a::positive, b::positive;

eq1 = r1^2 + y1^2 - (R - r1)^2
eq2 = r1^2 + (R - r2 - y1)^2 - (r1 + r2)^2
eq3 = x^2 + a^2 - b^2
eq4 = x^2 + (a - R)^2 - R^2
eq5 = y1 + (R - a) - r1

以上の連立方程式を a, b について解く。

res = solve([eq1, eq2, eq3, eq4, eq5], (y1, r1, r2, x, R))

   1-element Vector{NTuple{5, Sym}}:
    (b*(2*a - b)/(2*a), -a + b, (-a*b^2 + b^3)/(2*a^2 + 2*a*b), sqrt(-a + b)*sqrt(a + b), b^2/(2*a))

任意の a, b を式に与えれば解が得られる。

a, b = 3, 5
(b*(2*a - b)/(2*a), -a + b, (-a*b^2 + b^3)/(2*a^2 + 2*a*b), sqrt(-a + b)*sqrt(a + b)), b^2/(2*a))

   (0.8333333333333334, 2, 1.0416666666666667, 4.0, 4.166666666666667)

draw(4, 7, false)

   y1 = 0.87500;  r1 = 3.00000;  r2 = 1.67045;  x = 5.74456;  R = 6.12500

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=:blue, 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(a, b, more)
    pyplot(size=(500, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
   (y1, r1, r2, x, R) = (b*(2*a - b)/(2*a), -a + b, (-a*b^2 + b^3)/(2*a^2 + 2*a*b), sqrt(-a + b)*sqrt(a + b), b^2/(2*a))
   @printf("y1 = %.5f;  r1 = %.5f;  r2 = %.5f;  x = %.5f;  R = %.5f\n", y1, r1, r2, x, R)
   plot()
   circle(0, 0, R, :black)
   circle(r1, y1, r1)
   circle(-r1, y1, r1)
   circle(0, R - r2, r2, :blue)
   if more == true
       point(r1, y1, "  (r1,y1)  地") 
       point(0, R - r2, " R-r2  天") 
       point(0, -R, " -R")
       point(0, a - R, " a-R")
       point(x, a - R, "(x,a-R)", :blue, :right, :bottom)
       point(0, (a - 2R)/2, "a", :green, mark=false)
       point(x/2, (a - 2R)/2, "b", :green, mark=false)
       hline!([0], color=:black, lw=0.5)
       vline!([0], color=:black, lw=0.5)
   else
       plot!(showaxis=false)
   end
   plot!([0, x, -x, 0, 0], [-R, a - R, a - R, -R, a - R], color=:magenta, lw=0.5)
end;

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

算額(その209)

2023年04月20日 | Julia

算額(その209)

中村信弥「改訂増補 長野県の算額」
http://www.wasan.jp/zoho/zoho.html
県内の算額2(123)
長野県千曲市八幡 八幡社 文政13年(1830)
長野県長野市安茂里正覚院 久保寺観音堂 文政13年(1830)

台形内に1本の対角線を引き,全円と小円を入れる。全円の径が 6 寸 5 分,上底の長さが 5寸のとき,小円の径はいかほどか。

方程式中の定数が整数になるように20倍して考える。座標軸を図のように定め,x が台形の右下の座標,小円の半径を r1,中心座標を (x1, 130 - r1) とし,連立方程式を解く。

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 x::positive, x1::negative, r1::positive;
#@syms x, x1, r1
eq1 = distance(-x, 0, -50, 130, x1, 130 - r1) - r1^2
eq2 = distance(-x, 0, 50, 130, x1, 130 - r1) - r1^2
eq3 = distance(-x, 0, -50, 130, 0, 65) - 65^2
res = solve([eq1, eq2, eq3], (x, x1, r1))

   2-element Vector{Tuple{Sym, Sym, Sym}}:
    (169/2, -sqrt(139961)/4 + 72361*sqrt(212322 - 538*sqrt(139961))/270400 + 269*sqrt(29716799442 - 75299018*sqrt(139961))/270400, -269*sqrt(212322 - 538*sqrt(139961))/1040 - 69*sqrt(139961)/1040 + 86161/1040)
    (169/2, -sqrt(139961)/4 - 269*sqrt(29716799442 - 75299018*sqrt(139961))/270400 - 72361*sqrt(212322 - 538*sqrt(139961))/270400, -69*sqrt(139961)/1040 + 269*sqrt(212322 - 538*sqrt(139961))/1040 + 86161/1040)

x1 は式が長いが,二重根号を外して簡単にできる。

-sqrt(Sym(139961))/4 + 72361*sqrt(212322 - 538*sqrt(Sym(139961)))/270400 + 269*sqrt(29716799442 - 75299018*sqrt(Sym(139961)))/270400 |> sympy.sqrtdenest |> println

   sqrt(139961)*(-1/4 + 269*sqrt(139961)/559844)

r1 も同じく,二重根号を外して簡単にできる。

-269*sqrt(212322 - 538*sqrt(Sym(139961)))/1040 - 69*sqrt(Sym(139961))/1040 + 86161//1040 |> sympy.sqrtdenest |> simplify |> println

   6097/40 - 13*sqrt(139961)/40

6097/40 - 13*sqrt(139961)/40

   30.838073790805964

求まるのは 20 倍した半径である。直径はこの数値の 1/10 である。元の単位でいえば 3 寸 0 分 8 厘 3 毛 8 糸あまりあり

   x = 84.50000,  x1 = -26.27840,  r1 = 30.83807

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=:blue, 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)
    pyplot(size=(500, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
   (x, x1, r1) = res[1]
   @printf("%.5f,  %.5f,  %.5f\n", x, x1, r1)
   plot([x, 50, -50, -x, x], [0, 130, 130, 0, 0], color=:black, lw=0.5)
   circle(0, 65, 65)
   circle(x1, 130 - r1, r1, :blue)
   segment(-x, 0, 50, 130, :magenta)
   if more == true
       point(x, 0, " x", :blue, :left, :bottom)
       point(0, 65, " 65")
       point(0, 130, " 130", :blue, :left, :bottom)
       point(x1, 130 - r1, "(x1,130-r1)", :blue, :center, :top)
       hline!([0], color=:black, lw=0.5)
       vline!([0], color=:black, lw=0.5)
   else
       plot!(showaxis=false)
   end
end;

 

 

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

算額(その208)

2023年04月19日 | Julia

算額(その208)

中村信弥「改訂増補 長野県の算額」
http://www.wasan.jp/zoho/zoho.html
県内の算額2(113)
長野県長野市信更町田野口 清水神社 文政11年(1828)

鉤が 60 寸,股が 80 寸の鉤股弦内に径が 40 寸の全円を入れる。全円と中斜と股に接する甲円,全円と中斜と鉤に接する乙円の径を求めよ。

甲円,乙円の半径を r1, r2 とする。中心座標は (x1, r1), (r2, y2) とする。
中斜と弦は点 A で垂直に交わる。A の座標は (48*3/5, 48*4/5) である。
甲円,乙円が全円と外接する条件式と甲円,乙円の中心から中斜までの距離に関する条件式の連立方程式を解く。

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 r1::positive, x1::positive, r2::positive, y2::positive;

eq1 = (20 - x1)^2 + (20 - r1)^2 - (20 + r1)^2
eq2 = (20 - r2)^2 + (20 - y2)^2 - (20 + r2)^2
eq3 = distance(0, 0, 48*3/5, 48*4/5, x1, r1) - r1^2
eq4 = distance(0, 0, 48*3/5, 48*4/5, r2, y2) - r2^2
res = solve([eq1, eq2, eq3, eq4], (r1, x1, r2, y2))

   4-element Vector{NTuple{4, Sym}}:
    ((20 - 20*sqrt(3))^2/80, 40 - 20*sqrt(3), 20/9, 20/3)
    ((20 - 20*sqrt(3))^2/80, 40 - 20*sqrt(3), 20, 60)
    ((20 + 20*sqrt(3))^2/80, 20*sqrt(3) + 40, 20/9, 20/3)
    ((20 + 20*sqrt(3))^2/80, 20*sqrt(3) + 40, 20, 60)

using Printf
for i in 1:4
   @printf("r1 = %.5f, x1 = %.5f, r2 = %.5f, y2 = %.5f\n", res[i][1], res[i][2], res[i][3], res[i][4])
end

   r1 = 2.67949, x1 = 5.35898, r2 = 2.22222, y2 = 6.66667
   r1 = 2.67949, x1 = 5.35898, r2 = 20.00000, y2 = 60.00000
   r1 = 37.32051, x1 = 74.64102, r2 = 2.22222, y2 = 6.66667
   r1 = 37.32051, x1 = 74.64102, r2 = 20.00000, y2 = 60.00000

最初の組が適切解である。「甲円,乙円の直径は 5寸3分5厘あまりあり,4寸4分4厘あまりあり

(20 - 20*sqrt(3))^2/80*2, 20/9*2

   (5.358983848622452, 4.444444444444445)

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=:blue, 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, x1, r2, y2) = res[1]
   @printf("r1 = %.5f, x1 = %.5f, r2 = %.5f, y2 = %.5f\n", r1, x1, r2, y2)
   plot([0, 80, 0, 0, 48*3/5], [0, 0, 60, 0, 48*4/5], color=1, lw=0.5)
   circle(20, 20, 20)
   circle(x1, r1, r1, :blue)
   circle(r2, y2, r2, :green)
   if more == true
       point(20, 20, "(20,20) 全円", :red)
       point(x1, r1, "  (x1,r1) 甲円", :blue)
       point(r2, y2, "    (r2,y2) 乙円", :green, :left, :bottom)
       point(48*3/5, 48*4/5, "  A (48*3/5, 48*4/5)", :orange, :left, :bottom)
       hline!([0], color=:black, lw=0.5)
       vline!([0], color=:black, lw=0.5)
   else
       plot!(showaxis=false)
   end
end;

 

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

算額(その207)

2023年04月19日 | Julia

算額(その207)

中村信弥「改訂増補 長野県の算額」
http://www.wasan.jp/zoho/zoho.html
県内の算額2(113)
長野県長野市信更町田野口 清水神社 文政11年(1828)

直線の上に甲円,2 個の正方形とその上に乙円がある。正方形の一辺の長さと乙円の直径は 10 寸である。甲円の直径を求めよ。

甲円の半径を r,正方形の位置(図参照) x として連立方程式を解く。

using SymPy

@syms x::positive, r::positive;

eq1 = x^2 + (r -10)^2 - r^2
eq2 = (x + 10)^2 + (r - 15)^2 - (r + 5)^2;
res = solve([eq1, eq2], (r, x))

   1-element Vector{Tuple{Sym, Sym}}:
    (10*sqrt(2) + 20, 10 + 10*sqrt(2))

using Printf
@printf("r = %.5f;  x = %.5f\n", 10*sqrt(2) + 20, 10 + 10*sqrt(2))

   r = 34.14214;  x = 24.14214

甲円の直径は 68.28428 寸である。

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=:blue, 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")
   (r, x) = res[1]
   @printf("r = %.5f;  x = %.5f\n", r, x)
   plot([x+10, x+10, x, x, x+20, x+20, x+10], [0, 10, 10, 0, 0, 10, 10], color=:green, lw=0.5)
   circle(0, r, r)
   circle(x + 10, 15, 5, :blue)
   hline!([0], color=:black, lw=0.5)
   if more == true
       point(0, r, " r", :red)
       point(15, 40, "甲円", :red, mark=false)
       point(40, 20, "乙円", :blue, mark=false) 
       point(x + 2, 6, "正方形", :green, mark=false)
       point(x + 12, 6, "正方形", :green, mark=false)
       point(x, 0, " x", :green, :left, :bottom)
       point(x + 10, 15, " (x+10,15)", :blue)
       vline!([0], color=:black, lw=0.5)
   else
       plot!(showaxis=false)
   end
end;

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

算額(その206)

2023年04月18日 | Julia

算額(その206)

中村信弥「改訂増補 長野県の算額」
http://www.wasan.jp/zoho/zoho.html
県内の算額2(112)
長野県長野市信更町田野口 清水神社 文政11年(1828)

鉤股弦の中に股,弦に内接し,たがいに外接する全円,天,地,人,末円の 5 円がある。
全円と末円の径がそれぞれ 50 寸,20寸4分8厘のとき,鉤,股を求めよ。

全円,天,地,人,末円の半径をそれぞれ r1, r2, r3, r4, r5 とおく。また,それぞれの円の中心座標の x 座標を x1, x2, x3, x4, x5 とおく。鉤,股 の長さを y, x とおく。r1 = 25, r5 = 256/25

以下の方程式を立て,nlsolve() で数値解を求める。

using SymPy

@syms x::positive, y::positive,
     x1::positive, x2::positive, x3::positive, x4::positive, x5::positive,
     r1::positive, r2::positive, r3::positive, r4::positive, r5::positive;

r1 = 5000 // 200
r5 = 2048 // 200
x1 = r1

eq1 = r1*(x + y + sqrt(x^2 + y^2)) - x*y

eq2 = (x2 - x1)^2 + (r1 - r2)^2 - (r1 + r2)^2
eq3 = (x3 - x2)^2 + (r2 - r3)^2 - (r2 + r3)^2
eq4 = (x4 - x3)^2 + (r3 - r4)^2 - (r3 + r4)^2
eq5 = (x5 - x4)^2 + (r4 - r5)^2 - (r4 + r5)^2

eq6 = r2/(x - x2) - r1/(x - x1)
eq7 = r3/(x - x3) - r1/(x - x1)
eq8 = r4/(x - x4) - r1/(x - x1)
eq9 = r5/(x - x5) - r1/(x - x1);

println(eq1, ",")
println(eq2, ",")
println(eq3, ",")
println(eq4, ",")
println(eq5, ",")
println(eq6, ",")
println(eq7, ",")
println(eq8, ",")
println(eq9, ",")

   -x*y + 25*x + 25*y + 25*sqrt(x^2 + y^2),
   (25 - r2)^2 - (r2 + 25)^2 + (x2 - 25)^2,
   (r2 - r3)^2 - (r2 + r3)^2 + (-x2 + x3)^2,
   (r3 - r4)^2 - (r3 + r4)^2 + (-x3 + x4)^2,
   (r4 - 256/25)^2 - (r4 + 256/25)^2 + (-x4 + x5)^2,
   r2/(x - x2) - 25/(x - 25),
   r3/(x - x3) - 25/(x - 25),
   r4/(x - x4) - 25/(x - 25),
   256/(25*(x - x5)) - 25/(x - 25),

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)
   (x, y, x2, r2, x3, r3, x4, r4, x5) = u
   return [
       -x*y + 25*x + 25*y + 25*sqrt(x^2 + y^2),
       (25 - r2)^2 - (r2 + 25)^2 + (x2 - 25)^2,
       (r2 - r3)^2 - (r2 + r3)^2 + (-x2 + x3)^2,
       (r3 - r4)^2 - (r3 + r4)^2 + (-x3 + x4)^2,
       (r4 - 256/25)^2 - (r4 + 256/25)^2 + (-x4 + x5)^2,
       r2/(x - x2) - 25/(x - 25),
       r3/(x - x3) - 25/(x - 25),
       r4/(x - x4) - 25/(x - 25),
       256/(25*(x - x5)) - 25/(x - 25),
   ]
end;

iniv = [big"250.0",  # x
       55,  # y
       70, 20,  # X2, r2
       105, 16,  # X3, r3
       135, 12,  # X4, r4
       160]  # x5
res = nls(H, ini=iniv)
println(res)

   (BigFloat[248.606797749978970222839406536185466507091524179219182152196301535957046927481, 56.29384298101212579525085037666427268874869468223197454780736127827494751538851, 69.72135954999579394111525781066703916824107934716963587235513853765339786871819, 20.00000000000000001156648090736958850104539280236562242960299990500708203173324, 105.4984471899924291146979835493254982419429730073781057366671977198094813095843, 16.00000000000000001850612795056016606515371799639254621341461233340910418765029, 134.1201173019897372701167677264383116164631130351796330083731146721806074972733, 12.80000000000000002220710131319660797319040184322561478177989870880112933552403, 157.0174533915875840195588180120496500402444605280433804432470134951785287502897], true)

x = 248.60680;  y = 56.29384
x1 = 25.00000;  r1 = 25.00000
x2 = 69.72136;  r2 = 20.00000
x3 = 105.49845;  r3 = 16.00000
x4 = 134.12012;  r4 = 12.80000
x5 = 157.01745;  r5 = 10.24000
鉤 = 2.48607, 股 = 0.56294

鉤は約2寸4分8厘6毛,股は約5分6厘3毛である。
なお,全円から n 番目の円の半径は 25×0.8^n である。末円は 4 番目なので,半径は 25*0.8^4 = 10.24 である。
また,半径が求まれば,その円の中心x座標は r_i*(1-x/25)+x ただし,x は股の長さである。末円の中心x座標は 10.24*(1 - 248.60680/25) + 248.60680 ≒ 157.01745472 である。

なお,よくあることであるが,算額の図はアスペクト比が実際のものと大きく異なっている。

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=:blue, 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)
    pyplot(size=(500, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
   r1 = 5000 // 200
   r5 = 2048 // 200
   x1 = r1
   (x, y, x2, r2, x3, r3, x4, r4, x5) = res[1]
   @printf("x = %.5f;  y = %.5f;  x1 = %.5f;  r1 = %.5f;  x2 = %.5f;  r2 = %.5f;  x3 = %.5f;  r3 = %.5f;  x4 = %.5f;  r4 = %.5f;  x5 = %.5f;  r5 = %.5f\n",
           x, y, x1, r1, x2, r2, x3, r3, x4, r4, x5, r5)
   @printf("鉤 = %.5f, 股 = %.5f\n", x/100, y/100)
   plot([0, x, 0, 0], [0, 0, y, 0], color=:black, lw=0.5)
   circle(x1, r1, r1)
   circle(x2, r2, r2, :blue)
   circle(x3, r3, r3, :orange)
   circle(x4, r4, r4, :brown)
   circle(x5, r5, r5, :magenta)
   if more == true
       point(x1, r1, "全円", :red)
       point(x2, r2, "天", :blue)
       point(x3, r3, "地", :orange)
       point(x4, r4, "人", :brown)
       point(x5, r5, "末円", :magenta)
       point(0, y, " y", :black, :left, :bottom)
       point(x, 0, " x", :black, :left, :bottom)
       hline!([0], color=:black, lw=0.5)
       vline!([0], color=:black, lw=0.5)
   else
       plot!(showaxis=false)
   end
end;

漸化式を用いてたくさんの円を描く。

function draw2()
    pyplot(size=(2000, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
   r1 = 5000 // 200
   x1 = r1
   (x, y, x2, r2, x3, r3, x4, r4, x5) = res[1]
   plot([0, x, 0, 0], [0, 0, y, 0], color=:black, lw=0.5)
   circle(x1, r1, r1)
   for i in 1:20
       ri = 25*0.8^i
       xi = ri*(1 - x/25) + x
       circle(xi, ri, ri)
       point(xi, ri, i, mark=false)
   end
   plot!(showaxis=false)
end;

draw2()

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

算額(その205)

2023年04月18日 | Julia

算額(その205)

中村信弥「改訂増補 長野県の算額」
http://www.wasan.jp/zoho/zoho.html
県内の算額2(111)
長野県伊那市羽広 文政9年(1826)

鉤股弦の中に鉤,股,弦に内接し,たがいに外接する子,丑,寅,卯の 4 円と 4 円と股に接する初円,二円,終円がある。
初円と終円の径がそれぞれ 4 寸,5 分のとき,二円の径を求めよ。

子,丑,寅,卯,初円,二円,終円の半径をそれぞれ R1, R2, R3, R4, r1, r2, r3 とおく。径が整数値になるように分を単位として,r1 = 40, r3 = 5 とする。また,それぞれの円の中心座標の x 座標を X1, X2, X3, X4, x1, x2, x3 とおく(X1 = R1).

以下の方程式を立て,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 x::positive, y::positive,
     X1::positive, X2::positive, X3::positive, X4::positive,
     R1::positive, R2::positive, R3::positive, R4::positive,
     x1::positive, x2::positive, x3::positive,
     r1::positive, r2::positive, r3::positive;

X1 = R1
r1 = 40
r3 = 5
eq1 = R1*(x + y + sqrt(x^2 + y^2)) - x*y
eq2 = (X2 - X1)^2 + (R1 - R2)^2 - (R1 + R2)^2
eq3 = (X2 - x1)^2 + (R2 - r1)^2 - (R2 + r1)^2
eq4 = (x1 - X1)^2 + (R1 - r1)^2 - (R1 + r1)^2

eq5 = (X3 - X2)^2 + (R2 - R3)^2 - (R2 + R3)^2
eq6 = (X3 - x2)^2 + (R3 - r2)^2 - (R3 + r2)^2
eq7 = (x2 - X2)^2 + (R2 - r2)^2 - (R2 + r2)^2

eq8 = (X4 - X3)^2 + (R3 - R4)^2 - (R3 + R4)^2
eq9 = (X4 - x3)^2 + (R4 - r3)^2 - (R4 + r3)^2
eq10 = (x3 - X3)^2 + (R3 - r3)^2 - (R3 +r3)^2

eq11 = R2/(x - X2) - R1/(x - X1)
eq12 = R3/(x - X3) - R1/(x - X1)
eq13 = R4/(x - X4) - R1/(x - X1);

println(eq1, ",")
println(eq2, ",")
println(eq3, ",")
println(eq4, ",")
println(eq5, ",")
println(eq6, ",")
println(eq7, ",")
println(eq8, ",")
println(eq9, ",")
println(eq10, ",")
println(eq11, ",")
println(eq12, ",")
println(eq13, ",")

   R1*(x + y + sqrt(x^2 + y^2)) - x*y,
   (-R1 + X2)^2 + (R1 - R2)^2 - (R1 + R2)^2,
   (R2 - 40)^2 - (R2 + 40)^2 + (X2 - x1)^2,
   (-R1 + x1)^2 + (R1 - 40)^2 - (R1 + 40)^2,
   (R2 - R3)^2 - (R2 + R3)^2 + (-X2 + X3)^2,
   (R3 - r2)^2 - (R3 + r2)^2 + (X3 - x2)^2,
   (R2 - r2)^2 - (R2 + r2)^2 + (-X2 + x2)^2,
   (R3 - R4)^2 - (R3 + R4)^2 + (-X3 + X4)^2,
   (R4 - 5)^2 - (R4 + 5)^2 + (X4 - x3)^2,
   (R3 - 5)^2 - (R3 + 5)^2 + (-X3 + x3)^2,
   -R1/(-R1 + x) + R2/(-X2 + x),
   -R1/(-R1 + x) + R3/(-X3 + x),
   -R1/(-R1 + x) + R4/(-X4 + x),

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)
   (x, y, X2, X3, X4, R1, R2, R3, R4, x1, x2, x3, r2) = u
   return [
       R1*(x + y + sqrt(x^2 + y^2)) - x*y,
       (-R1 + X2)^2 + (R1 - R2)^2 - (R1 + R2)^2,
       (R2 - 40)^2 - (R2 + 40)^2 + (X2 - x1)^2,
       (-R1 + x1)^2 + (R1 - 40)^2 - (R1 + 40)^2,
       (R2 - R3)^2 - (R2 + R3)^2 + (-X2 + X3)^2,
       (R3 - r2)^2 - (R3 + r2)^2 + (X3 - x2)^2,
       (R2 - r2)^2 - (R2 + r2)^2 + (-X2 + x2)^2,
       (R3 - R4)^2 - (R3 + R4)^2 + (-X3 + X4)^2,
       (R4 - 5)^2 - (R4 + 5)^2 + (X4 - x3)^2,
       (R3 - 5)^2 - (R3 + 5)^2 + (-X3 + x3)^2,
       -R1/(-R1 + x) + R2/(-X2 + x),
       -R1/(-R1 + x) + R3/(-X3 + x),
       -R1/(-R1 + x) + R4/(-X4 + x),
   ]
end;

iniv = [big"833.000",1053.000,617.000,740.000,790.000,273.000,107.000,40.000,13.000,483.000,700.000,767.000,15.000]
res = nls(H, ini=iniv)
println(res)
   (BigFloat[816.8993302199391936863183540594148369505171487651418982079256420562895530123538, 1260.635980376047836523365316686083957150213669568045877012075569884013238695705, 629.792222471145100342837737942010864940953010573889950734632525355653451117743, 750.7469778712520358242055534024832109465659037439265863008270430689866716736023, 793.510941751339932018412978220965754687756296707953499140668753072776774066944, 287.6805114304419307891495187259128409297740103861724094435838059794655101858596, 101.7104202238397931567089553334551855237791286606845268770946909703629116545619, 35.96006392880524134862875986686960078608960052891263144000779136274804853500832, 12.71380252797997414459150127469079570614480096717487185970557120337723627659082, 502.2239378710362576741426542375178843787306201784478572695275704907133737711138, 705.6447783187158439875606652338967930483915626990290230026993160508635031873259, 777.5649061763263266848242507889009904417376056559312443132730383063044133019538, 14.1421356237309504880139126050091908511062132238608639715198083480294343305563], true)

x  = 816.899;  y  = 1260.636
X1 = 287.681;  R1 = 287.681
X2 = 629.792;  R2 = 101.710
X3 = 750.747;  R3 = 35.960
X4 = 793.511;  R4 = 12.714
x1 = 502.224;  r1 = 40.000
x2 = 705.645;  r2 = 14.142
x3 = 777.565;  r3 = 5.000

二円の半径は 10√2 = 14.1421356 である。元の単位でいえば,「二円の径は 1寸4分1厘4毛2糸あまりあり」である。

なお,よくあることであるが,算額の図はアスペクト比が実際のものと大きく異なっている。


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=:blue, 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(r1, more)
    pyplot(size=(500, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
   r1 = 40
   r3 = 5
   (x, y, X2, X3, X4, R1, R2, R3, R4, x1, x2, x3, r2) = res[1]
   X1 = R1
   @printf("x = %.3f;  y = %.3f;  X1 = %.3f;  R1 = %.3f;  X2 = %.3f;  R2 = %.3f;  X3 = %.3f;  R3 = %.3f;  X4 = %.3f;  R4 = %.3f;  x1 = %.3f;  r1 = %.3f;  x2 = %.3f;  r2 = %.3f;  x3 = %.3f;  r3 = %.3f\n",
           x, y, X1, R1, X2, R2, X3, R3, X4, R4, x1, r1, x2, r2, x3, r3)
   plot([0, x, 0, 0], [0, 0, y, 0], color=:black, lw=0.5)
   circle(X1, R1, R1)
   circle(X2, R2, R2, :blue)
   circle(x1, r1, r1, :green)
   circle(X3, R3, R3, :orange)
   circle(x2, r2, r2, :black)
   circle(x3, r3, r3, :magenta)
   circle(X4, R4, R4, :brown)
   plot!(xlims=(400,850), ylims=(-10, 210))
   if more == true
       point(X1, R1, "子", :red)
       point(X2, R2, "丑", :blue)
       point(X3, R3, "寅", :orange)
       point(X4, R4, "卯", :brown)
       point(x1, 0, "初円", :green)
       point(x2, 0, "二円", :black)
       point(x3, 0, "終円", :magenta)
       point(0, y, " y")
       point(x, 0, " x")
       hline!([0], color=:black, lw=0.5)
       vline!([0], color=:black, lw=0.5)
   else
       plot!(showaxis=false)
   end
end;

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

算額(その204)

2023年04月17日 | Julia

算額(その204)

中村信弥「改訂増補 長野県の算額」
http://www.wasan.jp/zoho/zoho.html
県内の算額2(110)
長野県伊那市羽広 仲仙寺 文政9年(1826)

正方形に 4 本の斜線を引き,区分された領域に大円,中円,小円を配置する。大円の径が 1 寸のとき,小円の径はいかほどか。

正方形の一辺の長さを 4a,大円,中円,小円の半径をそれぞれ r1, r2, r3 とおく。
各円の中心から斜線までの距離に関する方程式を立て,r について解く。

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 r1::positive, r2::positive, r3::positive, a::positive;

eq1 = distance(0, 2a, 2a, -2a, 0,  0) - r1^2
eq2 = distance(0, 2a, 2a, -2a, a, 2a - r2) - r2^2
eq3 = distance(0, 2a, 2a, -2a, 2a - r3, 0) - r3^2
res = solve([eq1, eq2, eq3], (r2, r3, a))

   1-element Vector{Tuple{Sym, Sym, Sym}}:
    (-sqrt(5)*r1/4 + 5*r1/4, -2*sqrt(5)*r1 + 5*r1, sqrt(5)*r1/2)

小円の径は「大円の径 × (5 - 2√5)/4」である。
ちなみに,小円の径は「大円の径 × (5 - √5)/4」である。

r1 = 1 のとき,

   r1 = 1.000;  r2 = 0.691;  r3 = 0.528;  a = 1.118

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=:blue, 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(r1, more)
    pyplot(size=(500, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
   (r2, r3, a) = (-sqrt(5)*r1/4 + 5*r1/4, -2*sqrt(5)*r1 + 5*r1, sqrt(5)*r1/2)
   @printf("r1 = %.3f;  r2 = %.3f;  r3 = %.3f;  a = %.3f\n", r1, r2, r3, a)
   plot([2a, 2a, -2a, -2a, 2a], [-2a, 2a, 2a, -2a, -2a], color=:black, lw=0.5)
   circle(0, 0, r1)
   circle(a, 2a - r2, r2, :blue)
   circle(a, r2 - 2a, r2, :blue)
   circle(-a, 2a - r2, r2, :blue)
   circle(-a, r2 - 2a, r2, :blue)
   circle(2a - r3, 0, r3, :green)
   circle(r3 - 2a, 0, r3, :green)
   plot!([-2a, 0, 2a], [2a, -2a, 2a], color=:black, lw=0.5)
   plot!([-2a, 0, 2a], [-2a, 2a, -2a], color=:black, lw=0.5)
   if more == true
       point(r1, 0, "r1 ", :red, :right)
       point(a, 2a - r2, "(a,2a-r2)", :blue)
       point(2a-r3, 0, "2a-r3", :green, :center)
       point(2a, 0, "a ", :black, :right)
       point(0, 2a, " a", :black)
       hline!([0], color=:black, lw=0.5)
       vline!([0], color=:black, lw=0.5)
   else
       plot!(showaxis=false)
   end
end;

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

算額(その203)

2023年04月17日 | Julia

算額(その203)

中村信弥「改訂増補 長野県の算額」
http://www.wasan.jp/zoho/zoho.html
県内の算額2(108)
長野県佐久市広川原 禅昌寺 文政9年(1826) もしくは 文政11年(1828)

大円と中円 1 個が交わり,もう一つの中円が大円に内接している。更に, 4 個の小円が配置されている。大円の径を知って中円の径を求めよ。

大円,中円,小円の半径をそれぞれ r1, r2, r3 とおく。右上小円の中心座標の y 座標を y3,左の小円の中心座標の x 座標を x3 とおく。
方程式を立て,r について解く。

using SymPy

@syms r1::positive, r2::positive, r3::positive, x3::positive, y3::positive;

eq1 = r3^2 + (y3 - r2)^2 - (r2 -r3)^2
eq2 = r3^2 + (y3 - r1 + 2r2)^2 - (r1 + r3)^2
eq3 = x3^2 + r2^2 - (r2 + r3)^2
eq4 = x3^2 + (r1 - 2r2)^2 - (r1 - r3)^2
res = solve([eq1, eq2, eq3, eq4], (r2, r3, x3, y3))

   1-element Vector{NTuple{4, Sym}}:
    (-5*r1 + 4*sqrt(2)*r1, r1*(13 - 9*sqrt(2)), r1*sqrt(57 - 40*sqrt(2)), r1*(8 - 5*sqrt(2)))

中円の径は「(4√2 - 5) × 大円の径」である。

   r1 = 1.000;  r2 = 0.657;  r3 = 0.272;  x3 = 0.657;  y3 = 0.929

ちなみに,小円の径は「(13 - 9√2) × 大円の径」である。

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=:blue, 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(r1, more)
    pyplot(size=(500, 500), grid=false, aspectratio=1, label="", fontfamily="IPAMincho")
   (r2, r3, x3, y3) = (-5*r1 + 4*sqrt(2)*r1, r1*(13 - 9*sqrt(2)), r1*sqrt(57 - 40*sqrt(2)), r1*(8 - 5*sqrt(2)))
   @printf("r1 = %.3f;  r2 = %.3f;  r3 = %.3f;  x3 = %.3f;  y3 = %.3f\n", r1, r2, r3, x3, y3)
   plot()
   circle(0, r1 - 2r2, r1)
   circle(0, r2, r2, :blue)
   circle(0, -r2, r2, :blue)
   circle(r3, y3, r3, :green)
   circle(-r3, y3, r3, :green)
   circle(x3, 0, r3, :green)
   circle(-x3, 0, r3, :green)
   if more == true
       point(r3, y3, "(r3,y3)", :green)
       point(x3, 0, " x3", :green)
       point(0, r1 - 2r2, " r1-2r2", :red)
       point(0, r2, " r2", :blue)
       point(0, -r2, " -r2", :blue)
       hline!([0], color=:black, lw=0.5)
       vline!([0], color=:black, lw=0.5)
   else
       plot!(showaxis=false)
   end
end;

 

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

PVアクセスランキング にほんブログ村

PVアクセスランキング にほんブログ村