Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
回复
 
主题工具 显示模式
旧 2019-11-23, 20:41   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
默认 Matlab use fminsearch to optimize multi variables

I am using Matlab fminsearch to minimize a equation with two variables sum((interval-5).^2, 2)*factorThe interval is a vector contains 5 values. They can be only picked sequentially from value 1 to 30 with step size is 1. The factor is a value from 0.1 to 0.9.

The code is below. I think the interval values are correct but factor value is wrong.

Interval value: [3 4 5 6 7]factor value: 0.6Final Output: 6

I think the factor value should be 0.1 and final output should be 1 as global minimum.

%% initialization of problem parametersminval = 1;maxval = 30;step = 1;count = 5;minFactor = 0.1;maxFactor = 0.9;%% the objective functionfun = @(interval, factor) sum((interval-5).^2, 2)*factor;%% a function that generates an interval from its initial valuegetinterval = @(start) floor(start) + (0:(count-1)) * step;getfactor =@(start2) floor(start2 * 10)/10;%% a modified objective function that handles constraintsobjective = @(start, start2) f(start, fun, getinterval, minval, maxval, getfactor, minFactor, maxFactor);%% finding the interval that minimizes the objective functionstart = [(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)The code uses the following function 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

更多...
poster 当前离线   回复时引用此帖
回复


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 16:20


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.