求大师看看一样的的程序,do循环和for循环得出的结果不一样,检查一下结果,do循环才是想要的答案。为什么会出现这种情况?应该怎么改?才能是他们一样。可不可以用nestlist?
hy = Table[.0, {i, 1, 201}];
ez = Table[.0, {i, 1, 201}];
t = {{0, 0}};
Do[
Do[hy[[m]] = hy[[m]] + (ez[[m + 1]] - ez[[m]])/377., {m, 1, 200}];
Do[ez[[m]] = ez[[m]] + (hy[[m]] - hy[[m - 1]])*377., {m, 2, 201}];
ez[[50]] = Exp[-(x - 20.0)*(x - 20.0)/(100.)];
If[Mod[x, 10] == 0,
Do[t = AppendTo[t, {m, Chop[ez[[m]]]}], {m, 1, 200}]],
{x, 0, 200}] // AbsoluteTiming;
hh = Table[.0, {i, 1, 201}];
th = Table[.0, {i, 1, 201}];
t = {{0, 0}};
For[x = 0, x < 250, x++,
For[m = 1, m < 200, m++,
hh[[m]] = hh[[m]] + (th[[m + 1]] - th[[m]])/377.0;
];
For[m = 2, m < 201, m++,
th[[m]] = th[[m]] + (hh[[m]] - hh[[m - 1]])*377.0;
];
th[[50]] = Exp[-(x - 30.0)*(x - 30.0)/100.0];
If[x == 0,
For[m = 1, m < 200, m++,
Print[{m, Chop[th[[m]]]}, ",", x];
]];
If[Mod[x, 10] == 0,
For[m = 1, m < 200, m++,
Print[{m, Chop[th[[m]]]}, ",", x];
]]];
hy = Table[.0, {i, 1, 201}];
ez = Table[.0, {i, 1, 201}];
t = {{0, 0}};
Do[
Do[hy[[m]] = hy[[m]] + (ez[[m + 1]] - ez[[m]])/377., {m, 1, 200}];
Do[ez[[m]] = ez[[m]] + (hy[[m]] - hy[[m - 1]])*377., {m, 2, 201}];
ez[[50]] = Exp[-(x - 20.0)*(x - 20.0)/(100.)];
If[Mod[x, 10] == 0,
Do[t = AppendTo[t, {m, Chop[ez[[m]]]}], {m, 1, 200}]],
{x, 0, 200}] // AbsoluteTiming;
hh = Table[.0, {i, 1, 201}];
th = Table[.0, {i, 1, 201}];
t = {{0, 0}};
For[x = 0, x < 250, x++,
For[m = 1, m < 200, m++,
hh[[m]] = hh[[m]] + (th[[m + 1]] - th[[m]])/377.0;
];
For[m = 2, m < 201, m++,
th[[m]] = th[[m]] + (hh[[m]] - hh[[m - 1]])*377.0;
];
th[[50]] = Exp[-(x - 30.0)*(x - 30.0)/100.0];
If[x == 0,
For[m = 1, m < 200, m++,
Print[{m, Chop[th[[m]]]}, ",", x];
]];
If[Mod[x, 10] == 0,
For[m = 1, m < 200, m++,
Print[{m, Chop[th[[m]]]}, ",", x];
]]];
