异常的 Solve[] 行为延迟
对于一个涉及从光谱数据计算键长的项目,我使用 Solve[]
来求解一个相当简单的未知方程。当我更改输入时,我开始注意到“异常”行为。具体来说,当我改变一个数字并求解时,结果就是之前的答案。然而,再次执行代码会给出正确的答案;就好像一次执行有延迟。 这是一个例子:
B = (11.09 + del)*2.998*10^10;
c = h*1000*n*10^20/(8 \[Pi]^2);
h = 6.62618*10^-34;
n = 6.02204*10^23;
del = 0;
Solve[c/B == 0.97959253 R^2, R]
执行给出
{{R -> -1.24567}, {R -> 1.24567}}
但是,当我在上面的块中将 del = 0
更改为 del = 10
时,我在执行时得到相同的答案!当我第二次执行该块时,我得到了正确的答案:
{{R -> -0.903299}, {R -> 0.903299}}
然后,将 del = 10
更改回 del = 0
并执行给出:
{{R -> -0.903299}, {R -> 0.903299}}
正如你可以想象的,第二次执行该块给出了正确的答案
{{R -> -1.24567}, {R -> 1.24567}}
0 和 10 没有什么特别的,任何 2 个数字都可以。就像这个 Solve[]
块具有延迟效果......
我不确定这是否是我的计算机(MacBook Intel)的一个怪癖,或者它是否是 Solve 固有的东西。告诉我,你们在运行这段代码时是否遇到与我相同的行为,如果是,您知道为什么会发生这种情况吗? (我尝试重新启动 Mathematica 并再次运行它,它总是这样)。
For a project that involved computing bond lengths from spectroscopic data, I used Solve[]
to solve a fairly simple equation for an unknown. I began noticing "unusual" behavior when I changed the input. Specifically, when I changed a number and Solve'd, the result is the answer previously. Executing the code again, however, gives the correct answer; it's like there's a delay of one execution.
Here's an example:
B = (11.09 + del)*2.998*10^10;
c = h*1000*n*10^20/(8 \[Pi]^2);
h = 6.62618*10^-34;
n = 6.02204*10^23;
del = 0;
Solve[c/B == 0.97959253 R^2, R]
Executing gives
{{R -> -1.24567}, {R -> 1.24567}}
However, when I change del = 0
to del = 10
in the above block, I get the same answer when I execute! When I execute the block a second time, I get the correct answer:
{{R -> -0.903299}, {R -> 0.903299}}
Then, changing del = 10
back to del = 0
and executing gives:
{{R -> -0.903299}, {R -> 0.903299}}
and as you can imagine, executing the block a second time gives the correct answer
{{R -> -1.24567}, {R -> 1.24567}}
There is nothing special about 0 and 10, any 2 numbers work. It's like this Solve[]
block has a delay effect...
I'm not sure if this is a quirk of my computer (MacBook Intel) or if it's something inherent in Solve. Tell me if you guys get the same behavior as I did when running this code, and if so, do you have any idea why this is happening? (I have tried restarting Mathematica and running it again, and it always behaves this way).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您的
del
定义位于存在del
的其他定义之后。因此,当这些执行时,del
仍然具有其旧值。您有 2 个选择:将del
赋值放在顶部:或使用
SetDelayed
(:=
) 进行赋值:The problem is that your
del
definition is after some others wheredel
is present. Therefore, at the time when those execute,del
still has its old value. You have 2 choices: either placedel
assignment on top:or use
SetDelayed
(:=
) for assignment:我认为解决 PJR 问题的最简单方法就是在
代码之前添加。那么
del
的改变将没有机会重新定义B
。 ^_^I think the simplest solution to PJR's problem is just adding
beofroe the code. Then the changes of
del
will have no chance to redefineB
. ^_^