  MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档 Matlab use fminsearch to optimize multi variables
 用户名 记住信息 密码
 资料存档 资料存档
 主题工具 显示模式 2019-11-23, 08:41 PM #1 高级会员   注册日期: 2019-11-21 帖子: 2,897 感谢他人: 0 有 0 帖获得 0 感谢 声望力: 60  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 更多...  主题工具 显示可打印版本 邮寄本页给好友 显示模式 平板模式 切换到混合模式 切换到树形模式 发帖规则 您不可以发表新主题 您不可以发表回复 您不可以上传附件 您不可以编辑自己的帖子 启用 BB 代码 论坛禁用 表情符号 论坛启用 [IMG] 代码 论坛启用 HTML 代码Trackbacks are 禁用 Pingbacks are 禁用 Refbacks are 禁用 版面规则

 -- 简体中文 -- 繁體中文 -- English (US) MATLAB爱好者论坛 - 论坛存档 - 返回顶端 