追逐问题的代码如何写得比较Mathematica style?
mathematica吧
全部回复
仅看楼主
level 8
we
bp
lus.nuist.edu.cn/s/49/t/82/p/42/c/416/list.jspy
这个也算比较经典的问题了,做法也有不少,这次不是探讨哪种做法好的,主要是想把那个过程式的代码写得更符合Mathematica风格。我已经改了一部分,但是还是没有避免AppendTo
2018年01月16日 11点01分 1
level 8
Clear["`*"];
v = 1; t = 18; dt = 0.01; n = t/dt;
T = {{{0, 10}}, {{10, 10}}, {{10, 0}}, {{0, 0}}};
Do[
{x1, y1} = T[[i, j]];
{x2, y2} = T[[Mod[i + 1, 4, 1], j]];
x1 = x1 + (v dt (x2 - x1))/Sqrt[(x2 - x1)^2 + (y2 - y1)^2];
y1 = y1 + (v dt (y2 - y1))/Sqrt[(x2 - x1)^2 + (y2 - y1)^2];
AppendTo[T[[i]], {x1, y1}],
{j, n}, {i, 4}
];
Graphics[{Thread[{{Red, Green, Blue, Orange}, Line /@ T}],
Line[{{0, 10}, {10, 10}, {10, 0}, {0, 0}, {0, 10}}]}]
2018年01月16日 11点01分 2
吧务
level 12
v = 1; t = 18; dt = 0.01; n = Quotient[t, dt];
NestList[
# + v*dt*Normalize /@ (RotateLeft[#] - #) &,
N@{{0, 10}, {10, 10}, {10, 0}, {0, 0}}, n - 1]\[Transpose];
ListLinePlot[T, AspectRatio -> 1]
2018年01月16日 11点01分 3
NestList前面少加了个T =
2018年01月16日 12点01分
厉害![大拇指]
2018年01月16日 12点01分
level 1

ds=0.0025;stp={{ds,0}};lt=1;
While[lt>2 ds,lt=Norm[Last@AnglePath[stp]-{0.5,0.5}] Sqrt[2];stp=Append[stp,{ds,ds/lt}]];
Graphics[Rotate[Line[{AnglePath[stp],AnglePath[{0}]}],#,{0.5,0.5}]&/@(Pi/2 Range[0,3]),ImagePadding->30]
2018年01月17日 03点01分 7
1