求 Mathematica 中积分定义的函数的最小值
我需要找到函数 f(t) = int g(t,x) dx 在 [0,1] 上的最小值。我在mathematica中所做的如下:
f[t_] = NIntegrate[g[t,x],{x,-1,1}]
FindMinimum[f[t],{t,t0}]
然而mathematica在第一次尝试时就停止了,因为NIntegrate不支持符号t。它需要一个特定的值来评估。尽管 Plot[f[t],{t,0,1}] 工作正常,但 FindMinimum 停在初始点.
我不能用 Integrate 替换 NIntegrate,因为函数 g 有点复杂,如果你输入 Integrate,mathematica 就会继续运行......
有什么方法可以解决它吗?谢谢!
I need to find the minimum of a function f(t) = int g(t,x) dx over [0,1]. What I did in mathematica is as follows:
f[t_] = NIntegrate[g[t,x],{x,-1,1}]
FindMinimum[f[t],{t,t0}]
However mathematica halts at the first try, because NIntegrate does not work with the symbolic t. It needs a specific value to evaluate. Although Plot[f[t],{t,0,1}] works perferctly, FindMinimum stops at the initial point.
I cannot replace NIntegrate by Integrate, because the function g is a bit complicated and if you type Integrate, mathematica just keep running...
Any way to get around it? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
我对您的代码做了两个相关更改:
使用
:=
定义 f,而不是使用=
。当 f 的用户提供了参数的值时,这有效地给出了 f“稍后”的定义。请参阅 SetDelayed。用
t_?NumericQ
而不是t_
定义f。这表示,t 可以是任何数字(Pi、7、0 等)。但不是任何非数字(t、x、“foo”等)。Try this:
Two relevant changes I made to your code:
Define f with
:=
instead of with=
. This effectively gives a definition for f "later", when the user of f has supplied the values of the arguments. See SetDelayed.Define f with
t_?NumericQ
instead oft_
. This says, t can be anything numeric (Pi, 7, 0, etc). But not anything non-numeric (t, x, "foo", etc).一点分析...
只要 Mathematica 可以对 g[t,x] 对 x 进行符号积分,您就可以获得精确答案,并完全避免数值积分的繁重工作,然后符号微分 wrtt 一个不那么简单的例子,具有更复杂的 g[t,x],包括 x 和 t 中的多项式积:
结果:
An ounce of analysis...
You can get an exact answer and completely avoid the heavy lifting of the numerical integration, as long as Mathematica can do symbolic integration of g[t,x] w.r.t x and then symbolic differentiation w.r.t. t. A less trivial example with a more complicated g[t,x] including polynomial products in x and t:
with the result:
函数的最小值只能在其导数的零点处,那么为什么首先要积分呢?
FindRoot
或Solve
来查找g
的根g的导数来验证点是否确实是局部最小值
(此时应该是积极的)。NIntegrate
找到f
的最小值 - 只需一次数值积分!Minimum of the function can be only at zero-points of it's derivate, so why to integrate in the first place?
FindRoot
orSolve
to find roots ofg
g
(it should be positive at that point).NIntegrate
to find minimum value off
- only one numerical integration!