{"id":5276,"date":"2026-04-28T14:10:00","date_gmt":"2026-04-28T05:10:00","guid":{"rendered":"https:\/\/www.ktech.biz\/jp\/?p=5276"},"modified":"2026-04-28T14:10:00","modified_gmt":"2026-04-28T05:10:00","slug":"xlpack-py-mpl-ex3","status":"publish","type":"post","link":"https:\/\/www.ktech.biz\/jp\/manual2\/xlpack-py-mpl-ex3\/","title":{"rendered":"XLPack for Python \u304a\u3088\u3073 XLPack for Matplotlib \u306e\u4f7f\u7528\u4f8b (3) \u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3 (\u30a2\u30ec\u30f3\u30b9\u30c8\u30fc\u30d5\u8ecc\u9053)"},"content":{"rendered":"\n\u8cea\u91cf\u304c \\(\\mu\\) \u3068 \\(\\mu&#8217; (= 1 &#8211; \\mu)\\) \u306e2\u3064\u306e\u5929\u4f53\u304c\u5e73\u9762\u5185\u56de\u8ee2\u904b\u52d5\u3092\u3057\u3066\u304a\u308a, \u305d\u306e2\u3064\u306b\u6bd4\u3079\u3066\u8cea\u91cf\u304c\u7121\u8996\u3067\u304d\u308b\u7b2c3\u306e\u7269\u4f53\u304c\u540c\u4e00\u5e73\u9762\u5185\u3092\u904b\u52d5\u3059\u308b\u3082\u306e\u3068\u3057\u307e\u3059. \u305d\u306e\u65b9\u7a0b\u5f0f\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059 (\u53c2\u8003\u6587\u732e: E. Hairer, \u4ed6\u300c\u5e38\u5fae\u5206\u65b9\u7a0b\u5f0f\u306e\u6570\u5024\u89e3\u6cd5 I\u300d(2007) \u30b7\u30e5\u30d7\u30ea\u30f3\u30ac\u30fc\u30fb\u30b8\u30e3\u30d1\u30f3).<br \/>\n\\[<br \/>\n\\begin{align}<br \/>\n&#038; y_1&#8221; = y_1 + 2y_2&#8242; &#8211; \\mu'(y_1 + \\mu)\/D_1 &#8211; \\mu(y_1 &#8211; \\mu&#8217;)\/D_2 \\\\<br \/>\n&#038; y_2&#8221; = y_2 &#8211; 2y_1&#8242; &#8211; \\mu&#8217;y_2\/D_1 &#8211; \\mu y_2\/D_2 \\\\<br \/>\n&#038; \u305f\u3060\u3057, \\\\<br \/>\n&#038; D_1 = ((y_1 + \\mu)^2 + y_2^2)^{3\/2} \\\\<br \/>\n&#038; D_2 = ((y_1 &#8211; \\mu&#8217;)^2 + y_2^2)^{3\/2} \\\\<br \/>\n&#038; \\mu = 0.012277471, \\mu&#8217; = 1 &#8211; \\mu \\\\<br \/>\n\\end{align}<br \/>\n\\]\n\u6b21\u306e\u521d\u671f\u5024\u3092\u7528\u3044\u308b\u3068, \\(t = 17.0652165601579625588917206249\\) \u3067\u5468\u671f\u89e3\u3068\u306a\u308b\u3053\u3068\u304c\u77e5\u3089\u308c\u3066\u3044\u307e\u3059.<br \/>\n\\[<br \/>\n\\begin{align}<br \/>\n&#038; y_1(0) = 0.994, y_1&#8242;(0) = 0 \\\\<br \/>\n&#038; y_2(0) = 0, y_2&#8242;(0) = -2.00158510637908252240537862224 \\\\<br \/>\n\\end{align}<br \/>\n\\]\n\u3053\u308c\u306f\u30a2\u30ec\u30f3\u30b9\u30c8\u30fc\u30d5\u8ecc\u9053\u3068\u3088\u3070\u308c\u3066\u3044\u307e\u3059.<\/p>\n<p>\u3053\u3053\u3067\u306f, 1\u5468\u671f\u5206\u306e\u6570\u5024\u89e3\u3092\u6c42\u3081\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3067\u8868\u793a\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3059.<\/p>\n<h2>1. \u5168\u4f53\u306e\u69cb\u6210<\/h2>\n<p>\u30e1\u30a4\u30f3\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-vb\" data-lang=\"VBA\"><code>Const Tend = 17.065216560158\r\n\r\nSub Main()\r\n    Dim Xt() As Double, Yt() As Double\r\n    Dim Dt As Double, Nt As Long, Info As Long\r\n    Dt = 0.1\r\n    Nt = Int(Tend \/ Dt) + 1\r\n    ReDim Xt(Nt), Yt(Nt)\r\n    Call ODE(Tend, Dt, Nt, Xt(), Yt(), Info)\r\n    Call Animate(Nt, Xt(), Yt(), Dt)\r\nEnd Sub<\/code><\/pre>\n<\/div>\n<p>\u30b5\u30d6\u30eb\u30fc\u30c1\u30f3 ODE() \u306f\u5fae\u5206\u65b9\u7a0b\u5f0f\u306e\u89e3\u3092\u6c42\u3081\u307e\u3059. \u5f97\u3089\u308c\u305f\u89e3\u306f\u30b5\u30d6\u30eb\u30fc\u30c1\u30f3 Animate() \u306b\u3088\u308a\u8868\u793a\u3055\u308c\u307e\u3059. Animate() \u3067\u306f XLPack for Python \u307e\u305f\u306f XLPack for Matplotlib \u3092\u4f7f\u7528\u3057\u3066\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3067\u8868\u793a\u3092\u884c\u3044\u307e\u3059.<\/p>\n<p>\u5b9f\u884c\u7d50\u679c\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059.<\/p>\n<p><video width=\"640\" height=\"480\" controls><source src=\"\/jp\/wp-content\/uploads\/sites\/2\/2026\/04\/Example_Aren.mp4\" type=\"video\/mp4\">Your browser does not support the video tag.<\/video><\/p>\n<h2>2. \u5fae\u5206\u65b9\u7a0b\u5f0f\u306e\u89e3<\/h2>\n<p>\u3053\u306e\u5fae\u5206\u65b9\u7a0b\u5f0f\u306f 2 \u968e\u5e38\u5fae\u5206\u65b9\u7a0b\u5f0f\u306a\u306e\u3067, 1 \u968e\u306e\u9023\u7acb\u5e38\u5fae\u5206\u65b9\u7a0b\u5f0f\u306e\u5f62\u306b\u5909\u6570\u5909\u63db\u3057\u3066 4 \u5143\u306e\u65b9\u7a0b\u5f0f\u3068\u3057\u3066\u89e3\u304d\u307e\u3059.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-vb\" data-lang=\"VBA\"><code>Const Xinit = 0.994\r\n\r\nSub F(N As Long, T As Double, Y() As Double, Yp() As Double)\r\n    Const Mu = 0.012277471, Mup = 1 - Mu\r\n    Dim D1 As Double, D2 As Double\r\n    Yp(0) = Y(2)\r\n    Yp(1) = Y(3)\r\n    D1 = ((Y(0) + Mu) ^ 2 + Y(1) ^ 2) ^ (3# \/ 2#)\r\n    D2 = ((Y(0) - Mup) ^ 2 + Y(1) ^ 2) ^ (3# \/ 2#)\r\n    Yp(2) = Y(0) + 2 * Y(3) - Mup * (Y(0) + Mu) \/ D1 - Mu * (Y(0) - Mup) \/ D2\r\n    Yp(3) = Y(1) - 2 * Y(2) - Mup * Y(1) \/ D1 - Mu * Y(1) \/ D2\r\nEnd Sub\r\n\r\nSub ODE(Tend As Double, Dt As Double, Nt As Long, Xt() As Double, Yt() As Double, Info As Long)\r\n    Const N = 4, Mode = 3, Tol = 0.0000000001 '1.0e-10\r\n    Dim Y(N - 1) As Double, T As Double, Tout As Double\r\n    Dim RTol(0) As Double, ATol(0) As Double, I As Integer\r\n    RTol(0) = Tol\r\n    ATol(0) = Tol\r\n    T = 0\r\n    Y(0) = Xinit: Y(1) = 0: Y(2) = 0: Y(3) = -2.00158510637908\r\n    Info = 0\r\n    Xt(0) = Y(0)\r\n    Yt(0) = Y(1)\r\n    For I = 0 To Nt - 1\r\n        Tout = (I + 1) * Dt\r\n        If Tout > Tend Then Tout = Tend\r\n        Call Derkfa(N, AddressOf F, T, Y(), Tout, Tend, RTol(), ATol(), Mode, Info)\r\n        If Info < 0 Or Info > 10 Then Exit Sub\r\n        Xt(I + 1) = Y(0)\r\n        Yt(I + 1) = Y(1)\r\n    Next\r\nEnd Sub<\/code><\/pre>\n<\/div>\n<p>\u5f97\u3089\u308c\u305f\u89e3\u306f, Xt(), Yt() \u306b\u4fdd\u5b58\u3057\u307e\u3059.<\/p>\n<h2>3. \u89e3\u306e\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u8868\u793a<\/h2>\n<p>\u5f97\u3089\u308c\u305f\u89e3\u3092 \u8868\u793a\u3059\u308b Python \u306e\u30b3\u30fc\u30c9\u3092 XLPack for Python \u3092\u4f7f\u3063\u3066\u76f4\u63a5\u8a18\u8ff0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059.<\/p>\n<p>\u914d\u5217\u30c7\u30fc\u30bf\u306f\u305d\u306e\u307e\u307e\u6e21\u3059\u3053\u3068\u304c\u3067\u304d\u306a\u3044\u306e\u3067, \u3044\u3063\u305f\u3093\u30ef\u30fc\u30af\u30b7\u30fc\u30c8\u306b\u66f8\u304d\u51fa\u3057\u3066\u304b\u3089 Python \u5074\u3067 xlc_get() \u3092\u4f7f\u3063\u3066\u8aad\u307f\u8fbc\u3093\u3067\u3044\u307e\u3059.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-vb\" data-lang=\"VBA\"><code>Sub Animate(Nt As Long, Xt() As Double, Yt() As Double, Dt As Double)\r\n    Const Col = 2, Row = 6\r\n    Dim I As Long\r\n    For I = 0 To Nt\r\n        Cells(Row + I, Col) = Xt(I)\r\n        Cells(Row + I, Col + 1) = Yt(I)\r\n    Next\r\n    '-- Python codes\r\n    PY \"import matplotlib.pyplot as plt\"\r\n    PY \"import matplotlib.animation as animation\"\r\n    PY \"from XLPackPy import *\"\r\n    PY \"fig = plt.figure()\"\r\n    PY \"ax = fig.gca()\"\r\n    PY \"ax.grid()\"\r\n    PY \"ax.plot(0, 0, '-o', color = 'orange')\"\r\n    PY \"ax.plot(1, 0, '-o', color = 'yellow')\"\r\n    PY \"ax.set_xlim(-1.5, 1.5)\"\r\n    PY \"ax.set_ylim(-1.5, 1.5)\"\r\n    PY \"xt = xlc_get(5, 1,\" + Str(5 + Nt) + \")\"\r\n    PY \"yt = xlc_get(5, 2,\" + Str(5 + Nt) + \")\"\r\n    PY \"xtrace = []; ytrace = []; artist = []\"\r\n    PY \"for i in range(len(xt)):\" + vbNewLine + _\r\n        \"  a_x = [xt[i]]; a_y = [yt[i]]\" + vbNewLine + _\r\n        \"  a, = ax.plot(a_x, a_y, 'o-', color = 'b')\" + vbNewLine + _\r\n        \"  xtrace.append(xt[i]); ytrace.append(yt[i])\" + vbNewLine + _\r\n        \"  trace, = ax.plot(xtrace, ytrace, ':c', lw=1)\" + vbNewLine + _\r\n        \"  artist.append((a, trace))\"\r\n    PY \"ani = animation.ArtistAnimation(fig, artist, interval = 100, blit = True, repeat = False)\"\r\n    PY \"plt.show()\"\r\nEnd Sub<\/code><\/pre>\n<\/div>\n<p>XLPack for Matplotlib \u3092\u4f7f\u3046\u3068\u540c\u3058\u51e6\u7406\u3092\u5168\u3066 VBA \u3067\u8a18\u8ff0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059. \u5185\u90e8\u7684\u306b\u306f Python \u3092\u547c\u3073\u51fa\u3057\u3066\u51e6\u7406\u3057\u307e\u3059.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-vb\" data-lang=\"VBA\"><code>Dim Plt As New PyPlot\r\nDim Animation As New Animation\r\n\r\nSub Animate(Nt As Long, Xt() As Double, Yt() As Double, Dt As Double)\r\n    Dim Ex(0) As Double, Ey(0) As Double, Mx(0) As Double, My(0) As Double\r\n    Dim X(0) As Double, Y(0) As Double, I As Long\r\n    Dim Fig As Figure, Ax As Axs, Artist() As PyObject\r\n    Dim Emarker As PyObject, Mmarker As PyObject\r\n    Dim Anim As ArtistAnimation\r\n    Ex(0) = 0: Ey(0) = 0\r\n    Mx(0) = Xinit: My(0) = 0\r\n    ReDim Artist(Nt, 1)\r\n    Set Fig = Plt.Figure()\r\n    Set Ax = Fig.Gca()\r\n    Call Ax.Grid\r\n    Call Ax.Set_xlim(-1.5, 1.5)\r\n    Call Ax.Set_ylim(-1.5, 1.5)\r\n    Set Emarker = Ax.Plot(1, Ex(), Ey(), \"\", \"color=orange,marker=o\")\r\n    Set Mmarker = Ax.Plot(1, Mx(), My(), \"\", \"color=yellow,marker=o\")\r\n    For I = 0 To Nt\r\n        X(0) = Xt(I): Y(0) = Yt(I)\r\n        Set Artist(I, 0) = Ax.Plot(1, X(), Y(), \"\", \"color=b,marker=o\")\r\n        Set Artist(I, 1) = Ax.Plot(I + 1, Xt(), Yt(), \":c\", \"lw=1\")\r\n    Next\r\n    Dim Ani As PyObject\r\n    Set Anim = Animation.ArtistAnimation(Fig, Nt + 1, 2, Artist(), \"interval =\" + Str(Dt * 1000) + \", repeat = False\")\r\n    Call Plt.Show\r\nEnd Sub<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u8cea\u91cf\u304c \\(\\mu\\) \u3068 \\(\\mu&#8217; (= 1 &#8211; \\mu)\\) \u306e2\u3064\u306e\u5929\u4f53\u304c\u5e73\u9762\u5185\u56de\u8ee2\u904b\u52d5\u3092\u3057\u3066\u304a\u308a, \u305d\u306e2\u3064\u306b\u6bd4\u3079\u3066\u8cea\u91cf\u304c\u7121\u8996\u3067\u304d\u308b\u7b2c3\u306e\u7269\u4f53\u304c\u540c\u4e00\u5e73\u9762\u5185\u3092\u904b\u52d5\u3059\u308b\u3082\u306e\u3068\u3057\u307e\u3059. \u305d\u306e [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[49],"tags":[],"class_list":["post-5276","post","type-post","status-publish","format-standard","hentry","category-manual2"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/posts\/5276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/comments?post=5276"}],"version-history":[{"count":13,"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/posts\/5276\/revisions"}],"predecessor-version":[{"id":5326,"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/posts\/5276\/revisions\/5326"}],"wp:attachment":[{"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/media?parent=5276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/categories?post=5276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ktech.biz\/jp\/wp-json\/wp\/v2\/tags?post=5276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}