<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>追逐问题 on 格物堂</title><link>https://gewutang.cn/tags/%E8%BF%BD%E9%80%90%E9%97%AE%E9%A2%98/</link><description>Recent content in 追逐问题 on 格物堂</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Mon, 29 Mar 2010 00:00:00 +0000</lastBuildDate><atom:link href="https://gewutang.cn/tags/%E8%BF%BD%E9%80%90%E9%97%AE%E9%A2%98/index.xml" rel="self" type="application/rss+xml"/><item><title>四龟追逐问题</title><link>https://gewutang.cn/2010/03/29/four-turtles/</link><pubDate>Mon, 29 Mar 2010 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2010/03/29/four-turtles/</guid><description>&lt;p&gt;四龟行走是一个很著名的论题，题目的大意是这样的：&lt;/p&gt;&#10;&#10; &lt;blockquote&gt;&#10; &lt;p&gt;在一个房间的四个角上，分别有一只乌龟。在某一个时刻t同时出发以匀速V走向顺时针方向的下一只乌龟，如果他们的方向始终对准目标，那么它们的运动轨迹是怎样的？&lt;/p&gt;&#10;&#10; &lt;/blockquote&gt;&#10;&lt;p&gt;这道题目的解答方法至少有三个：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;借助微分方程来解答，这是最正经的解法，但是也最没有意思，是&amp;quot;书虫&amp;quot;才干的出来的事情。&lt;/li&gt;&#10;&lt;li&gt;常理判断。由于四龟地位完全对称，而相邻两龟运动方向始终垂直，因此四龟轨迹对称且都是螺旋线。这是极其聪明的人才能干出的事情。&lt;/li&gt;&#10;&lt;li&gt;统计模拟方法。这个方法兼具艺术性和趣味性，而且不会远离群众，是有趣的人会干出的事情。&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;关于前两个这里一笔带过，第三个方法在薛毅很久之前的一本书《统计建模与R软件》是有提及过。怡轩说，这个统计模拟的例子于他有着特殊的意义。巧合的是，四龟行走也恰恰是我学习统计学的启蒙案例。也就是在见到这个例子的刹那，我才初次领悟了编程的景致之所在，也开始了对编程的敬仰之旅。&lt;/p&gt;&#10;&lt;p&gt;前段时间，在看自然图形时，突然发现自然界很多植物如“紫荆花”等等都有着类似四龟行走轨迹的花型。因此萌发妄想将它写成一个动画。我尽量避免使用复杂的数学语言来描述以上过程，因为我觉得数学语言尽管清晰有时候只会耽误大众对事情本质的理解。&lt;/p&gt;&#10;&lt;p&gt;下面是一段模拟四龟行走问题的R代码：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 加载必要的包&#10;library(ggplot2)&#10;library(gganimate)&#10;&#10;# 模拟参数设置&#10;V &amp;lt;- 0.02 # 移动速度&#10;dt &amp;lt;- 0.01 # 时间步长&#10;total_time &amp;lt;- 500 # 总模拟时间&#10;threshold &amp;lt;- 0.01 # 停止阈值&#10;&#10;# 初始化四只乌龟的起始位置（正方形顶点）&#10;turtles &amp;lt;- matrix(c(&#10; 0.0, 1.0, # 左上角乌龟1&#10; 1.0, 1.0, # 右上角乌龟2&#10; 1.0, 0.0, # 右下角乌龟3&#10; 0.0, 0.0 # 左下角乌龟4&#10;), ncol = 2, byrow = TRUE)&#10;&#10;# 轨迹记录矩阵&#10;history &amp;lt;- data.frame(&#10; turtle = integer(),&#10; time = numeric(),&#10; x = numeric(),&#10; y = numeric()&#10;)&#10;&#10;# 主循环模拟&#10;current_time &amp;lt;- 0&#10;while(current_time &amp;lt; total_time) {&#10; # 计算各乌龟的目标方向&#10; new_positions &amp;lt;- matrix(nrow = 4, ncol = 2)&#10; &#10; for(i in 1:4) {&#10; current_pos &amp;lt;- turtles[i, ]&#10; target_pos &amp;lt;- turtles[(i %% 4) + 1, ]&#10; &#10; # 计算方向向量并标准化&#10; direction &amp;lt;- target_pos - current_pos&#10; distance &amp;lt;- sqrt(sum(direction^2))&#10; &#10; if(distance &amp;lt; threshold) break # 提前终止条件&#10; &#10; unit_direction &amp;lt;- direction / distance&#10; displacement &amp;lt;- V * dt * unit_direction&#10; &#10; new_positions[i, ] &amp;lt;- current_pos + displacement&#10; }&#10; &#10; # 记录当前时刻所有位置&#10; for(i in 1:4) {&#10; history &amp;lt;- rbind(history, data.frame(&#10; turtle = i,&#10; time = current_time,&#10; x = turtles[i, 1],&#10; y = turtles[i, 2]&#10; ))&#10; }&#10; &#10; # 同步更新所有乌龟位置&#10; turtles &amp;lt;- new_positions&#10; current_time &amp;lt;- current_time + dt&#10;}&#10;&#10;# 可视化静态轨迹&#10;ggplot(history, aes(x, y, color = factor(turtle))) +&#10; geom_path(aes(group = turtle), linewidth = 0.8) +&#10; geom_point(data = subset(history, time == min(time)), &#10; size = 4, shape = 19) + # 起始点&#10; geom_point(data = subset(history, time == max(time)), &#10; size = 4, shape = 4) + # 终止点&#10; scale_color_manual(values = c(&amp;quot;#FF6B6B&amp;quot;, &amp;quot;#4ECDC4&amp;quot;, &amp;quot;#45B7D1&amp;quot;, &amp;quot;#96CEB4&amp;quot;)) +&#10; coord_fixed() +&#10; theme_minimal() +&#10; labs(title = &amp;quot;四龟追逐运动轨迹&amp;quot;, color = &amp;quot;乌龟编号&amp;quot;)&#10;&#10;# 生成动态可视化（需要安装gganimate）&#10;anim &amp;lt;- ggplot(history, aes(x, y, color = factor(turtle))) +&#10; geom_path(aes(group = turtle), alpha = 0.5) +&#10; geom_point(size = 3) +&#10; transition_reveal(time) +&#10; scale_color_manual(values = c(&amp;quot;#FF6B6B&amp;quot;, &amp;quot;#4ECDC4&amp;quot;, &amp;quot;#45B7D1&amp;quot;, &amp;quot;#96CEB4&amp;quot;)) +&#10; coord_fixed() +&#10; labs(title = '时间: {frame_along}', x = 'X坐标', y = 'Y坐标', color = '乌龟编号')&#10;&#10;animate(anim, fps = 20, duration = total_time, width = 600, height = 600)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;</description></item></channel></rss>