自动化数学过程:我需要工业量的结果,我得到了一个
我在mathematica中有以下简单的代码,这就是我想要的单个输出。但我想得到数百或数千。我该怎么办?
Clear["Global`*"]
k = 2; Put["phiout"]; Put["omegadiffout"];
Random[NormalDistribution[0, 0.1]];
For[i = 1, i < 31,
rnd[i] = Random[NormalDistribution[0, 0.1]]; i++]
Table[rnd[i], {i, 1, 30}]
For[i = 1, i < 30,
rnddf[i] = rnd[i + 1] - rnd[i]; i++
]
diffomega = Table [rnddf[i], {i, 1, 29}];
Table[
Table [rnddf[i], {i, 1, 29}], {j, 1, 100}];
PutAppend[Table[
diffomega, {j, 1, 100}] , "diffomega"]
eqs0 = Table [
k*phi[i + 1] + k*phi[i - 1] - 2*k*phi[i] - rnddf[i] == 0, {i, 1,
28}];
eqs1 = eqs0 /. {phi[0] -> phi[30], phi[31] -> phi[1]};
Sum[phi[i], {i, 1, 29}];
eqs2 = Append[eqs1, - phi[1] - phi[27] - 3 phi[29] == 0];
eqs3 = eqs2 /. {phi[30] -> -Sum[phi[i], {i, 1, 29}]};
vars = Table [phi[i], {i, 1, 29}];
eqs = NSolve[eqs3, vars];
PutAppend[diffomega, eqs , "phiout"]
这仅将最后一个值放入文件“phiout”和“omegadiffout”中。我需要数百个。对于每个随机生成,我需要一个输出。
提前致谢
I've the following simple code in mathematica that is what I want to a single output. But I want to get hundreds or thousands of it. How can I do?
Clear["Global`*"]
k = 2; Put["phiout"]; Put["omegadiffout"];
Random[NormalDistribution[0, 0.1]];
For[i = 1, i < 31,
rnd[i] = Random[NormalDistribution[0, 0.1]]; i++]
Table[rnd[i], {i, 1, 30}]
For[i = 1, i < 30,
rnddf[i] = rnd[i + 1] - rnd[i]; i++
]
diffomega = Table [rnddf[i], {i, 1, 29}];
Table[
Table [rnddf[i], {i, 1, 29}], {j, 1, 100}];
PutAppend[Table[
diffomega, {j, 1, 100}] , "diffomega"]
eqs0 = Table [
k*phi[i + 1] + k*phi[i - 1] - 2*k*phi[i] - rnddf[i] == 0, {i, 1,
28}];
eqs1 = eqs0 /. {phi[0] -> phi[30], phi[31] -> phi[1]};
Sum[phi[i], {i, 1, 29}];
eqs2 = Append[eqs1, - phi[1] - phi[27] - 3 phi[29] == 0];
eqs3 = eqs2 /. {phi[30] -> -Sum[phi[i], {i, 1, 29}]};
vars = Table [phi[i], {i, 1, 29}];
eqs = NSolve[eqs3, vars];
PutAppend[diffomega, eqs , "phiout"]
This put on file "phiout" and "omegadiffout" only the last value. I need hundreds of them. For each random generations I need an output.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@Paulo,您需要做的第一件事就是整理您的 Mathematica,以便您和我们能够见树不见林。例如,您的第八条语句:
创建一个大表,但该表未分配给变量或以任何其他方式使用。似乎还有其他语句的结果也没有被使用。
接下来,您应该放弃
For
循环并使用 Mathematica 习语 - 对于我们经常使用 Mathematica 的人来说,它们更清晰易懂,对您来说也更容易去写,而且可能也更有效率。 你的陈述如果我理解正确的话,
可以被替换为:永远记住,如果你在 Mathematica 中编写循环,你可能会犯一个错误。您应该进行的下一个更改是停止使用
Put
和PutAppend
,直到您可以在内存中至少构建您想要写入的整个输出的一个小示例。然后使用Save
或Export
或其他高级 I/O 函数之一将其一次性写入文件。当你完成此操作后,编辑你的代码并解释你想要做什么,我和其他 SOers 将尽力提供进一步的帮助。不幸的是,现在,您的代码是如此不符合 Mathematica 标准,以至于我无法弄清楚它。
The first thing you need to do, @Paulo, is tidy up your Mathematica so that you, and we, can see the wood for the trees. For example, your 8th statement:
makes a large table, but the table isn't assigned to a variable or used in any other way. There seem to be other statements whose results aren't used either.
Next you should abandon your
For
loops and use the Mathematica idioms -- they are clearer for we who use Mathematica regularly to understand, easier for you to write, and probably more efficient too. Your statementscan, if I understand correctly, be replaced by:
Always remember that if you are writing loops in Mathematica you are probably making a mistake. The next change you should make is to stop using
Put
andPutAppend
until you can build, in memory, at least a small example of the entire output that you want to write. Then write that to a file in one go withSave
orExport
or one of the other high-level I/O functions.When you have done that, edit your code and explain what you are trying to do and I, and other SOers, will try to help further. Unfortunately, right now, your code is so un-Mathematica-al that I'm having trouble figuring it out.