异常的 Solve[] 行为延迟

发布于 2024-11-06 10:21:32 字数 1031 浏览 0 评论 0原文

对于一个涉及从光谱数据计算键长的项目,我使用 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 技术交流群。

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

发布评论

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

评论(2

梦明 2024-11-13 10:21:33

问题是您的 del 定义位于存在 del 的其他定义之后。因此,当这些执行时,del仍然具有其旧值。您有 2 个选择:将 del 赋值放在顶部:

del = 0;
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;
Solve[c/B == 0.97959253 R^2, R]

或使用 SetDelayed (:=) 进行赋值:

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]

The problem is that your del definition is after some others where del is present. Therefore, at the time when those execute, del still has its old value. You have 2 choices: either place del assignment on top:

del = 0;
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;
Solve[c/B == 0.97959253 R^2, R]

or use SetDelayed (:=) for assignment:

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]
蓝梦月影 2024-11-13 10:21:33

我认为解决 PJR 问题的最简单方法就是在

Clear["Global`*"]  

代码之前添加。那么del的改变将没有机会重新定义B。 ^_^

I think the simplest solution to PJR's problem is just adding

Clear["Global`*"]  

beofroe the code. Then the changes of del will have no chance to redefine B. ^_^

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