Mathematica - 查找最大值 NDSolve 图
在数值求解微分方程并绘制结果后,我想确定绘制范围内的单个最大值,但不知道如何确定。
下面的代码用于数值求解微分方程并绘制结果。
s = NDSolve[{x''[t] + x[t] - 0.167 x[t]^3 == 0.005 Cos[t + -0.0000977162*t^2/2], x[0] == 0, x'[0] == 0}, x, {t, 0, 1000}]
Plot[Evaluate[x[t] /. s], {t, 0, 1000},
Frame -> {True, True, False, False}, FrameLabel -> {"t", "x"}, FrameStyle -> Directive[FontSize -> 15], Axes -> False]
After numerically solving a differential equation and plotting the results I would like to determine the single maximum value in the plotted range but do not know how.
The code below works for numerically solving the differential equation and plotting the results.
s = NDSolve[{x''[t] + x[t] - 0.167 x[t]^3 == 0.005 Cos[t + -0.0000977162*t^2/2], x[0] == 0, x'[0] == 0}, x, {t, 0, 1000}]
Plot[Evaluate[x[t] /. s], {t, 0, 1000},
Frame -> {True, True, False, False}, FrameLabel -> {"t", "x"}, FrameStyle -> Directive[FontSize -> 15], Axes -> False]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 NMaximize
第一个近似:
由于您的函数是像这样的快速振荡: ,它没有捕获真正的最大值,如下所示:
因此,我们细化边界,并调整NMaximize 函数很小:
它未能在所需的精度内收敛,但现在结果已经足够好
Use NMaximize
First approximation:
As your function is a rapid oscillation like this : , it doesn't catch the real max value, as you may see below:
So we refine our bounds, and tune a little the NMaximize function:
It failed to converge within the required accuracy, but now the result is good enough
您可以使用
Reap
和Sow
从任何评估中提取值列表。对于简单的Plot
,您可以Sow
您正在绘制的函数的值,并将整个绘图包含在Reap
中:<的第一个元素code>list 是绘图本身,第二个元素是绘图中使用的 Mathematica x 值列表。要获得最大值:
You can use
Reap
andSow
to extract a list of values from any evaluation. For a simplePlot
you wouldSow
the value of the function you are plotting and enclose the entire plot in aReap
:The first element of
list
is the plot itself and the second element is the list of x-values Mathematica used in the plot. To get the Maximum: