如何强制微分方程的变量为实数。警告引起的问题:“NDSolve::evfrf:”

发布于 2024-12-05 05:43:27 字数 587 浏览 4 评论 0原文

当我使用以下代码对颂歌进行数值求解时,会出现名为“evfrf”的警告。

我想知道如何强制微分方程的变量为实数

NDSolve[{y''[t] + .1 y'[t] + Sin[y[t]] == 0, y'[0] == 1, 
y[0] == 0}, y, {t, 0, 20}, 
Method -> {"EventLocator", "Event" -> y[t], 
"EventCondition" -> y'[t] > 0, 
"EventAction" :> Print[t, ", ", y[t], ", ", y'[t]]}]

警告消息:

NDSolve::evfrf: 
The event function did not evaluate to a real number somewhere 
between t =  1.5798366385128957` and t = 1.6426647495929725`,
preventing FindRoot from finding the root accurately. >>  

谢谢:)

When I numerically solving a ode with the following code, warnings named "evfrf" prompted.

I am wondering how to force variables of differential equations to be Real numbers

NDSolve[{y''[t] + .1 y'[t] + Sin[y[t]] == 0, y'[0] == 1, 
y[0] == 0}, y, {t, 0, 20}, 
Method -> {"EventLocator", "Event" -> y[t], 
"EventCondition" -> y'[t] > 0, 
"EventAction" :> Print[t, ", ", y[t], ", ", y'[t]]}]

warning message:

NDSolve::evfrf: 
The event function did not evaluate to a real number somewhere 
between t =  1.5798366385128957` and t = 1.6426647495929725`,
preventing FindRoot from finding the root accurately. >>  

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

意犹 2024-12-12 05:43:27

该错误消息似乎是由 "EventCondition" -> 引起的仅 y'[t] >= 0 部分。我不知道问题出在哪里,但考虑到您想将事件 (y[t]==0) 限制为向上的段落 (y'[t]>0),您可以将该部分替换为 <代码>“方向”-> 1 其作用相同。

或者,您可以简单地使用 Off[NDSolve::evfrf] 关闭消息,因为它似乎对最终结果没有影响。 “方向”-> 1 方法产生与生成消息的原始事件相同的事件。

The error message seems to be caused by the "EventCondition" -> y'[t] >= 0 part only. I don't know what the problem is there, but given that you want to restrict events (y[t]==0) to passages going up (y'[t]>0), you can replace that part with "Direction" -> 1 which does the same.

Alternatively, you could simply switch off the message using Off[NDSolve::evfrf] as it doesn't seem to make a difference in the final result. The "Direction" -> 1 method yields the same events as the original one which generated the messages.

山川志 2024-12-12 05:43:27

我认为这并不是答案在这些点上真正成为复数的问题。
以下不会报错。

sol = NDSolve[{y''[t] + .1 y'[t] + Sin[y[t]] == 0, y'[0] == 1, 
 y[0] == 0}, y, {t, 0, 20}]

Plot[y[t] /. sol, {t, 0, 20}]

在此处输入图像描述

问题在于尝试在 y'[t] 中查找零以及隐含的寻根过程的局限性。我尝试增加 WorkingPrecisionMaxSteps 但它没有消除错误。

sol = NDSolve[{y''[t] + .10`64 y'[t] + Sin[y[t]] == 0, y'[0] == 1, 
y[0] == 0}, y, {t, 0, 20}, 
  Method -> {"EventLocator", "Event" -> y[t], 
 "EventCondition" -> y'[t] >= 0, 
 "EventAction" :> Print[t, ", ", y[t], ", ", y'[t]]},  
   MaxSteps -> 10^9, MaxStepSize -> 0.0001, WorkingPrecision -> 32]

除非您真的关心第八位或随后的小数位,否则我建议不要担心此错误。

那些在数值分析方面比我更专业的人可能会不同意,但在我工作的领域,我们通常对百分比变化的小数点后第一位(级别的小数点后第三位)之后的任何数据的准确性没有任何信心。

I don't think this is an issue of the answer genuinely being a complex number at those points.
The following does not give an error.

sol = NDSolve[{y''[t] + .1 y'[t] + Sin[y[t]] == 0, y'[0] == 1, 
 y[0] == 0}, y, {t, 0, 20}]

Plot[y[t] /. sol, {t, 0, 20}]

enter image description here

The issue is the attempt to find the zero in y'[t] and limitations in the implied root-finding process. I tried increasing the WorkingPrecision and the MaxSteps but it didn't remove the error.

sol = NDSolve[{y''[t] + .10`64 y'[t] + Sin[y[t]] == 0, y'[0] == 1, 
y[0] == 0}, y, {t, 0, 20}, 
  Method -> {"EventLocator", "Event" -> y[t], 
 "EventCondition" -> y'[t] >= 0, 
 "EventAction" :> Print[t, ", ", y[t], ", ", y'[t]]},  
   MaxSteps -> 10^9, MaxStepSize -> 0.0001, WorkingPrecision -> 32]

Unless you really care about the eighth or subsequent decimal place, I would suggest not worrying about this error.

Those more expert than me in numerical analysis might disagree, but I work in a field where we usually don't have any faith in the accuracy of any data past the first decimal place of a percentage change (third decimal place of a level).

走野 2024-12-12 05:43:27

使用EventLocator 重要吗?是否可以求解 y' 然后对其应用 FindRoot?类似于:

ndsolveOptions = {MaxSteps -> Infinity, Method -> {"StiffnessSwitching", 
  Method ->{"ExplicitRungeKutta", Automatic}}, AccuracyGoal -> 10,PrecisionGoal -> 10};

sol = First@NDSolve[{y''[t] + .1 y'[t] + Sin[y[t]] == 0, y'[0] == 1, y[0] == 0}, 
     {y[t], y'[t]}, {t, 0, 20}, Sequence@ndsolveOptions];

der = y'[t] /. sol;
Plot[der, {t, 1.2, 1.7}]

FindRoot[der, {t, 1.6}]

      {t -> 1.614}

在此处输入图像描述

is it important to use the EventLocator? is it possible to solve for y' and then apply FindRoot on it? something like:

ndsolveOptions = {MaxSteps -> Infinity, Method -> {"StiffnessSwitching", 
  Method ->{"ExplicitRungeKutta", Automatic}}, AccuracyGoal -> 10,PrecisionGoal -> 10};

sol = First@NDSolve[{y''[t] + .1 y'[t] + Sin[y[t]] == 0, y'[0] == 1, y[0] == 0}, 
     {y[t], y'[t]}, {t, 0, 20}, Sequence@ndsolveOptions];

der = y'[t] /. sol;
Plot[der, {t, 1.2, 1.7}]

FindRoot[der, {t, 1.6}]

      {t -> 1.614}

enter image description here

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