求 Mathematica 中积分定义的函数的最小值

发布于 2024-09-14 20:14:33 字数 384 浏览 6 评论 0原文

我需要找到函数 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 技术交流群。

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

发布评论

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

评论(3

虫児飞 2024-09-21 20:14:33

试试这个:

In[58]:= g[t_, x_] := t^3 - t + x^2

In[59]:= f[t_?NumericQ] := NIntegrate[g[t, x], {x, -1, 1}]

In[60]:= FindMinimum[f[t], {t, 1}]

Out[60]= {-0.103134, {t -> 0.57735}}

In[61]:= Plot[f[t], {t, 0, 1}]

我对您的代码做了两个相关更改:

  1. 使用 := 定义 f,而不是使用 =。当 f 的用户提供了参数的值时,这有效地给出了 f“稍后”的定义。请参阅 SetDelayed

  2. t_?NumericQ而不是t_定义f。这表示,t 可以是任何数字(Pi、7、0 等)。但不是任何非数字(t、x、“foo”等)。

Try this:

In[58]:= g[t_, x_] := t^3 - t + x^2

In[59]:= f[t_?NumericQ] := NIntegrate[g[t, x], {x, -1, 1}]

In[60]:= FindMinimum[f[t], {t, 1}]

Out[60]= {-0.103134, {t -> 0.57735}}

In[61]:= Plot[f[t], {t, 0, 1}]

Two relevant changes I made to your code:

  1. 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.

  2. Define f with t_?NumericQ instead of t_. This says, t can be anything numeric (Pi, 7, 0, etc). But not anything non-numeric (t, x, "foo", etc).

只有影子陪我不离不弃 2024-09-21 20:14:33

一点分析...

只要 Mathematica 可以对 g[t,x] 对 x 进行符号积分,您就可以获得精确答案,并完全避免数值积分的繁重工作,然后符号微分 wrtt 一个不那么简单的例子,具有更复杂的 g[t,x],包括 x 和 t 中的多项式积:

g[t_, x_] := t^2 + (7*t*x - (x^3)/13)^2;
xMax = 1; xMin = -1; f[t_?NumericQ] := NIntegrate[g[t, x], {x, xMin, xMax}];
tMin = 0; tMax = 1;Plot[f[t], {t, tMin, tMax}];
tNumericAtMin = t /. FindMinimum[f[t], {t, tMax}][[2]];
dig[t_, x_] := D[Integrate[g[t, x], x], t];
Print["Differentiated integral is ", dig[t, x]];
digAtXMax = dig[t, x] /. x -> xMax; digAtXMin = dig[t, x] /. x -> xMin;
tSymbolicAtMin = Resolve[digAtXMax - digAtXMin == 0 && tMin ≤ t ≤ tMax, {t}];
Print["Exact: ", tSymbolicAtMin[[2]]];
Print["Numeric: ", tNumericAtMin];
Print["Difference: ", tSymbolicAtMin [[2]] - tNumericAtMin // N];

结果:

⁃Graphics⁃
Differentiated integral is 2 t x + 98 t x^3 / 3 - 14 x^5 / 65
Exact: 21/3380
Numeric: 0.00621302
Difference: -3.01143 x 10^-9

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:

g[t_, x_] := t^2 + (7*t*x - (x^3)/13)^2;
xMax = 1; xMin = -1; f[t_?NumericQ] := NIntegrate[g[t, x], {x, xMin, xMax}];
tMin = 0; tMax = 1;Plot[f[t], {t, tMin, tMax}];
tNumericAtMin = t /. FindMinimum[f[t], {t, tMax}][[2]];
dig[t_, x_] := D[Integrate[g[t, x], x], t];
Print["Differentiated integral is ", dig[t, x]];
digAtXMax = dig[t, x] /. x -> xMax; digAtXMin = dig[t, x] /. x -> xMin;
tSymbolicAtMin = Resolve[digAtXMax - digAtXMin == 0 && tMin ≤ t ≤ tMax, {t}];
Print["Exact: ", tSymbolicAtMin[[2]]];
Print["Numeric: ", tNumericAtMin];
Print["Difference: ", tSymbolicAtMin [[2]] - tNumericAtMin // N];

with the result:

⁃Graphics⁃
Differentiated integral is 2 t x + 98 t x^3 / 3 - 14 x^5 / 65
Exact: 21/3380
Numeric: 0.00621302
Difference: -3.01143 x 10^-9
夜雨飘雪 2024-09-21 20:14:33

函数的最小值只能在其导数的零点处,那么为什么首先要积分呢?

  • 您可以使用FindRootSolve来查找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?

  • You can use FindRoot or Solve to find roots of g
  • Then you can verify that points are really local minimums by checking derivates of g (it should be positive at that point).
  • Then you can NIntegrate to find minimum value of f - only one numerical integration!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文