2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > matlab多变量优化 matlab - Matlab使用fminsearch优化多变量 - 堆栈内存溢出

matlab多变量优化 matlab - Matlab使用fminsearch优化多变量 - 堆栈内存溢出

时间:2018-06-27 16:45:19

相关推荐

matlab多变量优化 matlab - Matlab使用fminsearch优化多变量 - 堆栈内存溢出

我正在使用Matlab fminsearch来最小化带有两个变量的方程sum((interval-5).^2, 2)*factor间隔是一个包含5个值的向量。 只能从1到30的步长为1的顺序选择它们。因子是0.1到0.9的值。

代码如下。 我认为区间值是正确的,但因子值是错误的。

间隔值:[3 4 5 6 7]因子值:0.6最终输出:6

我认为因子值应为0.1,最终输出应为1作为全局最小值。

%% initialization of problem parameters

minval = 1;

maxval = 30;

step = 1;

count = 5;

minFactor = 0.1;

maxFactor = 0.9;

%% the objective function

fun = @(interval, factor) sum((interval-5).^2, 2)*factor;

%% a function that generates an interval from its initial value

getinterval = @(start) floor(start) + (0:(count-1)) * step;

getfactor =@(start2) floor(start2 * 10)/10;

%% a modified objective function that handles constraints

objective = @(start, start2) f(start, fun, getinterval, minval, maxval, getfactor, minFactor, maxFactor);

%% finding the interval that minimizes the objective function

start = [(minval+maxval)/2 (minFactor+maxFactor)/2];

y = fminsearch(objective, start);

bestvals = getinterval(y(1));

bestfactor = getfactor(y(2));

eval = fun(bestvals,bestfactor);

disp(bestvals)

disp(bestfactor)

disp(eval)

该代码使用以下函数f 。

function y = f(start, fun, getinterval, minval, maxval, getfactor, minFactor, maxFactor)

interval = getinterval(start(1));

factor = getfactor(start(2));

if (min(interval) < minval) || (max(interval) > maxval) || (factormaxFactor)

y = Inf;

else

y = fun(interval, factor);

end

end

我按照亚当的建议尝试了GA函数。 考虑到我的变量来自不同的范围和步骤,我将其更改为两个不同的集合。 这是我的零钱。

step1 = 1;

set1 = 1:step1:30;

step2 = 0.1;

set2 = 0.1:step2:0.9;

% upper bound depends on how many integer used for mapping

ub = zeros(1, nvar);

ub(1) = length(set1);

ub(2) = length(set2);

然后,我改变了目标函数

% objective function

function y = f(x,set1, set2)

% mapping

xmap1 = set1(x(1));

xmap2 = set2(x(2));

y = (40 - xmap1)^xmap2;

end

运行代码后,我想我会得到想要的答案。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。