是否可以使用优化功能fmincon
用Matlab的定义函数?
我写的,我给几个固定参数(实数或复数),现在来看,我每次更改这些参数时,结果的变化(你不说)功能。
[output1, output2] = my_function(input1,input2,input3,input4)
我看到fmincon
功能可以找到一个给定的约束条件的最佳结果。比方说,我想找到最佳的输出只在input1
作用,并保持恒定的所有其他输入。是否有可能定义类似
fmincon(@(input1)my_function,[1,2],[],mean)
对于input1
从1变为2为最佳值mean
,其中的意思是一些其他结果的平均值。
我知道这是一个相当模糊的问题,但我不能给最低例如,由于function
使得很多东西
具有多个输出的第一次尝试给了我错误qazxsw POI
然后我尝试只有一个输出,如果我使用
Only functions can return multiple values.
我得到的错误
错误:“输出1”以前作为一个变量,其使用这里作为一个功能或命令的名称相冲突。参见MATLAB文档细节“如何MATLAB确认命令语法”。
随着output1 = @(input1)function(input2,input3);
fmincon(@output1,[1,2],[],mean)
我得到fmincon(@my_function,[1,2],[],mean)
输入应该在你的函数定义中使用 - 在Not enough input arguments.
应该怎么写读了。你不必使用匿名函数定义实际的目标函数(以下anonymous functions),你可以在自己的文件中使用的功能。关键是,目标函数应该返回到最小化标量。
这里是一个非常简单的例子,使用myFunction
找到fmincon
最小值的基础上,初步推测myFunction
。
[1.5,1.5]
你可以看到计算出的最佳是不完全% myFunction is min when x=1,y=2
myFunction = @(x,y) (x-1).^2 + (y-2).^2;
% Define the optimisation function.
% This should take one input (can be an array)
% and output a scalar to be minimised
optimFunc = @(P) myFunction( P(1), P(2) );
% Use fmincon to find the optimum solution, based on some initial guess
optimSoln = fmincon( optimFunc, [1.5, 1.5] );
% >> optimSoln
% optimSoln =
% 0.999999990065893 1.999999988824129
% Optimal x = optimSoln(1), optimal y = optimSoln(2);
,但它是默认的最优公差范围内。您可以更改[1,2]
求解器的选项 - 读fmincon
。
如果你想保持documentation为一个常数,你只需要更新的功能定义:
y=1
您可以添加使用% We only want solutions with y=1
optimFunc_y1 = @(P) myFunction( P(1), 1 ); % y=1 always
% Find new optimal solution
optimSoln_y1 = fmincon( optimFunc_y1, 1.5 );
% >> optimSoln_y1
% optimSoln_y1 =
% 0.999999990065893
% Optimal x when y=1 = optimSoln(1)
,A
,B
和Aeq
投入Beq
不等式约束,但是这太宽而不能进入这里,请参考文档。
请注意,您使用的关键字fmincon
在某种程度上这是无效的语法。我代替有效的变量名在我的演示功能。