本文介绍了Mathematica用符号而不是数字来调用NMinimize吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mathematica中使用NMinimize时,我注意到以下行为.目标函数的第一次调用是使用变量名,而不是像人们期望的那样使用空格中的点.

I noticed the following behavior while using NMinimize in Mathematica. The first invocation of the objective function is with variable names, rather than with points from the space, as one would expect.

例如,如果我的目标函数是一个模块,则只调用一次该模块,对其进行符号求值,然后在进一步的迭代中,使用来自变量空间中的点对这个符号表达式求值.

So for example if my objective function is a module, this module is called only once, evaluated symbolically and then in further iterations, this symbolic expression is evaluated with points from the variable space.

对于大表达式,此行为可能会大大降低计算速度.有什么办法可以解决这个问题?还有其他人经历过吗?有什么办法可以加快NMinimize的速度吗?

This behavior could slow down the computation significantly for a large expression.Is there any way to get around this? Has anyone else experienced this? Is there any way to speed up NMinimize then?

示例:

dummy[x_] := Module[
  {},
  Print["x=", x ];
  4 x^4 - 4 x^2 + 1
  ]

In: NMinimize[dummy[x], x]
Out:x=x
{0., {x -> 0.707107}}

推荐答案

您是否尝试过定义仅对数字输入求值的函数?

Have you tried defining your function to only evaluate for numeric input?

dummy[x_?NumericQ] := ...

这篇关于Mathematica用符号而不是数字来调用NMinimize吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 06:29