自动化数学过程:我需要工业量的结果,我得到了一个

发布于 2024-09-07 08:47:25 字数 1010 浏览 3 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撑一把青伞 2024-09-14 08:47:25

@Paulo,您需要做的第一件事就是整理您的 Mathematica,以便您和我们能够见树不见林。例如,您的第八条语句:

Table[
  Table [rnddf[i], {i, 1, 29}], {j, 1, 100}];

创建一个大表,但该表未分配给变量或以任何其他方式使用。似乎还有其他语句的结果也没有被使用。

接下来,您应该放弃 For 循环并使用 Mathematica 习语 - 对于我们经常使用 Mathematica 的人来说,它们更清晰易懂,对您来说也更容易去写,而且可能也更有效率。 你的陈述

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}];

如果我理解正确的话,

diffomega = Differences[RandomReal[NormalDistribution[0,0.1],{30}]];

可以被替换为:永远记住,如果你在 Mathematica 中编写循环,你可能会犯一个错误。您应该进行的下一个更改是停止使用 PutPutAppend ,直到您可以在内存中至少构建您想要写入的整个输出的一个小示例。然后使用 SaveExport 或其他高级 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:

Table[
  Table [rnddf[i], {i, 1, 29}], {j, 1, 100}];

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 statements

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}];

can, if I understand correctly, be replaced by:

diffomega = Differences[RandomReal[NormalDistribution[0,0.1],{30}]];

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 and PutAppend 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 with Save or Export 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文