使用 fminsearch 找到局部最小值和该值的最小值
Use fminsearch to find local minimizer and the minima at that value
我在使用 fminsearch
时遇到问题:收到错误提示,我的函数没有足够的输入参数。
f = @(x1,x2,x3) x1.^2 + 3.*x2.^2 + 4.*x3.^2 - 2.*x1.*x2 + 5.*x1 + 3.*x2 + 2.*x3;
[x, val] = fminsearch(f,0)
我的功能有问题吗?每当我想将它用作任何其他命令的输入函数时,我总是会出错。
这正是 Matlab 告诉您的:您的函数需要三个参数。你只超过了一个。
而不是
[x, val] = fminsearch(f,0)
你应该这样称呼它
[x, val] = fminsearch(f,[0,0,0])
因为您定义函数 f
仅接受三维向量作为输入。
您可以在 http://mathworks.com/help/matlab/ref/fminsearch.html:
的在线文档中阅读有关 fminsearch
规范的更多信息
x = fminsearch(fun,x0) starts at the point x0 and returns a value x
that is a local minimizer of the function described in fun. x0 can be
a scalar, vector, or matrix. fun is a function_handle.
I am having trouble using fminsearch
[...]
就此打住,再考虑一下您要最小化的函数。
此处不需要数值优化(fminsearch
所做的)。你的函数是向量x
的二次函数;也就是说,它在x
的值可以表示为
x^T A x + b^T x
其中矩阵 A
和向量 b
定义如下(使用 MATLAB 符号):
A = [ 1 -1 0;
-1 3 0;
0 0 4]
和
b = [5 3 2].'
因为 A
是正定的,你的函数只有一个最小值,可以在 MATLAB 中用
计算
x_sol = -0.5 * A \ b;
现在,如果您对 运行 所犯错误的原因感到好奇,请查看 ;但尽可能不使用 fminsearch
。
我在使用 fminsearch
时遇到问题:收到错误提示,我的函数没有足够的输入参数。
f = @(x1,x2,x3) x1.^2 + 3.*x2.^2 + 4.*x3.^2 - 2.*x1.*x2 + 5.*x1 + 3.*x2 + 2.*x3;
[x, val] = fminsearch(f,0)
我的功能有问题吗?每当我想将它用作任何其他命令的输入函数时,我总是会出错。
这正是 Matlab 告诉您的:您的函数需要三个参数。你只超过了一个。
而不是
[x, val] = fminsearch(f,0)
你应该这样称呼它
[x, val] = fminsearch(f,[0,0,0])
因为您定义函数 f
仅接受三维向量作为输入。
您可以在 http://mathworks.com/help/matlab/ref/fminsearch.html:
的在线文档中阅读有关fminsearch
规范的更多信息
x = fminsearch(fun,x0) starts at the point x0 and returns a value x that is a local minimizer of the function described in fun. x0 can be a scalar, vector, or matrix. fun is a function_handle.
I am having trouble using
fminsearch
[...]
就此打住,再考虑一下您要最小化的函数。
此处不需要数值优化(fminsearch
所做的)。你的函数是向量x
的二次函数;也就是说,它在x
的值可以表示为
x^T A x + b^T x
其中矩阵 A
和向量 b
定义如下(使用 MATLAB 符号):
A = [ 1 -1 0;
-1 3 0;
0 0 4]
和
b = [5 3 2].'
因为 A
是正定的,你的函数只有一个最小值,可以在 MATLAB 中用
x_sol = -0.5 * A \ b;
现在,如果您对 运行 所犯错误的原因感到好奇,请查看 fminsearch
。